经过网上各种资料的尝试,终于成功地利用mybatis进行自动生成代码,所以想记下解决的办法。顺便也说一下mybatis自动生成代码的过程。
首先我是用eclipse工具,利用maven创建的采用ssm框架的的项目,
搭建项目参考了https://www.cnblogs.com/aflyun/p/6421441.html,以下是我搭建的项目的结构
回到正题,如何自动生成代码,有参考https://blog.youkuaiyun.com/wangxy799/article/details/60870361,需要的东西如下
由于我的pom.xml里面已经引入了mybatis-3.4.1.jar,所以lib里面就不用再引入了
但是有一个问题,我的pom.xml里面也引入了mysql的jar包,lib里面不放mysql的jar包自动生成时就会报错说找不到mysql的jar包,所以这里必须放mysql的jar包
mybatis-generator-core-1.3.2.jar我没有在pom.xml里面引入,所以我把该包放在了lib下
好了,jar包准备完成了
接下来出场的也是重量级角色generatorConfig.xml配置文件,我的该配置文件也是放在lib下面的,代码如下:
<?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.30.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.145.27:3306/sun_test" userId="root" password="huatek@123">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="sun.ssm.entity" targetProject="E:\workspace\uip\sunTest\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="sun.ssm.mapper" targetProject="E:\workspace\uip\sunTest\src\main\java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="sun.ssm.dao" targetProject="E:\workspace\uip\sunTest\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"></table>
<table tableName="sys_role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
在这里,说明一下里面需要改的东西
1、<classPathEntry location="mysql-connector-java-5.1.30.jar"/> 该行的jar包名一定要和lib目录下的jar包名相同
2、 <context id="DB2Tables" targetRuntime="MyBatis3"> 这里的DB2Tables是唯一的,而且后面的MyBatis3要和mybatis的版本保持一致
3、<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.145.27:3306/sun_test" userId="root" password="xx@123">
</jdbcConnection>
该行是数据库的相关信息,不做详细介绍
4、 <!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="sun.ssm.entity" targetProject="E:\workspace\uip\sunTest\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
这里改的是最终表对应的实体要放到哪个包里,targetProject这里先不做介绍,最后做重点介绍
5、 <!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="sun.ssm.mapper" targetProject="E:\workspace\uip\sunTest\src\main\java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
这里需要改的是最终映射文件放在哪个包里面
6、 <!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="sun.ssm.dao" targetProject="E:\workspace\uip\sunTest\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
这里需要改的是Dao最终放的包
7、 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="user_t" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="sys_role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
最后是需要注意的是表名和实体名,也可以不指定实体名,默认会把带下划线的表名转成驼峰样式
东西准备完了之后,需要在lib目录下打开cmd窗口,这里说一个在某目录下打开cmd窗口的快捷方式
到指定目录下,按住shift,右击然后点击从这里打开命令窗口,英文版的则是点击open command window here.
打开命令窗口之后,执行命令
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
这就大功告成了!!!
这里可能会有人说跑题了吧,来说说为什么会报错Project . does not exist,一开始我的targetProject写的是src\main\java,然后就报错,在网上找了各种资料,尝试了各种方法,有的说是在前面要加上项目名sunTest,试了src也不行,看到有人说要写绝对路径,试了一下,也就是E:\workspace\uip\sunTest\src\main\java,成功啦,解决这个问题花了大半天,毅然决定记下来,需要的时候就可以看看啦
一个小提示,如果一个目录下生成的mapper.xml文件多的话,下面每个映射文件中与下面代码块相同的结果map的id值是相同的,所以自动生成后需要把每个映射文件resultMap的id值区分开来。
<resultMap id="BaseResultMap" type="sun.ssm.entity.Role">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="active" jdbcType="VARCHAR" property="active" />
<result column="role" jdbcType="VARCHAR" property="role" />
<result column="group_id" jdbcType="VARCHAR" property="groupId" />
<result column="comment" jdbcType="VARCHAR" property="comment" />
<result column="creator" jdbcType="VARCHAR" property="creator" />
<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
<result column="modifier" jdbcType="VARCHAR" property="modifier" />
<result column="modify_date" jdbcType="TIMESTAMP" property="modifyDate" />
</resultMap>