spring jdbcTemplate
一、JdbcTemplate主要提供以下五类方法:
- execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
- update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;
- query方法及queryForXXX方法:用于执行查询相关语句;
- call方法:用于执行存储过程、函数相关语句。
JdbcTemplate类支持的回调类:
- 预编译语句及存储过程创建回调:用于根据JdbcTemplate提供的连接创建相应的语句;
PreparedStatementCreator:通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的PreparedStatement;
CallableStatementCreator:通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的CallableStatement;
- 预编译语句设值回调:用于给预编译语句相应参数设值;
PreparedStatementSetter:通过回调获取JdbcTemplate提供的PreparedStatement,由用户来对相应的预编译语句相应参数设值;
BatchPreparedStatementSetter:;类似于PreparedStatementSetter,但用于批处理,需要指定批处理大小;
- 自定义功能回调:提供给用户一个扩展点,用户可以在指定类型的扩展点执行任何数量需要的操作;
ConnectionCallback:通过回调获取JdbcTemplate提供的Connection,用户可在该Connection执行任何数量的操作;
StatementCallback:通过回调获取JdbcTemplate提供的Statement,用户可以在该Statement执行任何数量的操作;
PreparedStatementCallback:通过回调获取JdbcTemplate提供的PreparedStatement,用户可以在该PreparedStatement执行任何数量的操作;
CallableStatementCallback:通过回调获取JdbcTemplate提供的CallableStatement,用户可以在该CallableStatement执行任何数量的操作;
- 结果集处理回调:通过回调处理ResultSet或将ResultSet转换为需要的形式;
RowMapper:用于将结果集每行数据转换为需要的类型,用户需实现方法mapRow(ResultSet rs, int rowNum)来完成将每行数据转换为相应的类型。
RowCallbackHandler:用于处理ResultSet的每一行结果,用户需实现方法processRow(ResultSet rs)来完成处理,在该回调方法中无需执行rs.next(),该操作由JdbcTemplate来执行,用户只需按行获取数据然后处理即可。
ResultSetExtractor:用于结果集数据提取,用户需实现方法extractData(ResultSet rs)来处理结果集,用户必须处理整个结果集;
二、接下来,看个小Demo吧
(1)小Demo整体架构
(2)jar包
(3)建立数据库
create database spring
建立user表
create table user(id int primary key auto_increment, name varchar(16), age int);
(4)具体代码
model层
1 package com.xpw.model; 2 3 public class User { 4 private int id; 5 private String name; 6 private int age; 7 8 public User() { 9 10 } 11 12 public User(String name, int age) { 13 this.name = name; 14 this.age = age; 15 } 16 17 public User(int id, String name, int age) { 18 this.id = id; 19 this.name = name; 20 this.age = age; 21 } 22 23 public int getId() { 24 return id; 25 } 26 27 public void setId(int id) { 28 this.id = id; 29 } 30 31 public String getName() { 32 return name; 33 } 34 35 public void setName(String name) { 36 this.name = name; 37 } 38 39 public int getAge() { 40 return age; 41 } 42 43 public void setAge(int age) { 44 this.age = age; 45 } 46 47 }
dao接口层
1 package com.xpw.dao; 2 3 import java.util.List; 4 5 import com.xpw.model.User; 6 7 public interface UserDao { 8 public void add(User user); 9 10 public void update(User user); 11 12 public void delete(int id); 13 14 public List<User> queryAll(); 15 }
dao实现层
1 package com.xpw.dao.impl; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.springframework.jdbc.core.JdbcTemplate; 9 import org.springframework.jdbc.core.RowCallbackHandler; 10 11 import com.xpw.dao.UserDao; 12 import com.xpw.model.User; 13 14 public class UserDaoImpl implements UserDao { 15 16 private JdbcTemplate jdbcTemplate; 17 18 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 19 this.jdbcTemplate = jdbcTemplate; 20 } 21 22 @Override 23 public void add(User user) { 24 String sql = "insert into user(name, age) values(?,?)"; 25 Object[] args = { user.getName(), user.getAge() }; 26 int res = jdbcTemplate.update(sql, args); 27 if (res == 1) 28 System.out.println("add success"); 29 } 30 31 @Override 32 public void update(User user) { 33 String sql = "update user set name = ?, age = ? where id = ?"; 34 Object[] args = { user.getName(), user.getAge(), user.getId() }; 35 int res = jdbcTemplate.update(sql, args); 36 if (res == 1) 37 System.out.println("update success"); 38 } 39 40 @Override 41 public void delete(int id) { 42 String sql = "delete from user where id = ?"; 43 int res = jdbcTemplate.update(sql, id); 44 if (res == 1) 45 System.out.println("delete success"); 46 } 47 48 @Override 49 public List<User> queryAll() { 50 String sql = "select * from user"; 51 52 final List<User> list = new ArrayList<User>(); 53 54 jdbcTemplate.query(sql, new RowCallbackHandler() { 55 @Override 56 public void processRow(ResultSet rs) throws SQLException { 57 User u = new User(); 58 u.setId(rs.getInt("id")); 59 u.setName(rs.getString("name")); 60 u.setAge(rs.getInt("age")); 61 list.add(u); 62 } 63 }); 64 return list; 65 } 66 }
service实现层
1 package com.xpw.service; 2 3 import java.util.List; 4 5 import com.xpw.model.User; 6 7 public interface UserService { 8 public void add(User user); 9 10 public void update(User user); 11 12 public void delete(int id); 13 14 public List<User> queryAll(); 15 }
service实现层.
1 package com.xpw.service.impl; 2 3 import java.util.List; 4 5 import com.xpw.dao.UserDao; 6 import com.xpw.model.User; 7 import com.xpw.service.UserService; 8 9 public class UserServiceImpl implements UserService { 10 11 private UserDao userDao; 12 13 public void setUserDao(UserDao userDao) { 14 this.userDao = userDao; 15 } 16 17 @Override 18 public void add(User user) { 19 this.userDao.add(user); 20 } 21 22 @Override 23 public void update(User user) { 24 this.userDao.update(user); 25 } 26 27 @Override 28 public void delete(int id) { 29 this.userDao.delete(id); 30 } 31 32 @Override 33 public List<User> queryAll() { 34 return this.userDao.queryAll(); 35 } 36 }
(3)文件配置
beans.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <context:component-scan base-package="org.springframework.docs.test" /> 11 <context:property-placeholder location="jdbc.properties"/> 12 13 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 14 <property name="driverClassName" value="${jdbc.driverClassName}"/> 15 <property name="url" value="${jdbc.url}"/> 16 <property name="username" value="${jdbc.username}"/> 17 <property name="password" value="${jdbc.password}"/> 18 </bean> 19 20 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 21 <property name="dataSource" ref="dataSource"/> 22 </bean> 23 24 <bean id="userDao" class="com.xpw.dao.impl.UserDaoImpl"> 25 <property name="jdbcTemplate" ref="jdbcTemplate"/> 26 </bean> 27 28 <bean id="userService" class="com.xpw.service.impl.UserServiceImpl"> 29 <property name="userDao" ref="userDao"></property> 30 </bean> 31 </beans>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring jdbc.username=root jdbc.password=root
(4)测试
1 package com.xpw.test; 2 3 import java.util.List; 4 5 import org.junit.Test; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 import com.xpw.model.User; 10 import com.xpw.service.UserService; 11 12 public class JdbcTemplateTest { 13 14 private static ApplicationContext ac; 15 16 static { 17 ac = new ClassPathXmlApplicationContext("beans.xml"); 18 } 19 20 @Test 21 public void testAdd() { 22 UserService us = (UserService) ac.getBean("userService"); 23 us.add(new User("2b", 10)); 24 } 25 26 @Test 27 public void testUpdate() { 28 UserService us = (UserService) ac.getBean("userService"); 29 us.update(new User(1, "2b", 11)); 30 } 31 32 @Test 33 public void testDelete() { 34 UserService us = (UserService) ac.getBean("userService"); 35 us.delete(1); 36 } 37 38 @Test 39 public void testQuery() { 40 UserService us = (UserService) ac.getBean("userService"); 41 List<User> list = us.queryAll(); 42 for (User u : list) { 43 System.out 44 .println(u.getId() + " " + u.getName() + " " + u.getAge()); 45 } 46 } 47 }
结果就不直接给出来了。。
是不是,觉得很简单O(∩_∩)O哈哈~