Preface:
This project is built based on Maven, using Mybatis -spring-boot as the persistence layer framework of spring-boot project
The mybatis persistence framework used in spring-boot is not the same as the original Spring project in terms of usage and annotation, and needs to rely on mybatis-spring-boot package
1. Introduce Mybatis and database and other project dependencies
1.1. Introduce myBatis dependency
<! -- mybatis-spring-boot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
Copy the code
1.2. Introduce mysql driver
<! -- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Copy the code
1.3 Overview of project POM.xml
<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>cn.eguid.carDeviceInfoSys</groupId>
<artifactId>carSys-web</artifactId>
<packaging>war</packaging>
<version>1.4.0 - the SNAPSHOT</version>
<name>carSys-web</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0. RELEASE</version>
</parent>
<dependencies>
<! -- spring-boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<! --<exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> -->
</dependency>
<! -- spring aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<! -- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<! -- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<! -- mybatis-spring-boot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<! -- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>production</id>
<dependencies>
<! -- This sample is a test for the autoconfig when commons-pool is *absent*. In production it would be useful to enable pooling by using this dependency. -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<type>pom.lastUpdated</type>
</dependency>
</dependencies>
</profile>
</profiles>
<! -- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
Copy the code
2. Configure database connection parameters, set the mappers package of Mybatis and the spring-boot service parameter configuration
Create an application.properties file under the project root to define the parameters associated with Spring-boot and the database parameters, and to configure the mappers scan path for Mybatis
For maven projects, application.properties is placed in the SRC /main/resource/ directory
The configuration is as follows:
spring.datasource.url=JDBC: mysql: / / 127.0.0.1:3306 / test
spring.datasource.username=root
spring.datasource.password=eguid
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=5
spring.datasource.max-wait=10000
spring.datasource.min-idle=1
spring.datasource.initial-size=3
server.port=8088
server.session.timeout=10
server.tomcat.max-threads=800
server.tomcat.uri-encoding=UTF-8
mybatis.mapperLocations=classpath:cn/eguid/carSysWEB/mappers/*.xml
Copy the code
3, Mybatis DAO interface and mapper. XML implementation
3.1 define the DAO interface of MyBatis
This interface is different from the Mybatis – Spring approach and requires an @mapper annotation.
The @mapper annotation is used to declare this interface to be the DAO interface of Mybatis.
package cn.eguid.carSysWeb.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import cn.eguid.carSysWeb.entity.DepInfo;
// Declare this interface as the DAO interface of Mybatis with Mapper annotations
@Mapper
public interface GetInfoDao {
public List<DepInfo> getRootInfo(a);
public List<DepInfo> getDepInfo(@Param(value = "parentCoding") String org_parent_coding);
public List<DepInfo> getDepInfoById(@Param(value="dep_id") String dep_id);
}
Copy the code
3.2 Mapper.xml corresponding to dao interface
Mapper. XML is written in the same way as mybatis
<! DOCTYPEmapper
PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.eguid.carSysWeb.dao.GetInfoDao">
<resultMap type="cn.eguid.carSysWeb.entity.DepInfo" id="depMap">
<id column="org_coding" property="dep_id"/>
<result column="org_name" property="dep_name"/>
<result column="org_parent_coding" property="dep_parent_coding"/>
<result column="last_time" property="last_time"/>
</resultMap>
<select id="getRootInfo" resultMap="depMap">
select * from car_organization where org_parent_coding is null
</select>
<select id="getDepInfo" parameterType="string" resultMap="depMap">
SELECT * FROM car_organization where org_parent_coding=#{parentCoding,jdbcType=VARCHAR}
</select>
<select id="getDepInfoById" parameterType="string" resultMap="depMap">
SELECT * FROM car_organization where org_coding=#{dep_id}
</select>
</mapper>
Copy the code
Supplement:
After the above steps, you can directly inject the DAO implementation of Mybatis in the service through spring IOC annotation. My DAO interface here is GetInfoDao, so if you inject ‘GetInfoDao’, you can correctly reference the persistence layer.
Note: The @ComponentScan annotation must be enabled in the spring-Boot entry class to scan all annotations in the project
package cn.eguid.carSysWeb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
// Enable general annotation scanning
@ComponentScan
public class Application extends org.springframework.boot.web.support.SpringBootServletInitializer {
/ * * * implementation SpringBootServletInitializer can let spring - the boot program running in the web container * /
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
builder.sources(this.getClass());
return super.configure(builder);
}
public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code
5. Conclusion:
-
To use mabatis in the spring-boot project, you need to rely on mybatis-spring-boot
-
You need to define the database connection parameters in application.xml and the mappers file scan path for Mybatis
-
Mybatis DAO interface needs to add @mapper annotation in order to be correctly scanned by Spring-boot
-
The annotation that spring-boot turns on annotation scanning is @ComponentScan