Project development inevitably needs to deal with the database, the author developed a project widely used MyBatis as an ORM framework.
This article explains how to integrate MyBatis in the Spring Boot project, and configure the annotation-based configuration in the next article based on the XML configuration. Development Environment JDK 1.8 Maven 3.3 Spring Boot 1.5.8.RELEASE Mybatis 3.4.4 code
First introduce spring-boot-starter-parent:
<!--Inherit defaults from Spring Boot--
<parent>
<groupid>org.springframework.boot</ groupid>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8. Release</version>
</parent>
MyBatis dependencies:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mybatis -<dependency> <groupId>org.mybatis.spring.boot</groupId> <artif Actid>mybatis-spring-boot-starter</artifactid> <version>1.3.1</version> </depe
ndency> <!--db--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.version}</ver
sion> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> & Lt;/dependency> ≪/dependencies>
Application.yml
MyBatis:
mapper-locations:classpath:mapper/*.xml
Type-aliases-package:springboot.mybatis.model
Type-handlers-package:springboot.mybatis.typehandler
configuration:
map-underscore-to-camel-case:true
default-fetch-size:100
Spring:
DataSource:
url:jdbc:mysql://localhost:3306/demo?useunicode= True&characterencoding=utf-8&zerodatetimebehavior=converttonull
username:root
password:root
Driver-class-name:com.mysql.jdbc.driver
The MyBatis mapper classes and mapper files involved are recommended for production through mybatis-generator, simple and efficient.
Launch class App
Package springboot.mybatis.xml;
Import Org.mybatis.spring.annotation.MapperScan;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* ${description}
*
* @author Ricky Fung
*
/@SpringBootApplication @MapperScan ( Basepackages = "Com.mindflow.springboot.mybatis.mapper") public
class App {public
static void Main (string[] args) {
springapplication.run (app.class, args);
}
}
Finally, just add @MapperScan annotations to the app class. Source Code
Portal: Spring-boot-mybatis3-xml-config View Source code