1、spring配置及讲解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
default-lazy-init="true">
<!-- 将com.spring.data包内的bean注入 -->
<context:component-scan base-package="com.spring.data"></context:component-scan>
<!-- 引入外部资源文件 -->
<context:property-placeholder location="classpath:application.properties"/>
<!-- 启用pring对jdbc的封装 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- 数据驱动 -->
<property name="driverClass">
<value>${jdbc.driver}</value>
</property>
<!-- 数据库连接 -->
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<!-- 数据库用户名 -->
<property name="user">
<value>${jdbc.username}</value>
</property>
<!-- 数据库密码 -->
<property name="password">
<value>${jdbc.password}</value>
</property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
<!--连接池中保留的最小连接数-->
<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
<!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出
SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->
<property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/>
<!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
<property name="maxStatements" value="${c3p0.maxStatements}"/>
</bean>
</beans>
需要注意的是如果checkoutTimeout配置的时间过短会报下面的错误
我的默认配置
#MySql database setting
jdbc.driver=com.mysql.jdbc.Driver
#localtest
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.username=root
jdbc.password=root
#c3p0 settings
c3p0.maxPoolSize=15
c3p0.minPoolSize=10
c3p0.initialPoolSize=3
c3p0.maxIdleTime=60
c3p0.checkoutTimeout=500
c3p0.acquireIncrement=3
c3p0.idleConnectionTestPeriod=60
c3p0.maxStatements=100
2、对应的操作bean
package com.spring.data;
public class User {
private int id;
private String name;
private String address;
private int age;
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]";
}
}
3、持久成操作数据库完成数据的增删改查
package com.spring.data;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class JdbcSpitterDao {
@Resource
private JdbcTemplate jdbcTemplate;
//插入数据的sql
private static final String ADD_SQL="insert into tbl_c3p0(name,address,age) values(?,?,?)";
//根据id获取user对象
private static final String QUERY_SQL="select id,name,address,age from tbl_c3p0 where id=?";
//修改user对象
private static final String UPDATE_SQL="update tbl_c3p0 set name=?,address=?,age=? where id=?";
//根据id删除user对象
private static final String DELETE_SQL="delete from tbl_c3p0 where id=?";
/**
* 插入数据
*/
public void add(User user){
jdbcTemplate.update(ADD_SQL,user.getName(),user.getAddress(),user.getAge());
}
/**
* 获取User对象
*/
public User query(int id){
return jdbcTemplate.queryForObject(QUERY_SQL, new ParameterizedRowMapper<User>() {
public User mapRow(ResultSet rs,int rowNum) throws SQLException{
User user = new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setAddress(rs.getString(3));
user.setAge(rs.getInt(4));
return user;
}
},id);
}
/**
* 根据id修改user对象
*/
public void update(User user){
jdbcTemplate.update(UPDATE_SQL, user.getName(),user.getAddress(),user.getAge(),user.getId());
}
/**
* 根据id删除user对象
*/
public void delete(int id){
jdbcTemplate.update(DELETE_SQL, id);
}
}
4、整个过程测试入口
public class Test {
public static void main(String[] args) {
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load("com/spring/data/applicationContext.xml");
context.refresh();
JdbcSpitterDao dao = (JdbcSpitterDao)context.getBean("jdbcSpitterDao");
/*User user = new User();
user.setId(1);
user.setName("fanjunkai");
user.setAddress("hennapuyang");
user.setAge(18);
dao.add(user);*/
/*User u1 = dao.query(2);
System.out.println(u1.toString());
User u2 = new User();
u2.setAge(10);
u2.setName("xiayali");
u2.setAddress("zhumadian");
u2.setId(2);
dao.update(u2);*/
dao.delete(1);
}
}