环境
pom.xml 的依赖包
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-702.jdbc4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC03</version>
</dependency>
</dependencies>
资源文件夹 (src/main/resources) 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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- <context:property-placeholder location="classpath:jdbc.properties" /> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/test" />
<property name="username" value="postgres" />
<property name="password" value="postgres" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="3" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="5" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="3" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="3" />
<!-- 设置在自动回收超时连接的时候打印连接的超时错误 -->
<property name="logAbandoned" value="true"/>
<!-- 设置自动回收超时连接 -->
<property name="removeAbandoned" value="true"/>
<!-- 超时时间(以秒数为单位) -->
<property name="removeAbandonedTimeout" value="10"/>
<!-- 超时等待时间以毫秒为单位 -->
<property name="maxWait" value="100"/>
</bean>
<bean id="personDao" class="com.jdbc.PersonDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="personService" class="com.jdbc.PersonServiceImpl">
<property name="personDao" ref="personDao"></property>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>
类PersonDaoImpl的实现方法
public int addPerson(PersonBean person) {
int key = 0;
KeyHolder holder = new GeneratedKeyHolder();
try {
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
int i = 1;
PreparedStatement ps = con.prepareStatement(
"insert into person(name) values(?)",
new String[] { "id" } //id为Sequence所产生,即表所定义的字段名称
/* person 表在postgresql 的schema
CREATE SEQUENCE seq_id
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 14631
CACHE 1;
ALTER TABLE seq_id
OWNER TO postgres;
GRANT ALL ON TABLE seq_id TO postgres;
GRANT ALL ON TABLE seq_id TO public;
CREATE TABLE person
(
id integer NOT NULL DEFAULT nextval(('"seq_id"'::text)::regclass),
name character varying(1024) NOT NULL DEFAULT ''::character varying
)
WITH (
OIDS=FALSE
);
ALTER TABLE person
OWNER TO postgres;
*/ );
ps.setString(i++, "jack" + i);
return ps;
}
},
holder
);
key = holder.getKey().intValue();
}catch (DataAccessException e) {
e.printStackTrace() ;
}
return key;
}