标签:des style blog class c code
一. 配置数据源
<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://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
<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>
使用<context:property-placeholderlocation=“jdbc.properties”/>属性占位符
<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>
jdbc.properties
driverClassName=org.gjt.mm.mysql.Driver url=jdbc\:mysql\://localhost\:3306/itcast?useUnicode\=true&characterEncoding\=UTF-8 username=root password=123456 initialSize=1 maxActive=500 maxIdle=2 minIdle=1
二. 配置事务
[ 注解配置 ]
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!– 采用@Transactional注解方式使用事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
@Service @Transactional
public class PersonServiceBean implements PersonService {
}
[ XML配置 ]
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.zdp.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/> <!-- 查询不用设置事务--> <tx:method name="*"/> </tx:attributes> </tx:advice>
三. 事务传播属性
四. 使用JdbcTemplate
1. 一个实际的例子:
public class PersonServiceImpl implements PersonService {
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() {
public Object mapRow(ResultSet rs, int index) throws SQLException {
Person person = new Person(rs.getString("name"));
person.setId(rs.getInt("id"));
return person;
}
});
}
// 查询多条用户记录
@SuppressWarnings("unchecked")
public List<Person> getPersons() {
return (List<Person>) jdbcTemplate.query("select * from person",
new PersonRowMapper() {
public Object mapRow(ResultSet rs, int index) throws SQLException {
Person person = new Person(rs.getString("name"));
person.setId(rs.getInt("id"));
return person;
}
});
}
// 插入一条用户记录
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 });
}
}深入探索spring技术内幕(八): Spring +JDBC组合开发和事务控制,布布扣,bubuko.com
深入探索spring技术内幕(八): Spring +JDBC组合开发和事务控制
标签:des style blog class c code
原文地址:http://blog.csdn.net/zdp072/article/details/26167425