SpringBoot integrates MyBatis

Foreword:

It has been said on the Internet that SpringBoot is a framework for unlocking your configuration troubles, a super rapid development framework. I've always wanted to learn, and I just had time recently, so I learned it

This is a tutorial for SpringBoot to integrate MyBatis, using Ali's druid connection pool, known as the first in the universe.

text:

First look at the package structure.

Off topic, I really don't like ideas. It took me more than 100 seconds to open an idea, and it only took me more than 10 seconds to open an eclipse. Both count as index builds. And I don't feel that the idea is easy to use. I push people to Amway every day, except that the package structure is relatively clear.

 

pom file:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>springboot</groupId>
 7     <artifactId>login</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>login</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>2.0.1.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28         <!-- web start -->
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-web</artifactId>
32         </dependency>
33 
34         <!-- test -->
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-test</artifactId>
38             <scope>test</scope>
39         </dependency>
40 
41         <!-- 模板引擎 html 依赖 -->
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-starter-thymeleaf</artifactId>
45         </dependency>
46 
47         <!-- mybatis -->
48         <dependency>
49             <groupId>org.mybatis.spring.boot</groupId>
50             <artifactId>mybatis-spring-boot-starter</artifactId>
51             <version>1.3.1</version>
52         </dependency>
53 
54         <!-- mysql -->
55         <dependency>
56             <groupId>mysql</groupId>
57             <artifactId>mysql-connector-java</artifactId>
58             </dependency>
59         
60         <!-- 线程池 阿里的druid -->
61         <dependency>
62            <groupId>com.alibaba</groupId>
63            <artifactId>druid-spring-boot-starter</artifactId>
64            <version>1.1.9</version>
65         </dependency>
66 
67     </dependencies>
68 
69     <build>
70         <plugins>
71             <plugin>
72                 <groupId>org.springframework.boot</groupId>
73                 <artifactId>spring-boot-maven-plugin</artifactId>
74             </plugin>
75         </plugins>
76     </build>
77 
78 </project>

 

Note: If you want to use the druid thread pool, you must introduce the druid-spring-boot-starter package, not the druid package, you can see Ali's official documentation

SpringBoot integrates MyBatis steps:

1: First configure mybatis in properties to scan the xml configuration file of mybatis -> mybatis.mapperLocations=classpath:mapper/*.xml

2: Secondly, add the annotation to scan the dao layer in the startup class -> @MapperScan("com.dao")

3: Just use it with confidence

 

I feel that SpringBoot is really super convenient, and I like SpringBoot more and more.

 

 

Appendix:

1 # databases
2 spring.datasource.druid.url=jdbc:mysql:///seckill?serverTimezone=GMT&useSSL=false
3 spring.datasource.druid.username=root
4 spring.datasource.druid.password=123456
5 
6 # mybatis
7 mybatis.mapperLocations=classpath:mapper/*.xml

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325162343&siteId=291194637