MyBatis06 - mybatis-generator automatically generates the code

Preface:

In the beginning of the study, Java entity classes, Dao layer interface and the corresponding mapping file, has been his own handwriting, very troublesome, but also prone to error. And MyBatis Generator (MBG) MyBatis is a code generator that generates each version of the code and ibatis2.2.0 Mybatis and later versions of the code.

When this database tables and interact do not need to create our own objects and configuration files. MBG solve some simple CRUD operations have the greatest impact on database operations, but for related queries and stored procedures still need our handwriting stored procedures and objects.


mybatis-generator use

mybatis-generator have in Usage 3: Command Line, Maven plug-ins, Eclipse plug-ins

Here we only introduce one of the most convenient is what I feel one of the best used, Maven plug-ins.

Recently made a campus secondary trading platform, so here I use a database for this project to record what the use mybatis-generator.

The project database table:

(If there is our own handwriting java entity class, and then write their own Dao layer interface, and then write their own mapping file .Oh, it is simply too much trouble!)

Write pictures described here

1, was added mybatis-generator in pom.xml

<build>
    <finalName>squirrel</finalName>
    <plugins>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
          <configurationFile>src/main/resources/conf/generatorConfig.xml</configurationFile>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
      </plugin>
    </plugins>
  </build>

Write pictures described here

2, edit generatorConfig.xml

<?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>
    <!--数据库驱动jar -->
    <classPathEntry location="E:/JAR/mysql-connector-java-5.1.7-bin.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--去除注释  -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/squirrel"
                        userId="root"
                        password="123456">
        </jdbcConnection>
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--生成实体类) -->
        <javaModelGenerator targetPackage="com.ldu.pojo" targetProject="E:/IntelliJ_Project/squirrel/src/main/java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--生成SQLMAP文件 -->
        <sqlMapGenerator targetPackage="mapper"  targetProject="E:/IntelliJ_Project/squirrel/src/main/resources">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!--生成xml映射文件-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ldu.dao"  targetProject="E:/IntelliJ_Project/squirrel/src/main/java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等-->
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="catelog" domainObjectName="Catelog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="goods" domainObjectName="Goods" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="image" domainObjectName="Image" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="comments" domainObjectName="Comments" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="notice" domainObjectName="Notice" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="reply" domainObjectName="Reply" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>
  • tableName is the table name, domainObjectName is the corresponding Java entity class name.
  • Be sure to specify the package name and write the correct address generation, responsible for error.
  • enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" Avoid generating class Example

3, run in mybatis-generator in Maven Projects

Write pictures described here

If the path error, according to prompt an error, modify the line in the console.

Write pictures described here

View the generated java classes, interfaces, and mapping file

Write pictures described here

Write pictures described here

Published 128 original articles · won praise 239 · views 330 000 +

Guess you like

Origin blog.csdn.net/HLK_1135/article/details/71411575