Java框架spring 学习笔记(十六):c3p0连接池的配置以及dao使用jdbcTemplate

连接池可以大大提高数据库的性能和连接速度,将那些已连接的数据库连接存放在一个连接池里,以后别人要连接数据库的时候,将不会重新建立数据库连接,直接从连接池中取出可用的连接,用户使用完毕后,会释放连接重新放到连接池中。

下载必要jar包mchange-commons-java-0.2.14.jar和c3p0-0.9.2.1.jar

快捷键ctrl+alt+shift+s导入jar包

Java框架spring 学习笔记(十六):c3p0连接池的配置以及dao使用jdbcTemplate

编写UserDao.java

 package com.jdbc.c3p0;
import org.springframework.jdbc.core.JdbcTemplate; public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
} public void add(){
//创建jdbcTemplate对象
//JdbcTemplate jdbcTemplate = new JdbcTemplate();
String sql = "insert into user values(?,?)";
jdbcTemplate.update(sql,"Lilei","bbbbbb");
}
}

编写UserService.java

 package com.jdbc.c3p0;

 public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
} public void add(){
userDao.add();
}
}

编写配置文件bean.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入dao对象 -->
<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="xxxxx"></property>
</bean> <bean id="userService" class="com.jdbc.c3p0.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.jdbc.c3p0.UserDao">
<!-- 注入jdbcTemplate对象-->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!-- 创建jdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 把dataSource传递到模板对象中-->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

编写测试文件TestService.java

 package com.jdbc.c3p0;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestService {
@Test
public void testDemo(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}

运行可见添加一条记录

Java框架spring 学习笔记(十六):c3p0连接池的配置以及dao使用jdbcTemplate

上一篇:Python爬取豆瓣电影top


下一篇:JSP 分页显示数据 (Oracle)