MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

时间:2023-03-09 04:32:30
MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,

所以查资料发现有现成的工具可以自动生成底层模型类pojo类、Dao接口mapper类、甚至Mapping映射文件xml

生成代码需要的文件和jar包:

(上图文件下载地址:http://download.****.net/detail/u012909091/7206091

其中有mybatis框架的jar包,数据库驱动程序jar包以及MyBatis生成器jar包。其中的generatorConfig.xml是需要我们来配置的文件,配置如下:

随便从本地仓库找这几个jar,版本不同也没关系。

MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

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="mysql-connector-java-5.1.6.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!--是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.0.82:13306/program_shopcart" userId="sa" password="">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成模型的包名和位置-->
<javaModelGenerator targetPackage="tb.db.mysql.shopcart.mybatis.pojo" targetProject="D:\temp\mysql_mybatis_shopcart">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="tb.db.mysql.shopcart.mybatis.xml" targetProject="D:\temp\mysql_mybatis_shopcart">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="tb.db.mysql.shopcart.mybatis.mapper" targetProject="D:\temp\mysql_mybatis_shopcart">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="account_group" domainObjectName="Account_group" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="account_grouppermissionassign" domainObjectName="Account_grouppermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="account_permission" domainObjectName="Account_permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="account_role" domainObjectName="Account_role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="account_rolepermissionassign" domainObjectName="Account_rolepermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="account_user" domainObjectName="Account_user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="account_usergroupassign" domainObjectName="Account_usergroupassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="account_userpermissionassign" domainObjectName="Account_userpermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="account_userroleassign" domainObjectName="Account_userroleassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>

1、生成mybatis文件(在IDEA的命令行里面执行)

MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

java -jar mybatis-generator-core-1.4..jar -configfile generatorConfig.xml -overwrite

MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

 2、生成mybatis文件(在控制台里面执行),进入mybatis-generator-core-1.4.0.jar所在的目录下,执行脚本:

java -jar mybatis-generator-core-1.4.0.jar -configfile generatorConfig.xml -overwrite

MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

生成好的文件存放在磁盘目录里

MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping