1、JdbcTemplate操作数据库
url=jdbc\:mysql\://localhost\:3306/stanley?useUnicode\=true&characterEncoding\=UTF-8
username=root
password=123456
initialSize=1
maxActive=500
maxIdle=2
minIdle=1
< 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 ="${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 >
<!--Caused by: java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
< aop:config >
表示匹配所有方法
2)execution(public * com. savage.service.UserService.*(..))
表示匹配com.savage.server.UserService中所有的公有方法
3)execution(* com.savage.server..*.*(..))
表示匹配com.savage.server包及其子包下的所有方法
< aop:advisor advice-ref ="txAdvice" pointcut-ref ="transactionPointcut" />
</ aop:config >
< tx:attributes >
< tx:method name ="get*" read-only ="true" propagation ="NOT_SUPPORTED" />
< tx:method name ="*" />
</ tx:attributes >
</ tx:advice >
< bean id ="personService" class ="cn.comp.service.impl.PersonServiceBean" >
< property name ="dataSource" ref ="dataSource" />
</ bean >
</ beans >
private Integer id;
private String name;
public Person(){}
public Person(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public void save(Person person);
public void update(Person person);
public Person getPerson(Integer personid);
public List<Person> getPersons();
public void delete(Integer personid) throws Exception;
}
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void delete(Integer personid) throws Exception{
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});
}
}
public Object mapRow(ResultSet rs, int index) throws SQLException {
Person person = new Person(rs.getString( "name"));
person.setId(rs.getInt( "id"));
return person;
}
}
< property name ="dataSource" ref ="dataSource" />
</ bean >
< tx:annotation-driven transaction-manager ="txManager" />
< bean id ="personService" class ="cn.comp.service.impl.PersonServiceBean" >
< property name ="dataSource" ref ="dataSource" />
</ bean >
public class PersonServiceBean implements PersonService {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
// unchecked ,
// checked
@Transactional(noRollbackFor=RuntimeException. class)
public void delete(Integer personid) throws Exception{
jdbcTemplate.update( "delete from person where id=?", new Object[]{personid},
new int[]{java.sql.Types.INTEGER});
throw new RuntimeException( "运行期例外");
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
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());
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
@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});
}
后台抛出异常,查看数据库,记录插入进去了,说明我们配置事务不对RuntimeException回滚生效了.
既然可以配置不对RuntimeException回滚,那我们也可以配置对Exception进行回滚,主要用到的是
@Transactional(rollbackFor=Exception.class)
对于一些查询工作,因为不需要配置事务支持,我们配置事务的传播属性:
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
readOnly=true表示事务中不允许存在更新操作.
关于事务的传播属性有下面几种配置:
REQUIRED:业务方法需要在一个事务中运行,如果方法运行时,已经处于一个事务中,那么加入到该事务中,否则自己创建一个新的事务.(Spring默认的事务传播属性)
NOT_SUPPORTED:声明方法不需要事务,如果方法没有关联到一个事务,容器不会为它开启事务,如果方法在一个事务中被调用,该事务被挂起,在方法调用结束后,原先的事务便会恢复执行
REQUIRESNEW:不管是否存在事务,业务方法总会为自己发起一个新的事务,如果方法运行时已经存在一个事务,则该事务会被挂起,新的事务被创建,知道方法执行结束,新事务才结束,原先的事务才恢复执行.
MANDATORY:指定业务方法只能在一个已经存在的事务中执行,业务方法不能自己发起事务,如果业务方法没有在事务的环境下调用,则容器会抛出异常
SUPPORTS:如果业务方法在事务中被调用,则成为事务中的一部分,如果没有在事务中调用,则在没有事务的环境下执行
NEVER:指定业务方法绝对不能在事务范围内运行,否则会抛出异常.
NESTED:如果业务方法运行时已经存在一个事务,则新建一个嵌套的事务,该事务可以有多个回滚点,如果没有事务,则按REQUIRED属性执行. 注意:业务方法内部事务的回滚不会对外部事务造成影响,但是外部事务的回滚会影响内部事务
关于使用注解的方式来配置事务就到这里,
我们还可以使用另外一种方式实现事务的管理,通过xml文件的配置,主要通过AOP技术实现: