spring整合jdbc提供了一个工具类JdbcTemplate
该类spring专门用来简化jdbc的操作,JdbcTemplate里面含有很多对jdbc的方法。
如何创建JdbcTemplate对象
a)JdbcTemplate里面的方法是实例方法,不是静态方法,所以要创建JdbcTemplate对象
b)JdbcTemplate构造方法:
JdbcTemplate(DataSource dataSource){
...
}创建对象
要创建JdbcTemplate对象,必须要含有实现DataSource接口的对象
DataSource用来获得连接(conn),dbcp连接池的核心类(BasicDataSource)实现该接口,也就是说可以注入一个BasicDataSource对象
dao实现类里定义JdbcTemplate,dao中注入一个DataSource,创建JdbcTemplate对象.(也可以继承spring提供的JdbcDaoSupport,里面提供了一个getJdbcTemplate()方法,可以获得JdbcTemplate对象,这样就不用在dao中显示书写setDataSource(DataSource dataSource)的方法来创建JdbcTemplate对象,不过在配置文件中,dao层还是需要配置dataSource属性值)
public class EmpDaoImpl implements IEmpDao { private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource){ jdbcTemplate = new JdbcTemplate(dataSource); } public void deleteEmp(int empno) { String sql = "delete from emp where empno = ?"; jdbcTemplate.update(sql,new Object[]{empno}); } //有空值(Null)的数据不能转换 public Emp findEmp(int empno) { String sql = "select * from emp where empno = ?"; return (Emp) jdbcTemplate.queryForObject(sql, new Object[]{empno}, new BeanPropertyRowMapper(Emp.class)); } public long findEmpCount() { String sql = "select count(empno) from emp"; return jdbcTemplate.queryForLong(sql); } public List<Emp> findEmps() { String sql = "select * from emp"; return jdbcTemplate.queryForList(sql); } public List<Map> findMgrs() { String sql = "select ename,empno,mgr from emp"; return jdbcTemplate.queryForList(sql); } public Map findSals() { String sql = "select max(sal),min(sal),avg(sal) from emp"; return jdbcTemplate.queryForMap(sql); } public void zhuanZhang(final int srcEmpNo, final int descEmpNo, final double balance) { jdbcTemplate.execute(new ConnectionCallback(){ public Object doInConnection(Connection conn) throws SQLException, DataAccessException { String sql = "update emp set sal = sal + ? where empno = ?" ; PreparedStatement ps = null; int row = 0; try { // conn.setAutoCommit(false); ps = conn.prepareStatement(sql); ps.setDouble(1, -balance); ps.setInt(2, srcEmpNo); row = ps.executeUpdate(); if(row != 1){ throw new SQLException("减钱异常"); } ps.setDouble(1, balance); ps.setInt(2, descEmpNo); row = ps.executeUpdate(); if(row != 1){ throw new SQLException("加钱失败"); } // conn.commit(); } catch (Exception e) { e.printStackTrace(); if(conn != null){ // conn.rollback(); } } return null; } }); } public void zhuangZhang2(int empNo, double balance) { String sql = "update emp set sal = sal + ? where empno = ?"; jdbcTemplate.update(sql, new Object[]{balance,empNo}); } }
spring配置文件 applicationContext.xml
对数据库所有的操作,通过连接池操作数据库,连接池(里面预先含有很多conn),每次对数据库操作都是在连接池获得连接conn使用jdbc操作数据库(dbcp连接池)dbcp核心类:BasicDataSource操作整个连接池BasicDataSource特点:
1) 实现了DataSource接口:获得conn连接
2) 对连接池具体操作
a : 连接数据库4个基本参数:url,driverClass,username,password
b : 连接池中的链接数量的参数:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!-- 指定数据库的基本信息 --> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/spring"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> <!-- 连接池中的conn数量的控制 --> <property name="maxActive" value="50"></property> <property name="minIdle" value="5"></property> <property name="initialSize" value="10"></property> <property name="maxWait" value="5000"></property> <property name="maxIdle" value="10"></property> </bean> <!-- spring针对事务的管理(jdbc、hibernate) --> <!-- 1 指定事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 2 配置事务的特性(事务传播特性和事务的隔离级别) --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="true"/> <tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"/> </tx:attributes> </tx:advice> <!-- 3 使用aop把事务配置到service层--> <aop:config> <aop:pointcut expression="execution(* com.tarena.service..*.*(..))" id="curd"/> <!-- 将事务的特性配置到切入点 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="curd"/> </aop:config> <bean id="empDao" class="com.tarena.dao.impl.EmpDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="empService" class="com.tarena.service.impl.EmpServiceImpl"> <property name="empDao" ref="empDao"></property> </bean>