Springboot Learning--04springboot Integration MyBatis (top) (Configuration MyBatis generator,pagehelper)

Source: Internet
Author: User

The land continued to be busy for several days, continue to write.

This article is modeled after the excellent writing of the article, plus own understanding and trampled the pit, the original address: https://www.jianshu.com/p/5cd772c07041?utm_campaign=haruki&utm_content= Note&utm_medium=reader_share&utm_source=weixin

Environment/version at a glance:
    • Development tools: Eclipse
    • Springboot:2.0.1.release
    • Jdk:1.8.0_40
    • maven:3.3.9
Additional Features:
    • Pagehelper Paging Plugin
    • MyBatis Generator automatically generated code plug-ins
Start building: I. Create a project:

1, also as above, create Springboot project (default is the latest version),

2, fill in the basic information of the project,

3, select the base of the project dependency package, can be manually added later,

4. Select Finish to wait for the load of the dependent package to complete.

This is the build Pom.xml 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"3xsi: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>com.luozhen</groupId>7<artifactId>StudyForSpringBoot</artifactId>8<version>0.0.1-SNAPSHOT</version>9<packaging>jar</packaging>Ten  One<name>StudyForSpringBoot</name> A<description>there is and kinds of life, one was burning, the other is rotten.</description> -  -<parent> the<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-parent</artifactId> -<version>2.0.1.RELEASE</version> -<relativePath/> <!--lookup parent from repository to +</parent> -  +<properties> A<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> at<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> -<java.version>1.8</java.version> -</properties> -  -<dependencies> -<dependency> in<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-web</artifactId> to</dependency> +<dependency> -<groupId>org.mybatis.spring.boot</groupId> the<artifactId>mybatis-spring-boot-starter</artifactId> *<version>1.3.2</version> $</dependency>Panax Notoginseng  -<dependency> the<groupId>org.springframework.boot</groupId> +<artifactId>spring-boot-devtools</artifactId> A<scope>runtime</scope> the</dependency> +<dependency> -<groupId>mysql</groupId> $<artifactId>mysql-connector-java</artifactId> $<scope>runtime</scope> -</dependency> -<dependency> the<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-test</artifactId>Wuyi<scope>test</scope> the</dependency> -</dependencies> Wu  -<build> About<plugins> $<plugin> -<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-maven-plugin</artifactId> -</plugin> A</plugins> +</build> the  -  $</project>

This is the generated file directory, where the configuration file Application.properties can choose according to their own habits, use properties or yml files, this project uses the YML profile, so put the original application.properties deleted, create a application.yml file

After change: (can be directly rename suffix named Yml)

Two. Configure MyBatis and automatically generate code generate

There are two kinds of mybatis configuration, one is the annotation version, the other is the XML version, with generate, it can dynamically generate SQL, and it is convenient to adjust SQL.

Here we are explaining the XML version, with generate.

1. Although we relied on the MyBatis dependency package when we created the project earlier, we would still like to confirm it. If not previously checked, we can also manually import the MyBatis dependency package, write a code import dependency on the <dependencies> tab,

1         <dependency>2             <groupId>org.mybatis.spring.boot</groupId>3             < Artifactid>mybatis-spring-boot-starter</artifactid>4             <version>1.3.2</version >5         </dependency>

2. The next step is to add the relevant configuration in the APPLICATION.YML configuration file.

1 #端口号2 Server:3port:555554 5 #datasource, Data Connections6 Spring:7 DataSource:8driver-class-Name:com.mysql.jdbc.Driver9 # Basic PropertiesTenUrl:jdbc:mysql://Localhost:3306/test?useunicode=true&characterencoding=utf-8&usessl=false One Username:root Apassword:123456 -  - #mybatis the MyBatis: -type-aliases- Package: com.luozhen.entity #实体类映射文件包 -Mapper-locations:classpath:mybatis/mappers/*.xml #生成的sql语句

The above is the configuration file of the basic configuration, the following can be added to the detailed content, but also need to note that the configuration behind the #mybatis need the corresponding package, below is my file package,

Many articles have written on the need for mybatis-config.xml files, but you will find that the content will be duplicated with APPLICATION.YML, configured as a database connection configuration. Springboot will automatically load, spring.datasource.* the relevant configuration, the data source will be automatically injected into the sqlsessionfactory, Sqlsessionfactory will be automatically injected into the mapper, you don't have to worry about everything. , just pick it up and use it right away.

4. Here the MyBatis configuration is complete, and the next is the configuration of generate. Similarly, it is necessary to import a dependency package in Pom.xml, in <build><plugins></plugins></ Add Dependencies in Build>,

1<!--generator automatically generate code-dependent packages--2<plugin>3<groupId>org.mybatis.generator</groupId>4<artifactId>mybatis-generator-maven-plugin</artifactId>5<version>1.3.5</version>6<configuration>7<!--Configure the location of the generatorconfig, note: If you do not configure Generatorconfig location, the default location is Src/main/resources. -8<configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile>9<verbose>true</verbose>Ten<overwrite>true</overwrite> One</configuration> A<executions> -<execution> -<id>generate MyBatis files</id> the<goals> -<goal>generate</goal> -</goals> -<phase>generate</phase> +<configuration> -<verbose>true</verbose> +<overwrite>true</overwrite> A</configuration> at</execution> -</executions> -<dependencies> -<dependency> -<groupId>MySQL</groupId> -<artifactId>mysql-connector-Java</artifactId> in<version>5.1.46</version> -</dependency> to<dependency> +<groupId>org.mybatis.generator</groupId> -<artifactId>mybatis-generator-core</artifactId> the<version>1.3.6</version> *</dependency> $<dependency>Panax Notoginseng<groupId>org.mybatis</groupId> -<artifactId>mybatis</artifactId> the<version>3.4.6</version> +</dependency> A</dependencies> the</plugin>

5. Configure Generatorconfig.xml, content,

1<?xml version= "1.0" encoding= "UTF-8"?>2 3<!DOCTYPE generatorconfiguration4Public "-//mybatis.org//dtd MyBatis Generator Configuration 1.0//en"5"Http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >6 7<generatorConfiguration>8<context id= "Mysqltables" targetruntime= "MyBatis3" >9< whether to remove automatically generated annotations!--trueIsfalse: No--Ten<commentGenerator> One<property name= "Suppressdate" value= "false"/> A<property name= "Suppressallcomments" value= "true"/> -</commentGenerator> -<!--database link URL, user name, password-- the<jdbcconnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "Jdbc:mysql://localhost:3306/test? Useunicode=true&amp;characterencoding=utf-8&amp;usessl=false "userid=" root "password=" 123456 "> -</jdbcConnection> -  -<javaTypeResolver> +<property name= "Forcebigdecimals" value= "false"/> -</javaTypeResolver> +  A<!--generate model models, corresponding packages, storage locations can specify specific paths, such as/PROJECTNAME/SRC, or you can use MAVEN to generate at<javamodelgenerator targetpackage= "com.luozhen.entity" targetproject= "Src/main/java" > -<property name= "Enablesubpackages" value= "true"/> -<property name= "Trimstrings" value= "true"/> -</javaModelGenerator> -  -<!--the corresponding XML mapper file-- in<sqlmapgenerator targetpackage= "Mybatis/mappers" targetproject= "Src/main/resources" > -<property name= "Enablesubpackages" value= "true"/> to</sqlMapGenerator> +  -<!--the corresponding DAO interface-- the<javaclientgenerator type= "Xmlmapper" targetpackage= "Com.luozhen.daos" targetproject= "Src/main/java" > *<property name= "Enablesubpackages" value= "true"/> $</javaClientGenerator>Panax Notoginseng  -  the<!--the table TableName to be generated is the name of the table in the database or the view name Domainobjectname is the entity class name (does not generate example (helper Class) class)-- +<table tablename= "sys_department" domainobjectname= "Sysdepartment" enablecountbyexample= "false" Enableselectbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" > A</table> the  +</context> -  $</generatorConfiguration>

The specific reference configuration can be viewed:

https://www.jianshu.com/p/e09d2370b796 Mybatis Generator The most complete configuration detailed

6. Final configuration generator build, F5 refresh

Project Right-==>run as ==> maven bulid ==> Pop-up dialog ==> enter goals in Mybatis-enerator:generate or click Select--"Choose your MyBatis plugin-- "Apply--" run, the results are as follows

Springboot Learning--04springboot Integration MyBatis (top) (configuration MyBatis generator,pagehelper)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.