目录
说明
基于上次半注解的代码进行修改成全注解模式
https://blog.csdn.net/Android_Cob/article/details/105168439——半注解
https://blog.csdn.net/Android_Cob/article/details/105167595——xml注入
正文
1.pom.xml文件
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
2.我的项目结构
3. 写注解类
3.1注解父类
package com.utils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration /*声明配置类*/
@ComponentScan("com") /* 扫描包及其子包*/
@Import({
JdbcUtil.class,TransactionConfig.class}) /*导入其他配置文件*/
@PropertySource("jdbcConfig.properties") /*读取jdbc配置文件*/
@EnableTransactionManagement /*添加注解事务支持*/
public class SpringConfiguration {
}
3.2子类注解类——Jdbc注解
package com.utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
public class JdbcUtil {
/*使用EL*/
@Value("${driver}")
private String driverClassName;
@Value("${jdbcUrl}")
private String url;
@Value("${user}")
private String username;
@Value("${password}")
private String password;
/* 配置jdbcTemplate*/
@Bean(name = "jdbcTemplate")
public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "datasource")
public DataSource getDataSource() {
/*连接数据库的必备信息*/
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setPassword(password);
dataSource.setUsername(username);
return dataSource;
}
}
3.2子类注解——事务管理器
package com.utils;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
public class TransactionConfig {
/** * 用于创建事务管理器系统 * @param dataSource * @return */
@Bean(name = "transactionManager")
public PlatformTransactionManager getTransactionManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
4.其他类
与上次半注解一样
4.1持久层
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.stereotype.Repository;
import java.util.List;
@Repository("accountDao")
public class AccountDaoImpl 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;
}
}
4.2 事务层
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);
}
}
5.测试
5.1用转账测试事务控制
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中出错便会回滚事务*/
}
}
经测试可以受Spring事务控制