使用注解实现:
连接mysql数据库对数据库的增删改查事务管理 1.环境准备 1.1 项目目录
1.2 pom.xml配置1.3 AppConfig.class配置4.0.0 com.itheima AOP_z_tx_pure_anno_011.0-SNAPSHOT 11 11 junit junit4.12 test org.springframework spring-context5.1.2.RELEASE mysql mysql-connector-java5.1.47 com.alibaba druid1.1.23 org.mybatis mybatis3.5.6 org.mybatis mybatis-spring2.0.6 org.springframework spring-jdbc5.1.2.RELEASE org.slf4j slf4j-api1.7.20 ch.qos.logback logback-classic1.2.3 ch.qos.logback logback-core1.2.3 org.springframework spring-test5.1.2.RELEASE org.aspectj aspectjweaver1.9.4
@EnableTransactionManagement
启注解事务管理,等同于xml配置方式的
使用注解 @EnableTransactionManagement 开启事务支持后,然后在访问数据库的Service方法上添加注解 @Transactional 便可。
关于事务管理器,不管是JPA还是JDBC等都实现自接口 PlatformTransactionManager 如果你添加的是 spring-boot-starter-jdbc 依赖,框架会默认注入 DataSourceTransactionManager 实例。如果你添加的是 spring-boot-starter-data-jpa 依赖,框架会默认注入 JpaTransactionManager 实例。
@MapperScan(“com.itheima.dao”)
作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
添加位置:是在Springboot启动类上面添加
@PropertySource(“classpath:db.properties”)
导入properties文件
@ComponentScan(“com.itheima”)
根据定义的扫描路径,把符合扫描规则的类装配到spring容器中
@Configuration
用于定义配置类,可替换xml配置文件
@Value
注入简单类型的变量。复杂类型使用@Autowired注入
package com.itheima.config; import com.alibaba.druid.pool.DruidDataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; @EnableTransactionManagement @MapperScan("com.itheima.dao") @PropertySource("classpath:db.properties") @ComponentScan("com.itheima") @Configuration public class AppConfig { @Value("${db.driverClass}") private String driverClass; @Value("${db.url}") private String url; @Value("${db.username}") private String username; @Value("${db.password}") private String password; //2.创建数据源bean对象 放入IOC容器管理 @Bean public DataSource dataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driverClass); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); return druidDataSource; } //3.加载数据源 创建SqlSessionFactory bean对象 交给Spring IOC容器管理 @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); //细节2:设置别名 sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.bean"); return sqlSessionFactoryBean; } //4.创建事务管理员对象 @Bean public PlatformTransactionManager transactionManager(DataSource dataSource){ DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } }1.4 业务调用概述
Service ➡️ Dao ➡️ SQL
2.bean Accountpackage com.itheima.bean; public class Account { private Integer id; private String name; private Integer money; public Account() { } public Account(Integer id, String name, Integer money) { this.id = id; this.name = name; this.money = money; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getMoney() { return money; } public void setMoney(Integer money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + ''' + ", money=" + money + '}'; } }3.dao AccountDao 接口
package com.itheima.dao; import com.itheima.bean.Account; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import java.util.List; public interface AccountDao { @Select("select * from tb_user") List findAll(); @Update("update tb_user set money = money - #{money} where name = #{name}") int kouqian(@Param("name") String name,@Param("money") int money); @Update("update tb_user set money = money + #{money} where name = #{name}") int jiaqian(@Param("name") String name,@Param("money") int money); }LogDao 接口
package com.itheima.dao; import org.apache.ibatis.annotations.Insert; public interface LogDao { @Insert("insert into tb_log(info) values(#{info})") int writeLog(String info); }4.service AccountService 接口
package com.itheima.service; import com.itheima.bean.Account; import java.util.List; public interface AccountService { List findAll(); void transfer(String from, String to, int money); }LogService 接口
package com.itheima.service; public interface LogService { void writeLog(String info); }AccountServiceImpl 实现类
在该类中,先实现AccountService接口中的方法。然后在重写的方法中调用Dao层的AccountDao接口中的方法。当IOC容器在使用Service层的方法时,最终要实现Dao层的实现类中的重写方法。
调用链路:IOC ➡️ serviceDao ➡️ serviceDaoImpl ➡️ Dao ➡️ DaoImpl
@Transactional
该注解表示开启事务管理
如果打在类上,则表示对类中所有的方法进行事务管理
如果打在类中某个方法上,则表示对某个方法进行事务管理
@Service
该注解表示将该类注入到IOC容器中
@Autowired
该注解表示自动注入依赖,依赖类无需使用@Component注解
package com.itheima.service.impl; import com.itheima.bean.Account; import com.itheima.dao.AccountDao; import com.itheima.dao.LogDao; import com.itheima.service.AccountService; import com.itheima.service.LogService; import org.aspectj.lang.annotation.Around; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Transactional @Service public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao; @Autowired private LogService logService; @Override public List findAll() { return accountDao.findAll(); } @Override public void transfer(String from, String to, int money) { try { accountDao.kouqian(from, money); //int a = 1/0; accountDao.jiaqian(to, money); } finally { String info = from + "->" + to + ": " + money + "-RMB"; logService.writeLog(info); } } }LogServiceImpl 实现类
@Transactional(propagation = Propagation.REQUIRES_NEW)
propagation = Propagation.REQUIRES_NEW:表示无论如何都开启新事务
package com.itheima.service.impl; import com.itheima.dao.LogDao; import com.itheima.service.LogService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Transactional(propagation = Propagation.REQUIRES_NEW) @Service public class LogServiceImpl implements LogService { @Autowired private LogDao logDao; @Override public void writeLog(String info) { logDao.writeLog(info); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)