Integrated spring boot mybatis (3) - mybatis generator arranged


Spring Boot Integration Tutorial


Outline

MyBatis Generator is a code generator entity class can automatically generate the database table structure is mybatis, mapper interface and the corresponding mapper.xml, greatly reduce these repetitive tedious work, it is convenient.

MyBatis Generator is designed to be run multiple times, generating the code, java file will generally be covered, the old and new XML file merge code.

MyBatis Generator has three uses: the command line, eclipse plugin, maven plugin. We introduce here the most convenient maven plugin usage, other similar methods, learn may refer to relevant information.

Ready to work

Prepare data

Data will be reused before the tutorial data, such as no data refer to tutorial created.

Eclipse project

Do not create a new project, the reuse project before 2 tutorial, do not create a project by the first two articles.

Add dependent

Add mybatis generator plug-in build pom.xml of> plugins dependence under added

            <plugin>
              <groupId>org.mybatis.generator</groupId>
              <artifactId>mybatis-generator-maven-plugin</artifactId>
              <version>1.3.7</version>
              <configuration>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
              </configuration>
              <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.13</version>
                    <scope>runtime</scope>
                </dependency>
              </dependencies>
            </plugin>

The complete contents of pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qikegu</groupId>
    <artifactId>springboot-mybatis-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-mybatis-demo</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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            
            <plugin>
              <groupId>org.mybatis.generator</groupId>
              <artifactId>mybatis-generator-maven-plugin</artifactId>
              <version>1.3.7</version>
              <configuration>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
              </configuration>
              <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.13</version>
                    <scope>runtime</scope>
                </dependency>
              </dependencies>
            </plugin>
            
        </plugins>
    </build>

</project>

Add Configuration

New generatorConfig.xml file, which is MyBatis Generator's profile

image

Document reads as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 
<generatorConfiguration>
    <!--导入配置文件-->
    <properties resource="application.properties"></properties>
 
    <!-- 一个数据库一个context --> 
    <context id="default">
 
        <!-- 注释生成设置 --> 
        <commentGenerator>
            <!-- 是否生成注释代时间戳-->  
            <property name="suppressDate" value="true" />
            <!-- 是否取消注释 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
 
        <!--jdbc的数据库连接-->
        <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>
 
        <!-- 类型转换 -->  
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->  
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
 
        <!-- targetPackage:生成的实体类所在的包 -->
        <!-- targetProject:生成的实体类所在的硬盘位置 -->
        <javaModelGenerator targetPackage="com.qikegu.demo.model" targetProject="src/main/java">
            <!-- 是否允许子包 -->
            <property name="enableSubPackages" value="false" />
            <!-- 是否对modal添加构造函数 -->
            <property name="constructorBased" value="true" />
            <!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
            <property name="trimStrings" value="true" />
            <!-- 建立modal对象是否不可改变 即生成的modal对象不会有setter方法,只有构造方法 -->
            <property name="immutable" value="false" />
        </javaModelGenerator>
 
        <!-- targetPackage 和 targetProject:生成的  mapper xml 文件的包和位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <!-- 是否在当前路径下新加一层schema,ex:false路径com.qikegu.demo.model, com.qikegu.demo.model.[schemaName] -->  
            <property name="enableSubPackages" value="false" />
            <!-- 是否针对string类型的字段在set的时候进行trim调用 -->  
            <property name="trimStrings" value="true"/> 
        </sqlMapGenerator>
 
        <!-- targetPackage 和  targetProject:生成的  java interface 文件的包和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.qikegu.demo.repository" 
                             targetProject="src/main/java">
            <!-- 是否在当前路径下新加一层schema,ex:false路径com.qikegu.demo.model, com.qikegu.demo.model.[schemaName] --> 
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        
        <!-- 配置表信息 --> 
        <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample, 是否生成 example类   -->  
        <!-- 不同的表,修改tableName和domainObjectName就可以 -->
        <table tableName="user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

Explanation:

Refer to the comments in the code.

Automatic generated

Delete old files

Delete files in the following figure, these files will be automatically generated mybatis generator

image

Run mybatis generator

Eclipse on the left, right click on the project root directory pop-up menu, choose: run as -> 4.Maven build...pop-up dialog box as shown below, pay attention to modify the red circle, click run run:

image

If there is no accident, build complete, the file has been generated.

Modify the code

If you run the program, it will complain, because userMapper.java in selectUser () interface does not exist.

== comment out the relevant code: ==

UserService.java

//    public List<User> listUser(int page, int pageSize);

UserserviceImpl.java

//  @Override
//  public List<User> listUser(int page, int pageSize) {
//        List<User> result = null;
//        try {
//            PageHelper.startPage(page, pageSize); //每页的大小为pageSize,查询第page页的结果
//            PageHelper.orderBy("id ASC "); //进行分页结果的排序
//          result = userMapper.selectUser();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//      return result;
//  }

UserController.java

//    @RequestMapping(value="", method = RequestMethod.GET, produces="application/json")
//    public PageInfo<User> listUser(
//          @RequestParam(value="page", required=false, defaultValue="1") int page,
//          @RequestParam(value="page-size", required=false, defaultValue="5") int pageSize){
//      
//
//        List<User> result = userService.listUser(page, pageSize);
//        // PageInfo包装结果,返回更多分页相关信息
//        PageInfo<User> pi = new PageInfo<User>(result);
//        
//        return pi;
//    }

run

Eclipse on the left, right click on the project root directory pop-up menu, choose: run as -> spring boot apprun the program. Open Postman access interface, results are as follows:

image

to sum up

This article describes the use of mybatis generator, the main points are summarized as follows:

  1. Add mybatis generator of plug-in dependencies maven
  2. Add mybatis generator configuration file: generatorConfig.xml
  3. Run maven build: mybatis-generator: generate, generating code

The actual development, mybatis generator is useful, using them can reduce a lot of workload.

The complete code

Guess you like

Origin www.cnblogs.com/haibianren/p/11547026.html