使用步骤:
1)引入jar文件
spring-jdbc-3.2.5.RELEASE.jar
spring-tx-3.2.5.RELEASE.jar
2) 优化
package loaderman.h_jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import javax.sql.DataSource; public class UserDao02 { // IOC容器注入 private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void save() { try { String sql = "insert into t_dept(deptName) values('test');"; Connection con = null; Statement stmt = null; // 连接对象 con = dataSource.getConnection(); // 执行命令对象 stmt = con.createStatement(); // 执行 stmt.execute(sql); // 关闭 stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } }
package loaderman.h_jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class UserDao01 { /* * 保存方法 * 需求优化的地方: * 1. 连接管理 * 2. jdbc操作重复代码封装 */ public void save() { try { String sql = "insert into t_dept(deptName) values('test');"; Connection con = null; Statement stmt = null; Class.forName("com.mysql.jdbc.Driver"); // 连接对象 con = DriverManager.getConnection("jdbc:mysql:///hib_demo", "root", "root"); // 执行命令对象 stmt = con.createStatement(); // 执行 stmt.execute(sql); // 关闭 stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } }
package loaderman.h_jdbc; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; public class UserDao { // IOC容器注入 // private DataSource dataSource; // public void setDataSource(DataSource dataSource) { // this.dataSource = dataSource; // } private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void save() { String sql = "insert into t_dept(deptName) values('test');"; jdbcTemplate.update(sql); } public Dept findById(int id) { String sql = "select * from t_dept where deptId=?"; List<Dept> list = jdbcTemplate.query(sql,new MyResult(), id); return (list!=null && list.size()>0) ? list.get(0) : null; } public List<Dept> getAll() { String sql = "select * from t_dept"; List<Dept> list = jdbcTemplate.query(sql, new MyResult()); return list; } class MyResult implements RowMapper<Dept>{ // 如何封装一行记录 @Override public Dept mapRow(ResultSet rs, int index) throws SQLException { Dept dept = new Dept(); dept.setDeptId(rs.getInt("deptId")); dept.setDeptName(rs.getString("deptName")); return dept; } } }
package loaderman.h_jdbc; public class Dept { private int deptId; private String deptName; public int getDeptId() { return deptId; } public void setDeptId(int deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } }
package loaderman.h_jdbc; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { // 容器对象 ApplicationContext ac = new ClassPathXmlApplicationContext("loaderman/h_jdbc/bean.xml"); @Test public void testApp() throws Exception { UserDao ud = (UserDao) ac.getBean("userDao"); // ud.save(); System.out.println(ud.findById(9)); System.out.println(ud.getAll()); } }
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 1. 数据源对象: C3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="10"></property> <property name="maxStatements" value="100"></property> <property name="acquireIncrement" value="2"></property> </bean> <!-- 2. 创建JdbcTemplate对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- dao 实例 --> <bean id="userDao" class="loaderman.h_jdbc.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans>