01. 基础准备
mysql连接库: mysql-connector-java-8.0.23.jar
测试单元库: junit-4.5.jar
02. Druid的准备
>> 导入Jar包: druid-1.1.22.jar
>> 配置 druid.properties :
03. JDBCTemplate 准备
>> 导入 Jar 包:
spring-beans-5.3.6.jar
spring-core-5.3.6.jar
spring-jdbc-5.3.6.jar
spring-tx-5.3.6.jar
commons-logging-1.2.jar
04. Druid连接池准备
>> 公用单元的配置
1 public class JDBCUtils { 2 private static DataSource ds; 3 static { 4 try { 5 Properties pro = new Properties(); 6 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); 7 pro.load(is); 8 ds = DruidDataSourceFactory.createDataSource(pro); 9 } catch (IOException e) { 10 e.printStackTrace(); 11 } catch (Exception e) { 12 e.printStackTrace(); 13 } 14 } 15 16 //返回连接池对象 17 public static DataSource getDataSource(){ 18 return ds; 19 } 20 21 //返回连接对象 22 public static Connection getConnection() throws SQLException { 23 return ds.getConnection(); 24 } 25 26 //资源释放 27 public static void Close(Statement stmt,Connection con){ 28 if(stmt!=null){ 29 try { 30 stmt.close(); 31 } catch (SQLException e) { 32 e.printStackTrace(); 33 } 34 } 35 if(con!=null){ 36 try { 37 con.close(); 38 } catch (SQLException e) { 39 e.printStackTrace(); 40 } 41 } 42 } 43 public static void Close(ResultSet rs, Statement stmt, Connection con){ 44 if(rs!=null){ 45 try { 46 rs.close(); 47 } catch (SQLException e) { 48 e.printStackTrace(); 49 } 50 } 51 Close(stmt,con); 52 } 53 }
05. DML 演示
1 @Test 2 public void Test01() throws SQLException { 3 //取Template 4 JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); 5 //配置 SQL 6 String sql="Insert into user(username,birthday,sex,address) Values (?,?,?,?)"; 7 8 //执行 9 Date date = new Date(); 10 int i = template.update(sql, "张三丰", date, "男", "湖北"); 11 assert i==1; 12 } 13 14 @Test 15 public void Test02(){ 16 JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); 17 String sql = "update user set address=? where id=?"; 18 int i = template.update(sql, "湖南", 46); 19 System.out.println(i); 20 } 21 22 @Test 23 public void Test03(){ 24 JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); 25 String sql = "Delete from user where id=?"; 26 int i = template.update(sql, 46); 27 System.out.println(i); 28 }
06. DQL 演示
1 @Test 2 public void Test04(){ 3 JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); 4 String sql = "select * from user where id=?"; 5 Map<String, Object> map = template.queryForMap(sql, 45); 6 System.out.println(map); 7 } 8 9 @Test 10 public void Test05(){ 11 JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); 12 String sql = "select * from user where id<?"; 13 List<Map<String, Object>> maps = template.queryForList(sql, 0); 14 System.out.println(maps); 15 } 16 17 @Test 18 public void Test06(){ 19 JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); 20 String sql = "select * from user "; 21 List<User> list = template.query(sql, new BeanPropertyRowMapper<User>(User.class)); 22 for (User user : list) { 23 System.out.println(user); 24 } 25 }