您的位置:首页 > 编程语言

Mybatis Generator自动生成代码

2016-08-16 10:52 225 查看
mybatis-generator有三种用法:命令行、eclipse插件、maven插件。个人觉得maven插件最方便,可以在eclipse/intellij idea等ide上可以通用。

pom.xml文件添加插件

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.3</version>
<configuration>
<!-- 指定配置文件 -->
<configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
</plugin>

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/Administrator/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar"/>
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<!-- 配置连接属性 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/admin-template" userId="root"
password=""/>

<!-- 配置model类生成位置 -->
<javaModelGenerator targetPackage="org.springside.quickstart.entity"
targetProject="D:/workspaces/java/admin-template/src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<!-- 配置sqlMap文件生成位置 -->
<sqlMapGenerator targetPackage="mybatis"
targetProject="D:/workspaces/java/admin-template/src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<!-- 配置mapper接口生成位置 -->
<javaClientGenerator targetPackage="org.springside.quickstart.dao"
targetProject="D:/workspaces/java/admin-template/src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!-- 进行表配置 -->
<table tableName="sys_user" domainObjectName="SysUser"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>

<table tableName="sys_role" domainObjectName="SysRole"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>

<table tableName="sys_permission" domainObjectName="SysPermission"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>

<table tableName="sys_account" domainObjectName="SysAccount"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>

</context>
</generatorConfiguration>

运行

mvn mybatis-generator:generate
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: