spring boot mybaties整合 (spring boot mybaties 整合 基于Java注解方式写sql,无需任何得mapper xml文件)我们对于spring boot mybaties 整合有了一个基础的认知。这里主要正对上面得两篇文章中spring boot mybaties整合讲解得一个扩展学习,事物的配置,整合到spring 的事物控制中。spring boot mybatis 整合_spring boot与mybaties的使用 (spring boot mybaties 整合 xml mapper方式,也是实际应用最多得方式)
#==================DataSource Config Start==================
#默认采用Tomcat-jdbc-pool性能和并发最好,注意查看maven依赖中是否有tomcat-jdbc
#name
#spring.datasource.name=test
#url
#spring.datasource.url=jdbc:sqlserver://192.168.xxx.xxx;instanceName=sql_03;DatabaseName=edu;integratedSecurity=false
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
#DriverClass
#spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.tomcat.driver-class-name=com.mysql.jdbc.Driver
#DB username
spring.datasource.tomcat.username=root
#DB password
spring.datasource.tomcat.password=root
#最大连接数量
spring.datasource.tomcat.max-active=150
#最大闲置连接数量
spring.datasource.tomcat.max-idle=20
#最大等待时间
#spring.datasource.tomcat.max-wait=5000
#==================DataSource Config End==================
#==================mybaties Config Start==================
#ORM Bean Package
mybatis.type-aliases-package=com.example.pojo
mybatis.mapper-locations=classpath:/mapper/*.xml
#打印mybatiesSql语句
logging.level.com.example.mapper=DEBUG
#==================mybaties Config End ==================
#模板引擎配置缓存为FALSE。开发调试用
spring.thymeleaf.cache=false
这里注意关注数据连接配置和mybaties的xml mapper文件配置。package com.example.config;
import javax.sql.DataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* mybaties配置扫描mapper路径
*
* @author leftso
*
*/
@Configuration
@MapperScan(basePackages = { "com.example.mapper" }) /** 注意,这个注解是扫描mapper接口不是xml文件,使用xml模式必须在配置文件中添加xml的配置 **/
@EnableTransactionManagement /**
* 启用事物管理 ,在需要事物管理的service类或者方法上使用注解@Transactional
**/
public class MyBatiesConfig {
@Autowired
private DataSource dataSource;
/**
* 配合注解完成事物管理
*
* @return
*/
@Bean
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
注意必须把当前的数据源配置进入spring的注解事物管理器。否则通过spring框架的注解标签@Transactional是不会有事物作用的。
提示:
spring boot 2.1.4.RELEASE 版本无需配置PlatformTransactionManager 也能起作用,也就说仅需要一个注解@EnableTransactionManagement
Spring boot 2.x (Spring 5.0为基础的情况)无需使用@EnableTransactionManagement注解,spring boot 项目内部已经启用
package com.example.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TransactionalTest {
@Autowired
private UserMapper userMapper;
@Test
public void name() {
User user=new User("leftso", "男", 1);
userMapper.insert(user);
int t=1/0;
System.out.println(t);
}
}
执行前查询数据库:package com.example.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TransactionalTest {
@Autowired
private UserMapper userMapper;
@Test
@Transactional
public void name() {
User user=new User("测试哈哈", "女", 2);
userMapper.insert(user);
int t=1/0;
System.out.println(t);
}
}
https://www.leftso.com/article/341.html