SpringBoot2.x integrates mybatis-generator

brief description

mybatis is a semi-automatic orm framework, that is, mybaitis only supports the mapping of data detected from the database to the model class, and the mapping from entities to databases requires writing the corresponding relationship by yourself. mybatis is very flexible, and you can write your own sql statements as you like. Complex database operations, but require a lot of configuration files, as well as various mappers and dao and entity associations, resulting in the use of mybatis is not concise enough, and later mybatis also found this shortcoming, developed the mybatis-generator tool to automatically generate entities class, mapper configuration file, dao layer code to reduce the development workload

achieve integration

Create a SpringBoot project and configure the configuration file under resources

#数据库配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo
    username: root
    password: root
#mapper配置
mybatis:
  mapper-locations: classpath:mapper/*.xml

Introduce the mybatis-generator-maven-plugin plugin and depend on org.mybatis.generator in pom.xml

<dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                    <skip>false</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

Create a generator directory under resources, and create a configuration file generatorConfig.xml under the directory

<?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>
    <!-- 数据库驱动:选择你的本地的数据库驱动包-->
    <classPathEntry
            location="数据库的JDBC驱动位置 "/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="自填"
                        connectionURL="自填"
                        userId="自填" password="自填">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="xxx.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="resources.mapper" targetProject="src/main">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="xxx.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->

        <table tableName="" domainObjectName="" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

Run
1. Execute the maven command under the project

mvn mybatis-generator:generate

2. Click the command in the maven plug-in directory
insert image description here

Integration complete

Guess you like

Origin blog.csdn.net/qq_42133100/article/details/102782382