Part4 1.spring中的JdbcTemplate 1.1 JdbcTemplate的作用 是spring框架中提供的一个对象,用于和数据库交互,实现对表的增删改查 1.2 事前配置 //配置bean.xml文件
<!--配置JdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/springDay"></property> <property name="username" value="root"></property> <property name="password" value="SUJR20051106."></property> </bean>1.3 两种配置方法 1.3.1 JdbcTemplate最基本的用法(不用spring)
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; public class JdbcTemplateDemo1 { public static void main(String[] args) { //准备数据源:spring的内置数据源 DriverManagerDataSource ds= new DriverManagerDataSource(); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost:3306/springDay"); ds.setUsername("root"); ds.setPassword("SUJR20051106."); //1.创建JdbcTemplate对象 JdbcTemplate jt = new JdbcTemplate(); //给jt设置数据源 jt.setDataSource(ds); //2.执行操作 jt.execute("insert into account(name,money)values('ccc',1000)"); } }1.3.2 spring版JdbcTemplate
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; /** * JdbcTemplate的最基本用法,spring版 */ public class JdbcTemplateDemo2 { public static void main(String[] args) { //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.获取对象 JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class); jt.execute("insert into account(name,money)value('ddd',200)"); } }1.4 实现基本的CRUD操作(单表)
import com.stay.domain.Account; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; /** * JdbcTemplate能实现的单表操作(增删改查) */ public class JdbcTemplateDemo3 { public static void main(String[] args) { //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.获取对象 JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class); //3.操作(单表操作) //保存 jt.update("insert into account(name,money)value(?,?)","eee",3333f); //更新 jt.update("update account set name=?,money=? where id=?","test",4567,7);//将id为7的那一行改成test 4567 //删除 jt.update("delete from account where id=?",8); //查询所有money大于1000的 List<Account> accounts = jt.query("select * from account where money > ?",new AccountRowMapper(),1000f); //另一种写法(spring直接提供的) //List<Account> accounts = jt.query("select * from account where money > ?",new BeanPropertyRowMapper<Account>(Account.class),1000f); for(Account account:accounts){ System.out.println(account); } //查询一个 List<Account> accounts2 = jt.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),1); System.out.println(accounts2.isEmpty()?"没有内容":accounts.get(0)); //查询返回一行一列(就是符合条件的总行数)(使用聚合函数,但不加group by子句) Long count = jt.queryForObject("select count(*) from account where money > ?",Long.class,1000f); System.out.println(count); } } //定义Account的封装策略 class AccountRowMapper implements RowMapper<Account>{ /** * 把结果集中的数据封装到Account中,然后由spring把每个Account加到集合中 * @param resultSet * @param i * @return * @throws SQLException */ public Account mapRow(ResultSet resultSet, int i) throws SQLException { Account account = new Account(); account.setId(resultSet.getInt("id")); account.setName(resultSet.getString("name")); account.setMoney(resultSet.getFloat("money")); return account; } }1.5 spring中基于xml的声明式事务控制配置步骤 1.5.0 导入约束
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">1.5.1 配置事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean>1.5.2 配置事务的通知 使用tx:advice配置事务通知 属性:1.id 给事务通知起一个唯一标识 2.transaction-manager 给事务通知提供一个事务管理器引用
<!--配置事务通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!--配置事务的属性(transfer是业务层接口中的方法) isolation:用于指定事务的隔离级别,默认值是DEFAULT 表示使用数据库的默认隔离级别 propagation:用于指定事务的传播行为,默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS read-only:用于指定事务是否只读,只有查询方法才能设置为true,默认值是false,表示读写 rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚,没有默认值。表示任何异常都回滚 no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚,没有默认值。表示任何异常都回滚 timeout:用于指定事务的超时时间 默认值是-1,表示永不超时。如果指定了数值,以秒为单位 --> <tx:attributes> <tx:method name="*" propagation="REQUIRED" read-only="false"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method> </tx:attributes> </tx:advice>1.5.3 配置AOP中通用的切入点表达式
<aop:config> <!--配置切入点表达式--> <aop:pointcut id="pt1" expression="execution(* com.stay.service.impl.*.*(..))"></aop:pointcut>1.5.4 建立事务通知和切入点表达式的对应关系
<!--建立切入点表达式和事务通知的对应关系--> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor> </aop:config>1.5.5 配置事务的属性 在事务的通知tx:advice标签的内部配置