Abstract: This article brings you SpringBoot integration MybatisPlus tutorial, SpringBoot project to rely on data module for data operation, and a simple test.

This article is shared from huawei cloud community “SpringBoot integration MybatisPlus [super detail]”, the original author: Cow coaze Conan.

Create a SpringBoot project

== Check to generate the required dependencies: ==

I changed the suffix “application” to “.yml “to make it easier.

= = pom. XML: = =

<? The XML version = "1.0" encoding = "utf-8"? > < 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.4.4 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.keafmd</groupId> < artifactId > springboot - mybatisplus < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < name > springboot - mybatisplus < / name > <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>  <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId>  <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes>  <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>Copy the code

Since we have configured the data source, we need to configure the data source in application.yml, otherwise it will not get up, I also changed the port incidentally.

= = application. Yml: = =

Server: port: 80 spring: a datasource: url: JDBC: mysql: / / 127.0.0.1:3306 / SSM - java1? useSSL=false&&characterEncoding=UTF-8 driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 18044229Copy the code

Let’s test it out with HelloController

= = HelloController: = =

package com.keafmd.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Keafmd ** @className: HelloController * @description: * @author: @date: 2021-04-09 11:11 * @blog: https://keafmd.blog.csdn.net/ */ @RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "keafmd"; }}Copy the code

To run the startup class, visit http://127.0.0.1/hello

To prove that SpringBoot is ok.

Use a code generator to generate code

Add the required dependencies

Add the following dependencies to pom.xml:

<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <scope>test</scope> < version > 3.4.0 < / version > < / dependency > < the dependency > < groupId > org. Freemarker < / groupId > < artifactId > freemarker < / artifactId > the < scope > test < / scope > < version > 2.3.31 < / version > < / dependency >Copy the code

Since code generators are not used in production, they are only used in development. So we can write the code generator in the test package and define the dependent usage scenarios as test.

CodeGenerator

= = CodeGenerator: = =

package com.keafmd.mp; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Keafmd ** @className: CodeGenerator * @description: * @author: Cogenerator * @date: */ */ Public class CodeGenerator {/** * <p> * </p> */ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); Help.append (" Please enter "+ tip +" : "); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotBlank(ipt)) { return ipt; }} throw new MybatisPlusException(" please enter the correct "+ tip +"! ); } public static void main(String[] args) {// AutoGenerator MPG = new AutoGenerator(); // GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); // System.out.println("projectPath = " + projectPath); gc.setOutputDir(projectPath + "/src/main/java"); // gc.setOutputDir("D:\\test"); SetAuthor (" Public account: Conan the Cow "); gc.setOpen(false); // gc.setSwagger2(true); Entity attribute Swagger2 annotation GC.setServicename ("%sService"); mpg.setGlobalConfig(gc); // DataSourceConfig DSC = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/ssm-java1? useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("18044229"); mpg.setDataSource(dsc); PackageConfig PC = new PackageConfig(); pc.setModuleName(null); pc.setParent("com.keafmd"); mpg.setPackageInfo(pc); InjectionConfig CFG = new ectionConfig() {@override public void initMap() {// to do nothing}}; / / if the template engine is a freemarker String templatePath = "/ templates/mapper. XML. FTL"; // String templatePath = "/templates/mapper.xml.vm"; List<FileOutConfig> focList = new ArrayList<>(); Add (new FileOutConfig(templatePath) {@override public String outputFile(TableInfo TableInfo) { // Customize the output file name. If your Entity has a prefix or suffix, note that the XML name will change accordingly!! return projectPath + "/src/main/resources/mapper/" + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; }}); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // TemplateConfig TemplateConfig = new TemplateConfig(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); Strategy.setinclude (scanner(" table name, multiple English comma split ").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix("m_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }}Copy the code

Run the code generator and enter the tables you want to generate on the console

This will generate some packages and the corresponding code, note that the relevant code in the CodeGenerator (e.g. database, package name) needs to be what you need.

That’s all about SpringBoot integration with MybatisPlus.

Click to follow, the first time to learn about Huawei cloud fresh technology ~