Today’s sharing started, please give us more advice ~

The advent of Springboot has become a great boon for Java developers, greatly improving the efficiency of development. In fact, Springboot is just a package of Maven GAv on the basis of Maven. Today, springBoot can be quickly started with the simplest code.

Create a SpringBoot project

Check the dependencies needed to generate:

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>

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: 18044230java

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:
  • @Blog:

*/ @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/hell

To prove that SpringBoot is ok.

Use a code generator to generate code

Add the required dependencies

Add the following dependencies to pom.xml:

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

`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:
  • @date:

Public Class CodeGenerator {/* * public class CodeGenerator {/* * public class CodeGenerator {/* * public class CodeGenerator {/* *

* Read console contents *

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 (database, package name) needs to be changed to what you need.

That’s all about SpringBoot integration with MybatisPlus.

Two points need to be made:

(1) Class files should be placed at the same level or under the next directory of the startup class. This project is under the com.laowang.sptest package. Since SpringBoot by default scans and loads the tag classes (@restController, @Service, @Configuraion, @Component, etc.) at the same or lower level of the startup class, if you really need to load tag classes in other directories, This can be done by configuring the tag @ComponentScan(concrete package) on the startup.

Templates are dynamic files, such as HTML files, but use them with thymeleaf (gav is required for POM files).

summary

Springboot is mainly through the multiple starter and some default conventions provided by Spring to achieve the rapid construction of projects.

Today’s share is over, please forgive and give advice, thank you!