@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> -->