说明:本文延续上一次xml配置代码进行半注解配置
半注解半xml在我看来效率较高
https://blog.csdn.net/Android_Cob/article/details/105167595
xml注解链接地址
正文
1.注解配置需要修改Spring中的xml配置文件
这里增加了jdbc的注入配置,因为去除了继承JdbcDaoSupport,需要手动注入
<tx:annotation-driven transaction-manager=“transactionManager”>
</tx:annotation-driven>这是添加对注解的事务支持
去除了xml中的手动注入事务
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描包-->
<context:component-scan base-package="com"/>
<!--配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--连接数据库的必备信息-->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/travel?useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="root."></property>
</bean>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- spring中基于注解的声明式事务控制配置步骤
1、配置事务管理器
2、开启spring对注解事务的支持
3、在需要事务支持的地方使用@Transactional注解
-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
2.修改持久层实现类
由于上次持久层实现类中继承了spring-jdbc框架JdbcDaoSupport类,继承后不能使用注解,故而修改去除继承关系
package com.dao.impl;
import com.dao.IAccountDao;
import com.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("accountDao")
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Account> findAllAccount() {
List<Account> query=null;
try {
query = jdbcTemplate.query("select * from account ",
new BeanPropertyRowMapper<Account>(Account.class));
} catch (Exception e) {
e.printStackTrace();
}
return query;
}
public Account findById(Integer id) {
Account query =null;
try {
query = jdbcTemplate.queryForObject("select * from account where id = ? ", new BeanPropertyRowMapper<Account>(Account.class), id);
} catch (Exception e) {
e.printStackTrace();
}
return query;
}
public void saveAccount(Account account) {
jdbcTemplate.update("insert into account (name,money) values(?,?) ", account.getName(),
account.getMoney());
}
public void updateAccount(Account account) {
try {
jdbcTemplate.update("update account set money=?,name=? where id =? ", account.getMoney(), account.getName(), account.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
public void delAccount(Integer id) {
jdbcTemplate.update("delete from account where id =? ",id);
}
public Account findByName(String name) {
Account query = null;
try {
query = jdbcTemplate.queryForObject("select * from account where name =?",
new BeanPropertyRowMapper<Account>(Account.class), name);
} catch (Exception e) {
e.printStackTrace();
}
return query;
}
}
3.修改事务层Service实现类
事务控制应该在事务层实现
注解的缺点在这里能明确体现,因为只有查不需要读写操作和事务支持
所以增删改操作都要重新注解@Transactional设置里面的属性
不像xml注解能用method name属性来进行通配操作
所以事务控制推荐写入Spring xml配置文件中
package com.service.impl;
import com.dao.IAccountDao;
import com.domain.Account;
import com.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS ,readOnly = true)
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao dao;
public List<Account> findAllAccount() {
return dao.findAllAccount();
}
public Account findById(Integer id) {
return dao.findById(id);
}
@Transactional(propagation = Propagation.REQUIRED ,readOnly = false)
public void saveAccount(Account account) {
dao.saveAccount(account);
}
@Transactional(propagation = Propagation.REQUIRED ,readOnly = false)
public void updateAccount(Account account) {
dao.updateAccount(account);
}
@Transactional(propagation = Propagation.REQUIRED ,readOnly = false)
public void delAccount(Integer id) {
dao.delAccount(id);
}
@Transactional(propagation = Propagation.REQUIRED ,readOnly = false)
public void transfer(String Name, String ToName, Integer money) {
Account oldAccount = dao.findByName(Name);
Account newAccount = dao.findByName(ToName);
oldAccount.setMoney(oldAccount.getMoney()-money);
newAccount.setMoney(newAccount.getMoney()+money);
updateAccount(oldAccount);
//int a =5/0;
updateAccount(newAccount);
}
}
4.项目结构
5.测试
import com.service.IAccountService;
import com.utils.SpringConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.lang.annotation.Annotation;
public class test {
/*转账测试*/
@Test
void test() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
IAccountService service = (IAccountService) ac.getBean("accountService");
service.transfer("aaa","bbb",1000);
/*结论如果service中出错便会回滚事务*/
}
}