数据库的连接池以c3p0为例
整体概述:
1.将c3p0的配置文件c3p0.properties放到项目的resources文件夹下
2.在spring容器(spring.xml)中将连接池加载进来
3.让spring托管连接池,即:将c3p0的配置文件中的值配置进来
4.声明springJdbc工具类,并引入连接池
5.dao层使用即可.
对于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="dataSources" 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="dataSources"></property>
</bean>
</beans>
数据库表为t_user
创建测试类进行测试:
package Demo1;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.beans.PropertyVetoException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* SpringJDBC配置版单元测试
* @Author
* @date 2019/12/26
*/
public class SpringJdbcTest1 {
/**
* 添加用户
* @return
*/
@Test
public void addUserTest() throws PropertyVetoException {
//创建数据源
ComboPooledDataSource ds=new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/user");
ds.setUser("root");
ds.setPassword("123");
//创建springJdbc工具类
JdbcTemplate jdbcTemplate1 = new JdbcTemplate(ds);
//准备sql
String sql="insert into t_user(name,`PASSWORD`) values(?,?)";
//用springJdbc工具类调用方法执行sql语句并得到结果
int result = jdbcTemplate1.update(sql,"ee","123");
System.out.println("添加:"+result);
}
/**
* 修改用户
*/
@Test
public void updateUserTest() throws PropertyVetoException {
//创建数据源
ComboPooledDataSource ds=new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/user");
ds.setUser("root");
ds.setPassword("123");
//创建springJdbc工具类
JdbcTemplate jdbcTemplate1 = new JdbcTemplate(ds);
//准备sql
String sql="update t_user set name=?,`PASSWORD`=? where id=?";
//用springJdbc工具类调用方法执行sql语句并得到结果
int result=jdbcTemplate1.update(sql,"a1","321",1);
System.out.println("修改:"+result);
}
/**
* 删除用户
*/
@Test
public void delUserTest() throws PropertyVetoException {
//创建数据源
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/user");
ds.setUser("root");
ds.setPassword("123");
//创建springJdbc工具类对象
JdbcTemplate jdbcTemplate1 = new JdbcTemplate(ds);
//准备sql
String sql = "delete from t_user where id=?";
int result = jdbcTemplate1.update(sql,5);
System.out.println("删除:"+result);
}
/**
* 查询数据量
*/
@Test
public void queryCount() throws PropertyVetoException {
//创建数据源
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/user");
ds.setUser("root");
ds.setPassword("123");
//创建springJdbc工具类
JdbcTemplate jdbcTemplate1 = new JdbcTemplate(ds);
//准备sql
String sql = "select count(id) from t_user";
int count = jdbcTemplate1.queryForObject(sql,Integer.class);
System.out.println("总记录数:"+count);
}
/**
* 查询一个对象
*/
@Test
public void queryUserByIdTest() throws PropertyVetoException {
//创建数据源
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/user");
ds.setUser("root");
ds.setPassword("123");
//创建springJdbc工具类
JdbcTemplate jdbcTemplate1 = new JdbcTemplate(ds);
//准备sql
String sql="select id,name,PASSWORD from t_user where id=?";
//通过匿名内部类传入RowMapper对象,用来指定结果字段与Tuser属性的对应关系
Tuser u1 = jdbcTemplate1.queryForObject(sql, new Object[]{2}, new RowMapper<Tuser>() {
@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;
}
});
System.out.println("查到的对象为:"+u1);
}
/**
* 查询全部对象
*/
@Test
public void queryAllTest() throws PropertyVetoException {
//创建数据源
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/user");
ds.setUser("root");
ds.setPassword("123");
//创建springJdbc工具类
JdbcTemplate jdbcTemplate1 = new JdbcTemplate(ds);
//准备sql
String sql ="select id,name,PASSWORD from t_user";
List<Tuser> list = jdbcTemplate1.query(sql, new RowMapper<Tuser>() {
@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;
}
});
System.out.println("用EL表达式输出list");
list.stream().forEach((e)->System.out.println(e));
}
}
总结:
增/删/改都是调用springJdbc工具类的update()方法
查询对象(实体类对象或者数据结果对象)是调用queryForObject()方法
查询结果集用query()方法.
查询实例对象时需要通过RowMapper来设置查询数据与实体类属性的映射关系.
来源:CSDN
作者:米斯特尔.w
链接:https://blog.csdn.net/qq_34614766/article/details/103721597