Spring入门之C3P0连接池

49 篇文章 5 订阅
37 篇文章 1 订阅

前言

  • 本文讲解Spring配置c3p0连接池操作
  • 本文通过新建项目的形式讲解,并非讲解c3p0连接池原理
  • 连接的是MySQL数据库

环境

  • Intellij IDEA 2017 CI

具体步骤

1、新建Spring项目

新建Spring项目,并创建相关类、路径及applicationContext.xml配置文件

这里写图片描述

UserDao类(只在测试时用,和配置连接池无关):

public class UserDao {

    //得到jdbcTemplate对象
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    //添加操作
    public void add() {
        //创建jdbcTemplate对象
//        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        String sql = "insert into student(NAME,password,sex,id,address) values(?,?,?,?,?)";
        jdbcTemplate.update(sql,"Lucy","123456","girl",3,"Shanghai");
    }
}

UserService类(只在测试时用,和配置连接池无关):

public class UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void add(){
        userDao.add();
    }
}

User类(只在测试时用,和配置连接池无关):

public class User {
    private String username;
    private String password;
    private String sex;
    private String address;
    private int id;

    public User(){}

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public String getSex() {
        return sex;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String toString(){
        return "[ uername:"+username+" password:"+password+" sex:"+sex
                +" address:"+address+" id:"+id+"]";
    }

}

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--创建service对象和dao对象,在service中注入dao对象-->
    <bean class="spring.c3p0.UserService" id="service">
        <!--注入dao对象-->
        <property name="userDao" ref="dao"></property>
    </bean>
    <bean class="spring.c3p0.UserDao" id="dao">
        <!--注入jdbcTemplate对象-->
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>

    </bean>

</beans>

数据库表:
这里写图片描述

如若以上类及文件内容不清楚的小伙伴,可以通过以下文章了解

也可以通过关注我的博客专栏,查看相关文章学习了解
Java Engineer

2、JDBC实现代码(可跳过此步骤)

在配置连接c3p0连接池之前,我们先来回温下JDBC的实现

//jdbc实现代码
    @Test
    public void testJdbc(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //加载驱动
        try{
            Class.forName("com.mysql.jdbc.Driver");
            //创建连接
            connection = DriverManager.getConnection("jdbc:mysql:///test","root","root");
            //编写SQL语句
            String sql = "select * from student where name=?";
            //预编译SQL
            preparedStatement = connection.prepareStatement(sql);
            //设置参数值
            preparedStatement.setString(1,"Bob");
            resultSet = preparedStatement.executeQuery();
            //遍历结果集
            while (resultSet.next()){
                //得到返回结果值
                String username = resultSet.getString("name");
                String password = resultSet.getString("sex");
                String address = resultSet.getString("address");
                String sex = resultSet.getString("sex");
                int id = resultSet.getInt("id");
                System.out.println(username);   //测试
                System.out.println(password);
                System.out.println(sex);
                System.out.println(address);
                System.out.println(id);

            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

3、c3p0连接池信息,原始代码操作(可跳过此步骤)

在Spring对c3p0封装了后,我们使用起来那叫一个爽,今天我们在讨论c3p0的连接过程之前,还是来看看最初的c3p0配置吧

    //C3P0连接池信息(最原始的操作代码)
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
    comboPooledDataSource.setJdbcUrl("jdbc:mysql:///test");
    comboPooledDataSource.setUser("root");
    comboPooledDataSource.setPassword("root");

4、配置c3p0连接池

<!--配置C3P0连接池-->
    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--注入属性值-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

5、创建jdbcTemplate对象

<!--创建jdbcTemplate对象-->
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <!--把dataSource传递到jdbcTemplate对象中-->
        <property name="dataSource" ref="comboPooledDataSource"></property>
    </bean>

这里写图片描述

到这里我们就已经配置成功了,

6、测试

这里写图片描述

数据库表数据:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值