使用Spring+JDBC集成步骤如下:第一要配置数据源,第二要配置事务
配置数据源,如:
.....略
配置事务。配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间(见下页),事务的配置方式有两种:注解方式和基于XML配置方式(比较麻烦)。
数据源:
- <context:property-placeholder location=“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>
dataSource 配置成一个单例的bead 还有一个close方法 &是转义字符,如果用配置文件property.properies,在eclipse会自动转义的
使用属性占位符,就可以数据源的配置信息写在.properties文件里,自己喜欢
- driverClassName=org.gjt.mm.mysql.Driver
- url=jdbc\:mysql\://localhost\:3306/itcast?useUnicode\=true&characterEncoding\=UTF-8
- username=root
- password=king
- initialSize=1
- maxActive=500
- maxIdle=2
- minIdle=1
配置事务:
采用注解方式(要求注入一个数据源,我们把上面的注入就OK,dataSource)
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
定义配置项,注解本身是干不了事的,是因为有一个处理器,对这个注解进行处理,专门对这个注解进行解析,开启对事务注解的支持。如果是所有bean都要用到注解,还要在开头加上配置,
这里只是支持事务注解
- <!– 采用@Transactional注解方式使用事务 -->
- <tx:annotation-driven transaction-manager="txManager"/>
beans.xml的配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns=""
- xmlns:xsi=""
- xmlns:context=""
- xmlns:aop=""
- xmlns:tx=""
- xsi:schemaLocation="
- /spring-beans-2.5.xsd
- /spring-context-2.5.xsd
- /spring-aop-2.5.xsd
- /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="${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>
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <tx:annotation-driven transaction-manager="txManager"/>
-
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- </beans>
配置事务的bean
@Transactional在类名前标注,开启事务管理,则所有方法都纳入spring管理
- package cn.itcast.service.impl;
- import java.util.List;
- import javax.sql.DataSource;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.transaction.annotation.Transactional;
- import cn.itcast.bean.Person;
- import cn.itcast.service.PersonService;
- @Transactional
- public class PersonServiceBean implements PersonService {
- private JdbcTemplate jdbcTemplate;
-
- public void setDataSource(DataSource dataSource) {
- this.jdbcTemplate = new JdbcTemplate(dataSource);
- }
- public void delete(Integer personid) {
- jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
- new int[]{java.sql.Types.INTEGER});
- }
- 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());
- }
- @SuppressWarnings("unchecked")
- public List<Person> getPersons() {
- return (List<Person>)jdbcTemplate.query("select * from person", new PersonRowMapper());
- }
- public void save(Person person) {
- jdbcTemplate.update("insert into person(name) values(?)", new Object[]{person.getName()},
- new int[]{java.sql.Types.VARCHAR});
- }
- 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});
- }
- }
测试
- package junit.test;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import cn.itcast.bean.Person;
- import cn.itcast.service.PersonService;
- public class PersonServiceTest {
- private static PersonService personService;
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- try {
- ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
- personService = (PersonService) cxt.getBean("personService");
- } catch (RuntimeException e) {
- e.printStackTrace();
- }
- }
- @Test public void save(){
- for(int i=0; i<5; i++)
- personService.save(new Person("传智播客"+ i));
- }
-
- @Test public void getPerson(){
- Person person = personService.getPerson(1);
- System.out.println(person.getName());
- }
-
- @Test public void update(){
- Person person = personService.getPerson(1);
- person.setName("张xx");
- personService.update(person);
- }
-
- @Test public void delete(){
- personService.delete(1);
- }
-
- @Test public void getBeans(){
- for(Person person : personService.getPersons()){
- System.out.println(person.getName());
- }
- }
- }
阅读(539) | 评论(1) | 转发(0) |