This paper describes the initial integration process SpringBoot MyBatis and details that need attention.
SpringBoot Profile
Official website https://spring.io/projects/spring-boot/
Introduction to MyBatis
Official website: https://mybatis.org/mybatis-3/zh/index.html
MyBatis advantages:
History development
MyBatis is Ibatis evolved, Ibatis1.x and Ibatis2.x, are called Ibatis, in Ibatis3.x and later versions are called: MyBatis
Position in the three-tier system software
MyBatis located three Dao layer software architecture that MyBatis mainly interact with the database!
MyBatis in the end is what?
What is MyBatis?
MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.
MyBatis is a first class persistence framework, support for custom SQL , stored procedures and advanced mappings. MyBatis eliminates almost all JDBC manually set parameters and codes as well as result of the search. MyBatis can use simple XML or annotations configuration and primitives, mapping interface and Java POJOs (simple Java objects, is in fact general the JavaBeans ) mapped to the database record.
MyBatis advantage [focus]
MyBatis and native JDBC, Spring of JdbcTemplate compared to what advantage?
-
MyBatis itself is a frame except for the additions and deletions to change search operation data table, also supported field mapping, caching mechanism.
-
MyBatis supports dynamic SQL [according to different parameters, can be spliced into different SQL]
-
MyBatis supports java code [] to focus on business logic and SQL statements [focusing on] the separation process data
-
MyBatis can also link directly mapped to relational tables relationship POJO objects.
-
MyBatis is a semi-automatic (hand-written SQL) The ORM] [Object Relation Mapping Framework.
Operating principle
other
On specific cases, SSM integration, reverse engineering, detailed write free articles ... not much go into details here.
Integration steps
Prepare the database and tables
Import dependence
Introducing mybatis, MySQL, maven dependent AlibabaDruid connection pool
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>Modify SpringBoot profile for application.yml
#配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#springboot整合mybatis
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.kjlw.entityCreating the Entity Classes
Mapper interface and create a layer mapping file
Create a Service layer interface and implementation class
Creating Controller Layer Interface
Add annotations scan on SpringBoot main startup class
Start and test
slightly
Precautions
-
Traditional ssm integration Mapper interface layer mapping file should be in the same package (usually Mapper package), but SpringBoot with MyBatis integration, the Mapper interface if the default mapping files are placed in the same package under, still error, this time there are two solutions:
1) establish the same package name in the resources directory and map files in
2) Add the following configuration in the pom file:
-