数据库的连接池以c3p0为例
整体概述:
1.将c3p0的配置文件c3p0.properties放到项目的resources文件夹下
2.在spring容器(spring.xml)中将连接池加载进来
3.让spring托管连接池,即:将c3p0的配置文件中的值配置进来
4.声明springJdbc工具类,并注入连接池
5.声明Dao实现并注入工具类
6.spring容器负责对象实例的创建.
对于2,3,4的代码实现:
spring.xml:
<?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:context="http://www.springframework.org/schema/context"
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">
<!--加载数据库连接池的配置-->
<context:property-placeholder location="c3p0.properties"></context:property-placeholder>
<!--让Spring管理连接池:将配置文件中信息配置连接池中-->
<bean name="dataSources1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${c3p0.driverClass}"></property>
<property name="jdbcUrl" value="${c3p0.jdbcUrl}"></property>
<property name="user" value="${c3p0.user}"></property>
<property name="password" value="${c3p0.password}"></property>
</bean>
<!--声明springJdbc工具类-->
<bean name="JdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSources1"></property>
</bean>
<!--声明dao实现-->
<bean name="userDao1" class="Demo2.UserDaoImp">
<property name="jdbcTemplate" ref="JdbcTemplate1"></property>
</bean>
</beans>
数据库表为t_user
创建测试类对Dao实现进行测试:
package Demo2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.ResultSet;
import java.util.List;
/**
* @Author wu
* @date 2019/12/26
*/
public class SpringJdbcTest2 {
/**
* 测试添加方法
*/
@Test
public void addUserTest(){
//加载spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("spring2.xml");
//通过spring容器获得dao实现类对象,如果直接使用Dao实现类new对象来调用addUser方法,会导致空指针异常(要处理的话可以在addUser方法中手动创建jdbcTemplate对象来调用update方法,而非使用getJdbcTemplate方法 但是这样就没显示出spring的IOC特点,见文末图)
IuserDao iuserDao = ac.getBean("userDao1",IuserDao.class);
//创建实体类对象
Tuser tuser = new Tuser("ww","666");
//用对象调用方法
int result = iuserDao.addUser(tuser);
System.out.println("添加结果:"+result);
}
/**
* 测试修改方法
*/
@Test
public void updateUserTest(){
//加载spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("spring2.xml");
//通过spring容器获得dao实现类对象
UserDaoImp userDaoImp = ac.getBean("userDao1",UserDaoImp.class);
// IuserDao iuserDao = ac.getBean("userDao1",IuserDao.class);
Tuser tuser = new Tuser(1,"hh","456");
int result = userDaoImp.updateUser(tuser);
System.out.println("修改结果:"+result);
}
/**
* 测试删除的方法
*/
@Test
public void delUserTest(){
//加载spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("spring2.xml");
//通过spring容器获得dao实现类对象
IuserDao iuserDao = ac.getBean("userDao1",IuserDao.class);
int result = iuserDao.delUserById(1);
System.out.println("删除结果:"+result);
}
/**
* 测试查询总数据量的方法
*/
@Test
public void queryCountTest(){
//加载spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("spring2.xml");
//用spring容器创建dao实现类对象
IuserDao iuserDao = ac.getBean("userDao1",IuserDao.class);
int count = iuserDao.queryCount();
System.out.println("查询总量:"+count);
}
/**
* 测试查询一个对象
*/
@Test
public void aueryUserById(){
//加载spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("spring2.xml");
//用spring容器创建dao实现类对象
IuserDao iuserDao = ac.getBean("userDao1",IuserDao.class);
Tuser tuser = iuserDao.queryUserById(1);
System.out.println("查询一个对象:"+tuser);
}
/**
* 测试查询结果集
*/
@Test
public void queryAllUserTest(){
//加载spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("spring2.xml");
//用spring容器创建dao实现对象
IuserDao iuserDao = ac.getBean("userDao1",IuserDao.class);
List<Tuser> list = iuserDao.queryAll();
System.out.println("查询全部对象:");
list.stream().forEach((e)->System.out.println(e));
}
}
Dao实现代码:
package Demo2;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
*
* @Author
* @date 2019/12/26
*/
public class UserDaoImp extends JdbcDaoSupport implements IuserDao{
@Override
public int addUser(Tuser u) {
//准备sql语句
String sql1="insert into t_user(name,`PASSWORD`) values(?,?)";
//用springJdbc工具类调用方法执行sql语句并得到结果
int result1=getJdbcTemplate().update(sql1,u.getName(),u.getPassword());
return result;
}
@Override
public int updateUser(Tuser u) {
//准备sql语句
String sql="update t_user set name=?,`PASSWORD`=? where id=?";
//用springJdbc工具类调用方法执行sql语句并得到结果
int result=getJdbcTemplate().update(sql,u.getName(),u.getPassword(),u.getId());
return result;
}
@Override
public int delUserById(int id) {
//准备sql语句
String sql="delete from t_user where id=?";
//用springJdbc工具类调用方法执行sql语句并得到结果
int result=getJdbcTemplate().update(sql,id);
return result;
}
@Override
public int queryCount() {
//准备sql语句
String sql="select count(id) from t_user";
//用springJdbc工具类调用方法执行sql语句并得到结果
int count=getJdbcTemplate().queryForObject(sql,Integer.class);
return count;
}
@Override
public Tuser queryUserById(int id) {
//准备sql语句
String sql="select id,name,PASSWORD from t_user where id=?";
//用springJdbc工具类调用方法执行sql语句并得到结果
Tuser u1=getJdbcTemplate().queryForObject(sql, new Object[]{id}, new RowMapper<Tuser>() {
//resultSet结果集中每一行,这一行与实体的属性怎么接收
@Override
public Tuser mapRow(ResultSet resultSet, int i) throws SQLException {
Tuser user1=new Tuser();
user1.setId(resultSet.getInt("id"));
user1.setName(resultSet.getString("name"));
user1.setPassword(resultSet.getString("password"));
return user1;
}
});
return u1;
}
@Override
public List<Tuser> queryAll() {
//准备sql语句
String sql="select id,name,PASSWORD from t_user";
//用springJdbc工具类调用方法执行sql语句并得到结果
List<Tuser> uList=getJdbcTemplate().query(sql, new RowMapper<Tuser>() {
//resultSet结果集中每一行,这一行与实体的属性怎么接收
@Override
public Tuser mapRow(ResultSet resultSet, int i) throws SQLException {
Tuser user1=new Tuser();
user1.setId(resultSet.getInt("id"));
user1.setName(resultSet.getString("name"));
user1.setPassword(resultSet.getString("password"));
return user1;
}
});
return uList;
}
}
实体类:
package Demo2;
/**
* 实体类
* @Author
* @date 2019/12/26
*/
public class Tuser {
private int id;
private String name;
private String password;
public Tuser() {
}
public Tuser(String name, String password) {
this.name = name;
this.password = password;
}
public Tuser(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
@Override
public String toString() {
return "Tuser{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
总结:
增/删/改都是调用springJdbc工具类的update()方法
查询对象(实体类对象或者数据结果对象)是调用queryForObject()方法
查询结果集用query()方法.
查询实例对象时需要通过RowMapper来设置查询数据与实体类属性的映射关系.