Spring里面dbcp连接池的配置和使用
DBCP(DataBase connection pool),数据库连接池。是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要2个包:commons-dbcp.jar,commons-pool.jar由于建立数据库连接是一个非常耗时耗资源的行为,所以通过连接池预先同数据库建立一些连接,放在内存中,应用程序需要建立数据库连接时直接到连接池中申请一个就行,用完后再放回去。
-
相关jar包引入
下载地址:http://pan.baidu.com/s/1kV3DxIJ
-
数据库设计
/* Navicat MySQL Data Transfer Source Server : gg Source Host : localhost:3306 Source Database : spring_database Target Server Type : MYSQL File Encoding : 65001 Date: 2017-03-08 14:27:35 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for t_user -- ---------------------------- DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `_id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT NULL, `userpwd` varchar(32) DEFAULT NULL, PRIMARY KEY (`_id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_user -- ---------------------------- INSERT INTO `t_user` VALUES ('1', 'jake', '123456'); INSERT INTO `t_user` VALUES ('2', 'rose', '123456789'); INSERT INTO `t_user` VALUES ('3', 'tom', '999');
-
编码测试
创建一个web项目,将上面的jar导入lib文件下就是,然后依次创建user,创建UserDao,最后编码测试,src下创建配置文件bean.xml
- 创建user
package com.heima_jdbctemplate_dbcp.next; /** * 标准JavaBean * @author GGR * */ public class User { @Override public String toString() { // TODO Auto-generated method stub return super.toString(); } private Integer _id;//用户id private String username;//用户名 private String userpwd;//用户密码 public User(){} public User(String username,String userpwd){ this.username = username; this.setUserpwd(userpwd); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpwd() { return userpwd; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } public Integer get_id() { return _id; } public void set_id(Integer _id) { this._id = _id; } }
- 创建UserDao
package com.heima_jdbctemplate_dbcp; import java.util.List; import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import org.springframework.stereotype.Repository; /** * 用户操作dao * @author GGR * ParameterizedBeanPropertyRowMapper提供里数据库每行每个参数到bean对象每个属性的映射 */ @Repository("UserDao") public class UserDao extends JdbcDaoSupport{ public List<User> getAll(){ return this.getJdbcTemplate().query("select* from t_user",ParameterizedBeanPropertyRowMapper.newInstance(User.class)); } public User QuerybyId(Integer _id){ return this.getJdbcTemplate().queryForObject("select* from t_user where _id=?", ParameterizedBeanPropertyRowMapper.newInstance(User.class),_id); } }
- 配置文件bean.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"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/spring_database"></property> <property name="username" value="root"></property> <property name="password" value="362427gg"></property> </bean> <bean id="userdao" class="com.heima_jdbctemplate_dbcp.UserDao"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
- 编码测试
package com.heima_jdbctemplate_dbcp; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Testdbcp { @Test public void demo() { String xmlpath = "com/heima_jdbctemplate_dbcp/bean.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath); UserDao dao = applicationContext.getBean("userdao",UserDao.class); System.out.println(dao.getAll()); System.out.println(dao.QuerybyId(1)); } }
- 测试结果