Spring使用JdbcTemplate的模板
第一种方式,dao中定义 JdbcTemplate, 通过配置;来注入
使用jdbcTemplate模板对象
public class UserDaoImpl implements IUserDao{
/**
* 注入JdbcTemplate对象
*/
private JdbcTemplate jt;
/**
* 增
*/
@Override
public void save(User user) {
String sql = "insert into t_user values(null,?)";
jt.update(sql, user.getT_name());
}
/**
* 删
*/
@Override
public void delete(int id) {
String sql = "delete from t_user where t_id = ?";
jt.update(sql, id);
}
/**
* 改
*/
@Override
public void update(User user) {
String sql = "update t_user set t_name = ? where t_id = ?";
jt.update(sql, user.getT_name() , user.getT_id());
}
/**
* 查询一个
* public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args)
* RowMapper: 里面书写的mapRow方法就是把ResultSet返回的结果集遍历封装到对象中
*/
@Override
public User find(int id) {
String sql = "select * from t_user where t_id = ?";
User user = jt.queryForObject(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int index) throws SQLException {
User u = new User();
u.setT_id(rs.getInt("t_id"));
u.setT_name(rs.getString("t_name"));
return u;
}
}, id);
//判断
if(user == null){
return null;
}
return user;
}
/**
* 使用聚合函数
* public <T> T queryForObject(String sql, Class<T> requiredType)
* requiredType : 直接返回值的字节码
*/
@Override
public int count() {
String sql= "select count(*) from t_user";
return jt.queryForObject(sql,Integer.class);
}
/**
* 查询所有
* public <T> List<T> query(String sql, RowMapper<T> rowMapper)
*/
@Override
public List<User> findAll() {
String sql = "select * from t_user";
List<User> query = jt.query(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setT_id(rs.getInt("t_id"));
u.setT_name(rs.getString("t_name"));
return u;
}
});
//判断
if(query.isEmpty()){
return null;
}
return query;
}
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" 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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">
<!-- 把UserDaoImpl对象放进容器中 -->
<bean id="userDaoImpl" class="com.dao.impl.UserDaoImpl">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
<!-- 把jdbcTemplate放进容器中 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 把连接池放进容器中 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/hibernate_crm"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
测试
//使用spring结合的junit
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo {
//注入对象
@Resource(name="userDaoImpl")
private IUserDao userDao;
/**
* 增
*/
@Test
public void save(){
User user = new User();
user.setT_name("Hello");
userDao.save(user);
}
/**
* 删
*/
@Test
public void delete(){
userDao.delete(3);
}
/**
* 改
*/
@Test
public void update(){
User user = new User();
user.setT_id(2);
user.setT_name("Hello");
userDao.update(user);
}
/**
* 查
*/
@Test
public void find(){
User find = userDao.find(2);
System.out.println(find);
}
/**
* 使用聚合查询多少个
*/
@Test
public void count(){
int count = userDao.count();
System.out.println(count);
}
/**
* 查询所有
*/
@Test
public void findAll(){
List<User> findAll = userDao.findAll();
System.out.println(findAll);
}
}