一步一步深入spring(7)-- 整合spring和JDBC的环境

时间:2023-03-09 03:48:59
一步一步深入spring(7)-- 整合spring和JDBC的环境

1.配置数据源

(1).添加支持数据源的jar包commons-dbcp.jar 、commons-pool.jar
当然也要添加其他的spring用到的jar以及这里用到的数据库mysql的jar  mysql-connector-java-5.0.7-bin.jar
(2).在bean.xml中添加配置文件代码
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/yangyang"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
连接池启动时的初始值
<property name="initialSize" value="1"/>
连接池的最大值
<property name="maxActive" value="500"/>
最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止
<property name="maxIdle" value="2"/>
最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请
<property name="minIdle" value="1"/>
</bean>
2.配置事务
配置事务时,需要在命名空间中添加引用事务声明的tx命名空间,配置事务有两种方式,一种是基于注解,一种是基于xml方式
这里先来实现基于注解的方式。
 配置spring对事务的支持:
     <!-- spring的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 采用@Transactional使用注解方式处理事务 -->
<tx:annotation-driven transaction-manager="txManager"/>

3.编写业务bean,这里只给出实现部分的代码:

 package com.yangyang.service.impl;

 import java.util.List;

 import javax.sql.DataSource;

 import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional; import com.yangyang.model.Person;
import com.yangyang.service.PersonService; //受spring的事务管理
@Transactional
public class PersonServiceImpl implements PersonService{ private JdbcTemplate jdbcTemplate;//通过jdbcTemplate来操作数据源
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
} @Override
public void save(Person person) {
jdbcTemplate.update("insert into person(name) values(?)", new Object[]{person.getName()},
new int[]{java.sql.Types.VARCHAR});
} @Override
public void update(Person person) {
jdbcTemplate.update("update person set name=? where id=?",new Object[]{person.getName(),person.getId()},
new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});
} @Override
public void delete(Integer personId) {
jdbcTemplate.update("delete from person where id=?",new Object[]{personId},
new int[]{java.sql.Types.INTEGER});
} @Override
public Person getPerson(Integer personId) {
return (Person)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personId},new int[]{java.sql.Types.INTEGER}
,new PersonRowMapper());
} @Override
public List<Person> getPersons() {
return (List<Person>)jdbcTemplate.query("select * from person",new PersonRowMapper());
} }

对查询支持的RowMapper 类

 package com.yangyang.service.impl;

 import java.sql.ResultSet;
import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; import com.yangyang.model.Person; public class PersonRowMapper implements RowMapper{ @Override
public Object mapRow(ResultSet rs, int index) throws SQLException {
Person person=new Person();
person.setName(rs.getString("name"));
person.setId(rs.getInt("id"));
return person;
} }

当然我们也需要将业务bean配置到spring容器中,完整的配置文件如下:

 <?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"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/yangyang"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
连接池启动时的初始值
<property name="initialSize" value="1"/>
连接池的最大值
<property name="maxActive" value="500"/>
最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止
<property name="maxIdle" value="2"/>
最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请
<property name="minIdle" value="1"/>
</bean> <!-- spring的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 采用@Transactional使用注解方式处理事务 -->
<tx:annotation-driven transaction-manager="txManager"/> <bean id="personService" class="com.yangyang.service.impl.PersonServiceImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

接下来编写单元测试来测试我们的类,这里省略了,可以看到配置成功。

当然我们会想把jdbc连接的部分以properties的形式抽出来,这样就有

driverClassName=org.gjt.mm.mysql.Driver
url=jdbc:mysql://127.0.0.1:3306/yangyang
username=root
password=123456
initialSize=1
maxActive=500
maxIdle=2
minIdle=1

然后在bean中改为:

 <?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">
<!-- 加上aop的命名空间以及DTD验证 --> <!-- spring对外部文件的支持 -->
<context:property-placeholder location="classpath:resources/jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}"/>
</bean> <!-- spring的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 采用@Transactional使用注解方式处理事务 -->
<tx:annotation-driven transaction-manager="txManager"/> <bean id="personService" class="com.yangyang.service.impl.PersonServiceImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

执行单元测试发现也是ok的

下面再来利用xml配置的方式来实现事务,在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">
<!-- 加上aop的命名空间以及DTD验证 --> <!-- spring对外部文件的支持 -->
<context:property-placeholder location="classpath:resources/jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}"/>
</bean> <!-- spring的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 使用配置的形式使用注解 -->
<aop:config>
<aop:pointcut id="transactionPointcut" expression="exception(* com.yangyang.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--get开头的方法不添加事务 -->
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <bean id="personService" class="com.yangyang.service.impl.PersonServiceImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

这样,我们不需要在业务类中加入任何注解,通过配置也能实现事务鼓管理