一、依赖
注意:springboot整合mybatis包中默认依赖mybatis,故不需要再引入mybatis包,以免由于版本不同出现问题。
<!-- SpringBoot Mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.3</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
二、配置文件
在项目的resources文件夹下面新建:application.yml
配置数据源
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8
username: root
password: root
配置Mybatis
mybatis:
mapper-locations: classpath:cool/gjh/mapper/*Mapper.xml
type-aliases-package: cool.gjh.entity
配置DAO扫描
在入口类上加@MapperScan
注解
package cool.gjh.app;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* SpringBoot入口类
*
* @author ACE_GJH
*/
@SpringBootApplication
@MapperScan("cool.gjh.dao")
@ComponentScan("cool.gjh")
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
三、测试Mybatis
测试类
package cool.gjh.mybatis;
import cool.gjh.app.App;
import cool.gjh.dao.StudentDao;
import cool.gjh.entity.Student;
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 java.util.List;
/**
* 测试SpringBoot整合Mybatis
*
* @author ACE_GJH
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class MybatisTest {
@Autowired
private StudentDao studentDao;
@Test
public void testDao(){
List<Student> students = studentDao.selectAll();
for (Student student : students) {
System.out.println(student);
}
}
}
DAO接口依赖注入报错解决方案
如图,IDEA会出现如下报错:
其实代码是没有问题的,而且运行的话也能运行出正确的结果,但是这个报错很不好看,所有取消掉这个“误报”就好了。
在报错的地方按"alt+Enter",如图往下选择。
将"ERROR"改为"WARNING",点击"OK"就好了。
测试结果
四、事务控制
测试未开启事务插入数据
package cool.gjh.mybatis;
import cool.gjh.app.App;
import cool.gjh.dao.StudentDao;
import cool.gjh.entity.Student;
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 java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* 测试SpringBoot整合Mybatis
*
* @author ACE_GJH
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class MybatisTest {
@Autowired
private StudentDao studentDao;
@Test
public void testSelectAll() {
System.out.println("------开始查询------");
List<Student> students = studentDao.selectAll();
for (Student student : students) {
System.out.println(student);
}
System.out.println("------查询完毕------");
}
@Test
public void testInsert() {
System.out.println("-----开始插入----");
Student student = new Student();
student
.setId(UUID.randomUUID().toString())
.setName("CSDN")
.setAge(30)
.setBirthday(new Date())
.setSex(false)
.setHeight(1.65)
.setWeight(50)
;
studentDao.insert(student);
System.out.println("-----插入完毕----");
int error = 1 / 0;
testSelectAll();
}
}
测试结果testInsert():
可以看出,未开启事务,如果过程中出现了异常,数据还是插入了进来。
测试开启事务插入数据
在类和方法上加入@Transactional
控制事务,并在方法上细化事务的传播。
package cool.gjh.mybatis;
import cool.gjh.app.App;
import cool.gjh.dao.StudentDao;
import cool.gjh.entity.Student;
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.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* 测试SpringBoot整合Mybatis
*
* @author ACE_GJH
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
@Transactional
public class MybatisTest {
@Autowired
private StudentDao studentDao;
@Test
@Transactional(propagation = Propagation.SUPPORTS)
public void testSelectAll() {
System.out.println("------开始查询------");
List<Student> students = studentDao.selectAll();
for (Student student : students) {
System.out.println(student);
}
System.out.println("------查询完毕------");
}
@Test
@Transactional(propagation = Propagation.REQUIRED)
public void testInsert() {
System.out.println("-----开始插入----");
Student student = new Student();
student
.setId(UUID.randomUUID().toString())
.setName("CSDN")
.setAge(30)
.setBirthday(new Date())
.setSex(false)
.setHeight(1.65)
.setWeight(50)
;
studentDao.insert(student);
System.out.println("-----插入完毕----");
int error = 1 / 0;
testSelectAll();
}
}
测试结果:
可以看到,当加上事务控制后,在整个过程中如果出现了异常,事务可以回滚。