简介
MyBatis官方提供了逆向工程 mybatis-generator,可以针对数据库表自动生成MyBatis执行所需要的代码(如Mapper.java、Mapper.xml、POJO)。mybatis-generator 有三种用法:命令行、eclipse插件、maven插件。
注意:本教程会使用命令行方式,优点如下:
- 考虑到很多人使用不同编辑器对mybatis插件支持不同,操作方式不同造成一些小问题
- 把mybatis-generator生成的数据对象独立出来,方便数据库结构的调整,如果后期重新生成数据对象避免出现覆盖的问题,例如:可能后期数据库结构调整或者改变,需要手动修改数据对象结构或者使用mybatis-generator重新生成数据对象,推荐使用后者,因为手动修改容易出错
简单粗暴,三步到位
- 下载generator工具包,放到工程的src同级目录
链接:https://pan.baidu.com/s/1DFrZYeilx4PUu1YuxnRn6g
提取码:tf6w
- 修改
generator.xml
文件,内容如下:
官方文档:http://mybatis.org/generator/configreference/xmlconfig.html
<?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="F:/Workspace/bzd/generator/mysql-connector-java-5.1.34.jar" /> <!-- 1 -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/bzd?characterEncoding=utf8" userId="root" password="mysql"> <!-- 2 -->
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 --> <!-- 3 -->
<javaModelGenerator targetPackage="com.bbzd.model" targetProject="F:/Workspace/bzd/generator/src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的映射文件包名和位置 --> <!-- 4 -->
<sqlMapGenerator targetPackage="com.bbzd.mapper" targetProject="F:/Workspace/bzd/generator/src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 --> <!-- 5 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.bbzd.dao" targetProject="F:/Workspace/bzd/generator/src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成那些表(更改tableName和domainObjectName就可以) --><!-- 6 -->
<table tableName="sys_user" domainObjectName="SysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_dept" domainObjectName="SysDept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_acl" domainObjectName="SysAcl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_acl_module" domainObjectName="SysAclModule" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_role" domainObjectName="SysRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_role_acl" domainObjectName="SysRoleAcl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_role_user" domainObjectName="SysRoleUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_log" domainObjectName="SysLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
- 进入
generator
目录,执行java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
命令
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
成功
- 将生成的数据对象复制到工程对应的目录,修改数据库结构重新生成也不会覆盖代码。