1 Spring整合JDBC模版
1.1 spring中土拱了一个可以操作数据库的对象。对象封装了jdbc技术
JDBCTemplateJDBC模板对象
1.2 与DBUtils中的QueryRunner非常相似
1.3 准备工作
1.导包 4+2 基础包+日志包。 junit5+spring-test、spring-aop、c3p0连接池、JDBC驱动、spring-jdbc、spring-tx事务
JDBC演示
1.4 JDBC模版实现增删改查操作
准备接口UserDao
编写实现类 UserDaoImpl
增
删
改
查
主配置文件applicationContext.xml
测试
1.5 代码优化
1.6 spring整合JDBC --读取properties配置
准备properties文件
主要代码提供参考(下面是优化之前的代码)
UserDao接口
package com.legend.a_jdbctemplate;
import java.util.List;
import com.legend.bean.User;
public interface UserDao {
//增
void save(User u);
//删
void delete(Integer id);
//改
void update(User u);
//查
User getById(Integer id);//通过ID
int getTotalCount();//查找总记录数
List<User> getAll();//获取所有的记录
}
UserDaoImpl实现类
package com.legend.a_jdbctemplate;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.legend.bean.User;
/**
* 使用JDBC模版实现增删改查
* @author qichunlin
*
*/
public class UserDaoImpl implements UserDao {
//声明模版
private JdbcTemplate jt;
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
//增
@Override
public void save(User u) {
String sql = "insert into t_user values(null,?)";
jt.update(sql,u.getName());
System.out.println("执行完毕");
}
//删
@Override
public void delete(Integer id) {
// TODO Auto-generated method stub
String sql = "delete from t_user where id=?";
jt.update(sql,id);
System.out.println("执行完毕");
}
//改
@Override
public void update(User u) {
// TODO Auto-generated method stub
String sql = "update t_user set name=? where id=?";
jt.update(sql,u.getName(),u.getId());
System.out.println("执行完毕");
}
//通过id查找用户
@Override
public User getById(Integer id) {
// TODO Auto-generated method stub
String sql = "select * from t_user where id=?";
return jt.queryForObject(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
// 不需要判断是否有值
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
System.out.println("执行完毕");
return user;
}},id);
}
//获取所有的行数
@Override
public int getTotalCount() {
// TODO Auto-generated method stub
String sql = "select count(*) from t_user ";
//当返回的是值类型, 后面的Class对象就是
Integer count = jt.queryForObject(sql, Integer.class);
System.out.println("执行完毕");
return count;
}
//获取所有的对象记录
@Override
public List<User> getAll() {
String sql = "select * from t_user ";
List<User> list = jt.query(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
// 不需要判断是否有值
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}});
return list;
}
}
applicationContext.xml主配置文件
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 1.将连接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///crm"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 2.将JDBCTemplate放入spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3.将UserDao放入spring容器 -->
<bean name="userDao" class="com.legend.a_jdbctemplate.UserDaoImpl">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
</beans>
DemoTest测试类
package com.legend.a_jdbctemplate;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.legend.bean.User;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* SpringJDBC演示
* @author qichunlin
*
*/
//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class DemoTest2 {
//操作userDao对象
@Resource(name="userDao")
private UserDao ud;
@Test
public void fun2() throws Exception {
User u = new User();
u.setName("ATencent");
ud.save(u);
}
@Test
public void fun3() throws Exception {
User u = new User();
u.setId(10);
u.setName("hhhh");
ud.update(u);
}
@Test
public void fun4() throws Exception {
List<User> all = ud.getAll();
System.out.println(all.size());
}
@Test
public void fun5() throws Exception {
ud.delete(1);
}
@Test
public void fun6() throws Exception {
System.out.println(ud.getTotalCount());
}
//最原始的方法
@Test
public void fun1() throws Exception {
//准备连接池
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///crm");
dataSource.setUser("root");
dataSource.setPassword("123456");
//1.创建JDBC模版对象
JdbcTemplate jt = new JdbcTemplate();
jt.setDataSource(dataSource);
//2.书写sql,并执行
String sql = "insert into t_user values(3,\'legend\')";
jt.update(sql);
System.out.println("执行完毕");
}
}