publicclassAccountDaoImpextendsJdbcDaoSupportimplementsAccountDao{
@Overridepublic Account findAccountByName(String name){
try{
List<Account> accounts =getJdbcTemplate().query("select * from springaccount where name=?",newBeanPropertyRowMapper<Account>(Account.class),name);if(accounts == null || accounts.size()==0){
return null;}if(accounts.size()>1){
thrownewRuntimeException("结果不唯一,数据有问题");}return accounts.get(0);}catch(Exception e){
thrownewRuntimeException(e);}}@OverridepublicvoidupdateAccount(Account account){
try{
getJdbcTemplate().update("update springaccount set name=? ,account=? where id=?",account.getName(),account.getAccount(),account.getId());}catch(Exception e){
thrownewRuntimeException(e);}}@Overridepublic Account findAccountById(int id){
try{
List<Account> accounts =getJdbcTemplate().query("select * from springaccount where id=?",newBeanPropertyRowMapper<Account>(Account.class),id);return accounts.isEmpty()? null : accounts.get(0);}catch(Exception e){
thrownewRuntimeException(e);}}}
bean.xml
Configure the configuration steps of xml-based declaration transaction control in spring
Configure transaction manager
Notification of configuration transactions
Use tx:advice tag table to configure transaction notification
id attribute: provide a unique identifier for the transaction notification
transaction-manager attribute: provide a transaction management reference for transaction notification
Isolation: used to specify the isolation level of the transaction, the default value is DEFAULT, which means to use the default isolation level of the database.
Propagation: Used to specify the propagation behavior of the transaction, the default value is REQUIRED, which means there will be transactions, and the choice of addition, deletion and modification. The query method can choose SUPPORTS.
read-only: Used to specify whether the transaction is read-only. Only the query method can set true. The default value is false, which means read and write.
timeout: used to specify the timeout period of the transaction, the default value is -1, which means never timeout, if the number is specified
rollback-for: Used to specify an exception. When the exception occurs, the transaction rolls back, and when other exceptions occur, the transaction does not roll back. There is no default value, which means that any exceptions are rolled back.
no-rollback-for: Used to specify an exception. When an exception occurs, the transaction does not roll back, and when other exceptions occur, the transaction rolls back. There is no default value. Indicates that any exceptions are rolled back.
Configure general entry point expressions in AOP
Establish the relationship between transaction notification and pointcut expression
Configure the properties of the transaction
Is inside the transaction notification tx:advice tag