MyBatis Generator - quickly generate entity classes and mapping files

Table of contents

1. Use of MyBatis Generator

1.1. Generate class and mapping files

1.1.1. Introduce dependencies in pom.xml

1.1.2. Create the generatorConfig.xml file according to the path configured in the configurationFile tag.

1.1.3. Automatically generate classes and mapping files

1.1.4. Add the option to obtain the primary key value in the Insert tag

1.1.5. Scanning configuration: add @Mapper annotation / add scanning annotation

1.1.6. Configure mybatis

1.1.7. Testing


1. Use of MyBatis Generator


1.1. Generate class and mapping files

1.1.1. Introduce dependencies in pom.xml

Add the version number to the properties tag.

<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>

Add the following configuration to the build => plugins tag

            <!-- mybatis ⽣成器插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis-generator-plugin-version}</version>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <!-- 相关配置 -->
                <configuration>
                    <!-- 打开⽇志 -->
                    <verbose>true</verbose>
                    <!-- 允许覆盖 -->
                    <overwrite>true</overwrite>
                    <!-- 配置⽂件路径 -->
                    <configurationFile>
                        src/main/resources/mybatis/generatorConfig.xml
                    </configurationFile>
                </configuration>
            </plugin>

What needs to be noted in the above configuration is the "configuration file path", which is the location used to generate entity class and mapping file configuration rules.

1.1.2. Create the generatorConfig.xml file according to the path configured in the configurationFile tag.

This file is used to describe the generation rules.

According to the path (src/main/resources/mybatis), create the generatorConfig.xml file in the mybatis directory.

Ps: The following configuration files need to be modified: database connections, paths to entity classes and mapping files, and database table names.

<?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>
    <!-- 驱动包路径,location中路径替换成⾃⼰本地路径 -->
    <classPathEntry location="D:\class\source\mysql-connector-java-5.1.49.jar"/>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- 禁⽤⾃动⽣成的注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!-- 连接配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/javanav_db?
characterEncoding=utf8&amp;useSSL=false"

                        userId="root"

                        password="1111">
        </jdbcConnection>
        <javaTypeResolver>
            <!-- ⼩数统⼀转为BigDecimal -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 实体类⽣成位置 -->
        <javaModelGenerator targetPackage="com.example.cyk.model"

                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- mapper.xml⽣成位置 -->
        <sqlMapGenerator targetPackage="mapper"

                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- mapper 接口⽣成位置 -->
        <javaClientGenerator type="XMLMAPPER"

                             targetPackage="com.example.cyk.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 配置⽣成表与实例, 只需要修改表名tableName, 与对应类名domainObjectName 即
       可-->
        <table tableName="j_article" domainObjectName="Article"

               enableSelectByExample="false"

               enableDeleteByExample="false" enableDeleteByPrimaryKey="false"

               enableCountByExample="false"

               enableUpdateByExample="false">
            <!-- 类的属性⽤数据库中的真实字段名做为属性名, 不指定这个属性会⾃动转换 _ 为
           驼峰命名规则-->
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_article_reply" domainObjectName="ArticleReply"

               enableSelectByExample="false"

               enableDeleteByExample="false" enableDeleteByPrimaryKey="false"

               enableCountByExample="false"

               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_board" domainObjectName="Board"

               enableSelectByExample="false" enableDeleteByExample="false"

               enableDeleteByPrimaryKey="false" enableCountByExample="false"

               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_message" domainObjectName="Message"

               enableSelectByExample="false"

               enableDeleteByExample="false" enableDeleteByPrimaryKey="false"

               enableCountByExample="false"

               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_user" domainObjectName="User"

               enableSelectByExample="false" enableDeleteByExample="false"

               enableDeleteByPrimaryKey="false" enableCountByExample="false"

               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>
</generatorConfiguration>

Notice:

The driver package path is the path of your own local warehouse

 

But be sure to pay attention! ! It needs to be in a non-Chinese directory, so you can copy this driver package and put it in a non-Chinese directory.

1.1.3. Automatically generate classes and mapping files

Reload the Maven project, mybatis-generator appears under the Plugins node, double-click to run it, and generate the corresponding classes and mapping files in the corresponding directory:

Then you can see the corresponding generation

1.1.4. Add the option to obtain the primary key value in the Insert tag

In the generated xml file, add the following attributes to each insert tag: useGeneratedKeys="true" keyProperty="id"

<!-- useGeneratedKeys = true -->
<!-- keyProperty = 主键字段--> 

<!-- 当插⼊⼀条数据后,可以通过user.getId()获取到⾃动⽣成的Id值,如果⽅法中需要⽴即获取Id值,加⼊这个配置 --> 
<insert id="insert" parameterType="com.example.cyk.model.User" 
useGeneratedKeys="true" keyProperty="id" >

Ps: This option can also be automatically generated, but it is not ideal (some problems)

1.1.5. Scanning configuration: add @Mapper annotation / add scanning annotation

There are two ways to configure scanning Mapper interfaces.

1) Add the @Mapper annotation to the mapper interface under each mapper package.

2) Add the @MapperScan("com.example.cyk.mapper") annotation to the startup class or create a new configuration class (with @Configuration annotation).

1.1.6. Configure mybatis

Configure in yml file

mybatis:
  mapper-locations: classpath:mapper/**/*Mapper.xml

1.1.7. Testing

@SpringBootTest
public class TestMapper {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void select() {
        User user = userMapper.selectByPrimaryKey(1L);
        System.out.println(user);
    }

}


 

Guess you like

Origin blog.csdn.net/CYK_byte/article/details/133974232