Use STS (SpringToolSuite) tool Spring Boot to integrate MyBatis

The basic steps of SpringBoot integration Mybatis

Step 1: Add dependency;
Step 2: Configure data source;
Step 3: Scan interface package.

The detailed integration steps are as follows:

  1. Add dependencies; In addition to regular dependencies, Mybatis and MySQL dependencies need to be added.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ysd.springboot</groupId>
<artifactId>spring-boot-mybatis</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-mybatis</name>
<url>http://maven.apache.org</url>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<!-- 项目设置:编码格式 UTF-8 及 springboot 相关版本 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
<druid>1.0.18</druid>
</properties>
<dependencies>
<!-- Spring Boot SpringMVC 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${
    
    mybatis-spring-boot}</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${
    
    mysql-connector}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- SpringBoot 插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- SpringBoot 项目打包名称 -->
<finalName>springmybatis</finalName>
</build>
</project>
  1. In the application.properties configuration file, configure the data source, Mybatis configuration and mapping files.
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/库名
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
# 实体所在包,起别名
mybatis.typeAliasesPackage=com.ctt.entity
# 映射文件所在路径
mybatis.mapperLocations=classpath:mapper/*.xml
  1. Annotate the scanning interface package on the main module, using @MapperScan("package name").
@SpringBootApplication // Spring Boot 应用的标识
@MapperScan("com.ctt.dao") // mapper 接口类扫描包配置
//如果要显示 Sql 细节还需要在 logback 配置<logger name="接口类所在包" level="debug" />
public class Application {
    
    
public static void main(String[] args) {
    
    
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(Application.class,args);
}
}

Note: The automatic injection of Controller layer, Dao layer and Service layer is exactly the same as the original Spring framework usage. The
project structure diagram is as follows:
Insert picture description here
Use STS (SpringToolSuite) tool Spring Boot to integrate MyBatis case
@Transactional annotation to declare transaction

@Transactional //开启事务
public void transfer(int outter, int inner, Integer money) {
    
    
accountDao.moveOut(outter, money); //转出
//为测试转帐失败,此处抛出异常,用事务进行控制
int i = 1/0; // 抛出异常
accountDao.moveIn(inner, money); //转入
}

Guess you like

Origin blog.csdn.net/chaotiantian/article/details/115359278