@Repository
public class MemberDao {
private JdbcTemplate template;
@Resource(name="dataSource")
public void setDataSource(DataSource dataSource){
template = new JdbcTemplate(dataSource);
}
public List<Member> select(){
return template.query("select * from t_member", new RowMapper(){
@Override
public Member mapRow(ResultSet rs, int rowNum) throws SQLException {
Member m = new Member();
m.setId(rs.getLong("id"));
m.setName(rs.getString("name"));
return m;
}
});
}
public int insert(String name) throws Exception{
return template.update("insert into t_member(name) values(?)",name);
}
public int delete(long id) throws Exception{
return template.update("delete from t_member where id = ?",id);
}
public int update(long id,String name) throws Exception{
return template.update("update t_member set name = ? where id = ?",name,id);
}
}
<!--
1、数据源:不管是哪个厂商都要是实现DataSource接口,拿到实际上就是包含了Connection对象
2、使用Spring给我们提供的工具类TransactionMagager 事务管理器,来管理所有的 事务操作(肯定要拿到连接对象)
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 3、利用切面编程来实现对某一类方法进行事务统一管理(声明式事务) -->
<!-- 属于AOP中的东西,比较熟悉了的 -->
<!-- <aop:config>
<aop:pointcut expression="execution(public * com.gupaoedu.vip..*.service..*Service.*(..))" id="transactionPointcut"/>
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/>
</aop:config> -->
<!-- 4、配置通知规则 -->
<!-- Transaction tx :NameSpace -->
<!-- <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception,RuntimeException" timeout="-1"/>
<tx:method name="remove*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
<tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
<tx:method name="transfer*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="login" propagation="REQUIRED"/>
<tx:method name="query*" read-only="true"/>
</tx:attributes>
</tx:advice> -->