Use eclipse mybatis generator plugin to automatically build code in maven project and some pits encountered

mybatis-generator is a tool that automatically generates model, dao and mapper when using the mybatis framework, which greatly reduces the manual coding time of business developers. I researched it myself today and shared my experience for everyone's simplicity. use.

I am using maven to build. First, I need to add the mybatis-generator dependency package and plug-in to the pom.xml file. The configuration is as follows:

Add in dependencies:

<dependency>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-core</artifactId>

<version>1.3.2</version>

</dependency>

Add in build plugins:

<plugin>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-maven-plugin</artifactId>

<version>1.3.2</version>

<configuration>

<!-- Mybatis configuration file for code generation -->

<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>

<verbose>true</verbose>

<overwrite>true</overwrite>

</configuration>

</plugin>

The above is the configuration that needs to be added in pom.xml, the next step is to build the generatorConfig.xml file under resources, and the configuration

<?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="/Users/zhuyanhui/Documents/soft/mysql-connector-java-5.1.45/mysql-connector-java-5.1.45-bin.jar" />

<context id="my" targetRuntime="MyBatis3">

<commentGenerator>

<property name="suppressDate" value="false" />

<property name="suppressAllComments" value="true" />

</commentGenerator>

<!-- mysql database connection -->

<jdbcConnection driverClass="com.mysql.jdbc.Driver"

connectionURL="jdbc:mysql://127.0.0.1:3306/Managers" userId="root"

password="123456" />

<!-- Generate model entity class file location-->

<javaModelGenerator targetPackage="com.siyuan.entity"

targetProject="/Users/zhuyanhui/eclipse-workspaces/siyuan-entity/src/main/java">

<property name="enableSubPackages" value="true" />

<property name="trimStrings" value="true" />

</javaModelGenerator>

<!-- Generate mapper.xml configuration file location-->

<sqlMapGenerator targetPackage="mappings"

targetProject="/Users/zhuyanhui/eclipse-workspaces/siyuan-web/src/main/resources">

<property name="enableSubPackages" value="true" />

</sqlMapGenerator>

<!-- Generate mapper interface file location-->

<javaClientGenerator targetPackage="com.siyuan.dao"

targetProject="/Users/zhuyanhui/eclipse-workspaces/siyuan-dao/src/main/java"

type="XMLMAPPER">

<property name="enableSubPackages" value="true" />

</javaClientGenerator>

<!-- The table name corresponding to the entity class that needs to be generated, multiple entity classes can copy multiple copies of the configuration -->

<table tableName="userRole" domainObjectName="UserRole"

enableCountByExample="false" enableUpdateByExample="false"

enableDeleteByExample="false" enableSelectByExample="false"

selectByExampleQueryId="false">

</table>

</context>

</generatorConfiguration>

Since I operate in modules, the entity class is also a project, so the entity class and the dao layer and the mapping configuration are not under the same project.

So far, all the configurations have been completed. To use it in ecplise, right-click the project, maven build, and add the command mybatis-generator:generate to generate the code.

Refresh the project, you can see the new code

Finally, let me talk about the solutions to the following problems I encountered when using the command

[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: 

The only reason the dependency must be is that I added two identical dependencies to the pom file and just delete one

Non-resolvable parent POM for com.siyuan:siyuan-web:0.0.1-SNAPSHOT: Could not find artifa

It turns out that the parent project is not registered, right-click the parent project -run as -maven install to solve

I don't know if you have encountered some of these pits, I hope this blog can help you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325455102&siteId=291194637