使用maven生成mybatis代码

时间:2021-09-21 05:13:23

一:准备工作

1.数据库,数据表,例如:postgres spring(数据库) user_t(表)
2.maven项目(例如:SSMLTest)

二:开始

第一步:配置Maven 的pom.xml文件
也就是在pom.xml文件中加入下面这一段代码

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>

第二步:generatorConfig.xml放置路径
使用maven生成mybatis代码

第三步:配置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>
<classPathEntry location="C:/Users/witon/.m2/repository/org/postgresql/postgresql/9.4-1201-jdbc41/postgresql-9.4-1201-jdbc41.jar"></classPathEntry>
<context id="context" targetRuntime="MyBatis3" >
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释true 是 FALSE 否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<!-- 数据库连接URL 用户名,密码 -->
<jdbcConnection driverClass="org.postgresql.Driver" connectionURL="jdbc:postgresql://127.0.0.1:5432/spring" userId="root" password="postgres"/>

<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.zoey.test.domain" targetProject="src/main/java">
</javaModelGenerator>

<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.zoey.test.mapping" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<!-- 生成Dao的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zoey.test.Dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!-- 要生成的表tableName是数据库中的表名和视图名 domainobjectName是实体类名 -->
<table tableName="user_t" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >

<property name="useActualColumnNames" value="false"/>
</table>

</context>

</generatorConfiguration>

配置文件中要注意的几点:

1.驱动的地址,要注意,使用本地的jar包,所以需要绝对路径
<classPathEntry location="C:/Users/witon/.m2/repository/org/postgresql/postgresql/9.4-1201-jdbc41/postgresql-9.4-1201-jdbc41.jar"></classPathEntry>

2.要注意生成的pojo的名称domainObjectName属性

第四步:执行mybatis-generator:generate命令

选择项目,右键->Run As ->Maven Build->Goals输入mybatis-generator:generate,点击apply,点击Run

使用maven生成mybatis代码

结果:

[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SSMLTest Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ SSMLTest ---
[INFO] Connecting to the Database
[INFO] Introspecting table user_t
log4j:WARN No appenders could be found for logger (org.mybatis.generator.internal.db.DatabaseIntrospector).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO] Generating Record class for table user_t
[INFO] Generating Mapper Interface for table user_t
[INFO] Generating SQL Map for table user_t
[INFO] Saving file UserMapper.xml
[INFO] Saving file User.java
[INFO] Saving file UserMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.417 s
[INFO] Finished at: 2017-03-03T11:17:05+08:00
[INFO] Final Memory: 7M/116M
[INFO] ------------------------------------------------------------------------

为自己鼓掌,虽然中间出现好多问题,不过幸好都一一克服了!