In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "SpringBoot how to integrate MyBatis-Plus", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "SpringBoot how to integrate MyBatis-Plus" this article.
Environment
JDK 1.8.0 +
Maven 3.0 +
MySQL 5.6.17
SpringBoot 2.0.3
Development tools
IntelliJ IDEA
SQL script DROP TABLE IF EXISTS `springboot_mybatis_ plus`; CREATE TABLE `springboot_mybatis_ plus` (`id` bigint (20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'self-added ID', `type` varchar (2) DEFAULT NULL COMMENT' household goods category: 1. Home appliances 2. Kitchen utensils', `name` varchar (50) DEFAULT NULL COMMENT 'name of daily necessities', `kitchenware 'varchar' DEFAULT NULL COMMENT 'description of daily necessities, PRIMARY KEY (`id`) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='springboot integrated mybatis-plus test form' INSERT INTO springboot_mybatis_plus (type, name, description) VALUES (for steaming rice cooker, for steaming rice), (for boiling water, for boiling water), (for cooling or heating, for cooling or heating), (for chopping vegetables, for chopping vegetables), (for peeling, for peeling), (for peeling). 'used to stir eggs') Text single project POM file (note) 4.0.0 org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE com.zwc springboot-mybatis-plus-single 0.0.1-SNAPSHOT springboot-mybatis-plus-single springboot integrated mybatis-plus jar UTF-8 1.8 1.1.9 1.2.47 1.9.9 3.0-RELEASE 1.3.2 1.1.0 org.springframework.boot spring-boot-starter-web com .Alibaba druid-spring-boot-starter ${druid.version} com.alibaba fastjson ${fastjson.version} org.codehaus.jackson jackson-mapper-asl ${jackson.mapper.asl.version} Org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis-spring-boot-starter.version} com.baomidou mybatis-plus-boot-starter ${mybatis-plus-boot-starter.version} org.mybatis.caches mybatis-ehcache $ {mybatis.ehcache.version} mysql mysql-connector-java org.projectlombok lombok org.springframework.boot spring-boot-starter-test test Src/main/java * * / * .xml true src/main/resources * / * True org.springframework.boot spring-boot-maven-plugin
The core file of the Maven project, which is used to manage project dependencies
After relying on mybatis-plus-boot-starter, I integrated MyBatis-Plus into SpringBoot.
Application.properties (Note) # Port server.port=8081 # data Source # # driver spring.datasource.driver-class-name=com.mysql.jdbc.Driver## url pay attention to change the database to your local database name spring.datasource.url=jdbc:mysql://127.0.0.1:3306/base_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false## user name notice to change the user name to your local user name spring.datasource .username = root## password pay attention to change the password to your local password spring.datasource.password=123456 # mybatis-plusmybatis-plus.mapper-locations=classpath:com/zwc/base/mapper/xml/*.xmlmybatis-plus.type-aliases-package=com.zwc.base.domain# log logging.level.com.zwc.base.mapper=debug
By default, SpringBoot reads the configuration information in the application.properties file under the loaded resources folder.
Configure the location of the mybatis-plus:mapper-locations-> mapper file; type-aliases-package-> the package where the entity class is located
Custom configuration (Note) package com.zwc.base.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration / * @ ClassName MyBatisPlusConfig * @ Desc TODO mybatis-plus configuration * @ Date 16:00 on 2019-3-26 * @ Version 1.0 * / @ Configurationpublic class MyBatisPlusConfig {/ * * @ ClassName MyBatisPlusConfig * @ Desc TODO mybatis-plus configuration blocking * @ Date 2019-3-26 18:13 * @ Version 1.0 * / @ Bean public PaginationInterceptor paginationInterceptor () {PaginationInterceptor paginationInterceptor = new PaginationInterceptor () / / set dialect paginationInterceptor.setDialectType ("mysql"); return paginationInterceptor;}}
@ Configuration + @ Bean annotation injection PaginationInterceptor class configuration MyBatis-Plus paging plug-in
Set dialect to MySQL database
Entity class (note)
Basic entity class, which corresponds to the database field one by one
Package com.zwc.base.domain; import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.extension.activerecord.Model;import lombok.Data;import java.io.Serializable;/** * @ ClassName SpringbootMybatisPlus * @ Desc TODO springboot Integrated mybatis-plus Test Table * @ Date 17:45 on 2019-3-26 * @ Version 1.0 * / @ Datapublic class SpringbootMybatisPlus extends Model {private static final long serialVersionUID =-7876888313791106541L / * * self-added ID * / @ TableId (value = "id", type = IdType.AUTO) private Long id; * category of daily necessities: 1. Home appliances 2. Kitchenware private String type; * Household name private String name; * Household description private String description; public static final String ID = "id"; public static final String TYPE = "type"; public static final String NAME = "name"; public static final String DESCRIPTION = "description"; @ Override protected Serializable pkVal () {return this.id;}}
To inherit the Model class of MyBatis-Plus, annotate the id field with @ TableId
There are also two extended entity classes: encapsulating request parameters (SpringbootMybatisPlusRequestQueryDTO) and
Encapsulate the response result (SpringbootMybatisPlusResponseDTO)
Instead of taking up space here, you can get the source code in GitHub.
Mapper interface (note) package com.zwc.base.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.zwc.base.domain.SpringbootMybatisPlus;import com.zwc.base.dto.request.SpringbootMybatisPlusRequestQueryDTO;import com.zwc.base.dto.response.SpringbootMybatisPlusResponseDTO;import org.apache.ibatis.annotations.Param;import java.util.List / * * @ ClassName SpringbootMybatisPlusMapper * @ Desc TODO springboot integrates mybatis-plus test table mapper interface * @ Date 17:52 on 2019-3-26 * @ Version 1.0 * / public interface SpringbootMybatisPlusMapper extends BaseMapper {/ / paged query test table data List query (@ Param ("data") SpringbootMybatisPlusRequestQueryDTO springbootMybatisPlusRequestQueryDTO, Page page);}
You need to inherit the BaseMapper interface of MyBatis-Plus, and note that the type is the entity class of the Model class that inherits MyBatis-Plus. BaseMapper defines a lot of functions for developers to use.
The Service service implementation class (note) @ Servicepublic class SpringbootMybatisPlusServiceImpl extends ServiceImpl implements SpringbootMybatisPlusService {/ / only the code blocks that need to be noted are posted here.}
Methods in baseMapper can be called by inheriting the ServiceImpl class of MyBatis-Plus. If you need to call another custom Mapper, use the @ Resource annotation to inject
Controller front-end controller (note) package com.zwc.base.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.zwc.base.dto.request.SpringbootMybatisPlusRequestQueryDTO;import com.zwc.base.dto.response.SpringbootMybatisPlusResponseDTO;import com.zwc.base.service.SpringbootMybatisPlusService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody Import org.springframework.web.bind.annotation.RestController;/** * @ ClassName SpringbootMybatisPlusController * @ Desc TODO springboot integrates the front controller of mybatis-plus test table * @ Date 18:14 on 2019-3-26 * @ Version 1.0 * / @ RestController@RequestMapping ("smpc") public class SpringbootMybatisPlusController {@ Autowired private SpringbootMybatisPlusService springbootMybatisPlusService / * * @ ClassName SpringbootMybatisPlusController * @ Desc TODO New data * @ Date 11:40 on 2019-3-27 * @ Version 1.0 * / @ RequestMapping ("/ add") @ ResponseBody public String add () {return springbootMybatisPlusService.add () } * @ Desc TODO Delete data * @ Date 13:15 on 2019-3-27 @ RequestMapping ("/ delete") public String delete () {return springbootMybatisPlusService.delete (); * @ Desc TODO Update data * @ Date 13:46 on 2019-3-27 @ RequestMapping ("/ update") public String update () {return springbootMybatisPlusService.update () * @ Desc TODO paged query test table data * @ Date 18:10 on 2019-3-26 @ RequestMapping ("/ query") public Page query (@ RequestParam (value = "springbootMybatisPlusRequestQueryDTO", required = false) SpringbootMybatisPlusRequestQueryDTO springbootMybatisPlusRequestQueryDTO) {return springbootMybatisPlusService.getDataByPage (springbootMybatisPlusRequestQueryDTO);}
There is one example for each method of addition, deletion, modification and search.
SpringBoot launch class (note) package com.zwc; import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; @ SpringBootApplication (scanBasePackages = "com.zwc") @ MapperScan ("com.zwc.base.mapper") public class SpringbootMybatisPlusSingleApplication {public static void main (String [] args) {SpringApplication.run (SpringbootMybatisPlusSingleApplication.class, args);}}
Scan the Mapper interface using the @ MapperScan annotation
Enable the project and call the interface (note)
Port: 8081 (you can configure server.port in the application.properties configuration file according to your preferences)
New API: http://localhost:8081/smpc/add
Delete interface: http://localhost:8081/smpc/delete
Update API: http://localhost:8081/smpc/update
Paging query API: http://localhost:8081/smpc/query project structure
These are the points that need to be paid attention to in the integration of single project MyBatis-Plus. Let's talk about the structure of multiple projects.
Multi-project
Why build multiple projects?
Generally, in the actual work, the amount of code of the project will be very large, and with the passage of time, but also constantly add new functions, if you do not pay attention at the beginning, all generally written into a project, so that the later development will be more difficult.
How to divide the module?
General development will extract the common parts, such as tool classes, system constants, common configuration and so on. They are often called commons projects.
There is also a part of the project with business logic, which may contain basic modules, user modules, order modules and so on.
They are often called service projects.
Commons Project-POM file 4.0.0 com.zwc springboot-mybatis-plus-commons 0.0.1-SNAPSHOT springboot-mybatis-plus-commons Utility jar UTF-8 1.8 Cairo-SR3 1.1.9 1.2. 47 1.9.9 3.0-RELEASE 1.3.2 1.1.0 com.alibaba druid-spring-boot-starter ${druid.version} mysql mysql-connector-java Com.alibaba fastjson ${fastjson.version} org.codehaus.jackson jackson-mapper-asl ${jackson.mapper.asl.version} org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis -spring-boot-starter.version} com.baomidou mybatis-plus-boot-starter ${mybatis-plus-boot-starter.version} org.mybatis.caches mybatis-ehcache ${mybatis.ehcache.version} Io.spring.platform platform-bom ${platform-bom.version} pom import org.springframework.boot spring-boot-maven-plugin
Configure some common dependencies, including mybatis-plus-boot-starter dependencies for integration
MyBatis-Plus commons Project-system.properties# mybatis-plus## scan mapper files mybatis-plus.mapper-locations=classpath*:com/zwc/*/mapper/xml/*.xml## scan entity class mybatis-plus.type-aliases-package=com.zwc.*.domain
Some common configurations are not often modified, or can be modified uniformly.
Here, wildcards are used to scan Mapper files and entity classes.
For example, you can also configure OSS configuration information, Redis configuration information, MongoDB configuration information and so on.
Commons Project-Custom configuration package com.zwc.core.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver / * @ ClassName MyBatisPlusConfig * @ Desc TODO mybatis-plus configuration * @ Date 16:00 on 2019-3-26 * @ Version 1.0 * / @ Configuration@PropertySource ("classpath:system.properties") public class MyBatisPlusConfig {/ * @ ClassName MyBatisPlusConfig * @ Desc TODO mybatis-plus configuration Intercept * @ Date 2019-3-26 18:13 * @ Version 1.0 * / @ Bean public PaginationInterceptor paginationInterceptor () {PaginationInterceptor paginationInterceptor = new PaginationInterceptor () / / set dialect paginationInterceptor.setDialectType ("mysql"); return paginationInterceptor;}}
Note that when injecting classes, you also load the custom configuration file, because SpringBoot does not load system.properties by default.
Other codes are almost the same as those of a single project.
Commons Engineering-Project structure
Service Engineering-Project structure
A basic module of base-service has been created in the service master project.
Each module contains api and core
Api: an extension class that mainly contains interfaces, constants, and entity classes
Core: a project with a startup class in which the core code of this module is included
Open multiple engineering projects using IntelliJ IDEA
Download the project from GitHub to your local
Open IntelliJ IDEA
Click File-> Open
Open the project directory you downloaded to your local location
Springboot-mybatis-plus- > multi-module-> springboot-mybatis-plus-service (choose to open this project)
After opening the service project
Click File-> Project Structrue again
Select Modules and click the'+ 'symbol
Click Import Module
Or open the project directory that you downloaded to your local location
Springboot-mybatis-plus- > multi-module-> springboot-mybatis-plus-commons-> pom.xml
Click OK
Click Next,Finish
Click Apply,OK
The above is all the content of the article "how SpringBoot integrates MyBatis-Plus". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.