(1)、引入相关依赖
1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>druid</artifactId> 4 <version>1.1.12</version> 5 </dependency> 6 7 <dependency> 8 <groupId>mysql</groupId> 9 <artifactId>mysql-connector-java</artifactId> 10 <version>8.0.13</version> 11 </dependency> 12 13 <dependency> 14 <groupId>org.springframework</groupId> 15 <artifactId>spring-orm</artifactId> 16 <version>4.3.22.RELEASE</version> 17 </dependency> 18 19 <dependency> 20 <groupId>org.mybatis</groupId> 21 <artifactId>mybatis-spring</artifactId> 22 <version>2.0.0</version> 23 </dependency> 24 <dependency> 25 <groupId>org.mybatis</groupId> 26 <artifactId>mybatis</artifactId> 27 <version>3.5.0</version> 28 </dependency>
(2)、编写Mybatis配置类
1 package cn.coreqi.config; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 import org.mybatis.spring.SqlSessionFactoryBean; 5 import org.mybatis.spring.annotation.MapperScan; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 9 import org.springframework.transaction.annotation.EnableTransactionManagement; 10 11 import javax.sql.DataSource; 12 13 @EnableTransactionManagement //启动事务管理器 14 @Configuration 15 @MapperScan(value = "cn.coreqi.mapper") //扫描Mapper文件 16 public class MybatisConfig { 17 @Bean 18 public DataSource dataSource(){ 19 DruidDataSource ds = new DruidDataSource(); 20 ds.setDriverClassName("com.mysql.jdbc.Driver"); 21 ds.setUrl("jdbc:mysql://localhost:3306/mybatisdemo?serverTimezone=UTC"); 22 ds.setUsername("root"); 23 ds.setPassword("123456"); 24 return ds; 25 } 26 27 //注册Mybatis SqlSessionFactory 28 @Bean 29 public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){ 30 SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean(); 31 sqlSessionFactory.setDataSource(dataSource); 32 return sqlSessionFactory; 33 } 34 35 //注册事务管理器 36 @Bean 37 public DataSourceTransactionManager transactionManager(DataSource dataSource){ 38 DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); 39 transactionManager.setDataSource(dataSource); 40 return transactionManager; 41 } 42 }
(3)、编写Mapper文件
1 package cn.coreqi.mapper; 2 3 import cn.coreqi.entities.User; 4 import org.apache.ibatis.annotations.*; 5 6 @Mapper 7 public interface UserMapper{ 8 @Select("select * from users where Id = #{id}") 9 public User getUserById(Integer id); 10 @Insert("insert into users(UserName,PassWord,Enabled) values(#{UserName},#{PassWord},#{Enabled})") 11 @Options(useGeneratedKeys = true,keyProperty = "Id") 12 public int addUser(User user); 13 @Update("update users set UserName = #{UserName},PassWord = #{PassWord},Enabled = #{Enabled} where Id = #{Id}") 14 public int modifyUser(User user); 15 @Delete("delete from users where Id = #{id}") 16 public void delById(Integer id); 17 }
(4)、编写Service文件
1 package cn.coreqi.service; 2 3 import cn.coreqi.entities.User; 4 import cn.coreqi.mapper.UserMapper; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 import org.springframework.transaction.annotation.Isolation; 8 import org.springframework.transaction.annotation.Propagation; 9 import org.springframework.transaction.annotation.Transactional; 10 11 import java.io.IOException; 12 import java.sql.SQLException; 13 14 @Service 15 public class UserService { 16 @Autowired 17 private UserMapper userMapper; 18 19 @Transactional(readOnly = true) 20 public User getUserById(Integer id){ 21 return userMapper.getUserById(id); 22 } 23 24 @Transactional 25 public User insertUser(User user){ 26 userMapper.addUser(user); 27 return user; 28 } 29 30 //@Transactional属性 31 // 1.propagation 指定当前事务的传播行为,共7种,常用的有REQUIRES_NEW和REQUIRED 32 // 2.isolation 指定当前事务的隔离级别,共5种,常用的有READ_COMMITTED,*事务的隔离级别要得到底层数据库的支持,而不是应用程序和框架的支持 33 // 3.rollbackFor&noRollbackFor 指定当前事务的回滚属性,rollbackFor指定遇到那些异常进行回滚,noRollbackFor指定遇到那些异常不进行回滚。*如果不指定这两个属性的默认情况下 34 // Spring的声明式事务对所有的运行时异常(RuntimeException)和Error类型的异常进行回滚。 35 // 4.readOnly 指定当前事务是否只读,true表示这个事务只读取数据而不对数据做修改,这样可以帮助数据库引擎优化事务 36 // 5.timeout 指定当前事务可以保持多久,防止长期运行事务占用资源,单位秒。 37 @Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.READ_COMMITTED, 38 rollbackFor = {IOException.class, SQLException.class},noRollbackFor = {ArithmeticException.class}, 39 readOnly = false,timeout = 30) 40 public User modifyUser(User user){ 41 userMapper.modifyUser(user); 42 return user; 43 } 44 45 @Transactional 46 public void delById(Integer id){ 47 userMapper.delById(id); 48 49 } 50 }