Maven Web项目使用MyBatis_Generator_1.3.1自动生成javabean,dao,mapper.xml代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21891465/article/details/68954983
一. 说明:
Maven Web项目使用MyBatis_Generator_1.3.1自动生成javabean,dao,mapper.xml代码
二. 准备:MyBatis_Generator相关jar
MyBatis_Generator和项目源码百度网盘下载: http://pan.baidu.com/s/1boUJhW7
三. 在myeclipse中引入MyBatis_Generator插件
1.找到myeclipse安装目录下的features和plugins文件夹,如果没有则新建。

2.将下载的MyBatis_Generator里面对应的文件家里面的jar包拷贝到对应的myeclipse文件夹下
3.重启myeclipse在某个项目上右键->新建->other 里面,能看到已经加入了插件如下图

四.使用插件生成代码
1.继续上述操作,点击next在项目下新建一个generatorConfiguration.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 >
<context id="context1" >
<jdbcConnection driverClass="???" connectionURL="???" userId="???" password="???" />
<javaModelGenerator targetPackage="???" targetProject="???" />
<sqlMapGenerator targetPackage="???" targetProject="???" />
<javaClientGenerator targetPackage="???" targetProject="???" type="XMLMAPPER" />
<table schema="???" tableName="???" >
<columnOverride column="???" property="???" />
</table>
</context>
</generatorConfiguration>
5.配置generatorConfiguration.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 >
<context id="context1" >
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/kiboy-maven" userId="root" password="root" />
<!-- 实体类文件生成位置 -->
<javaModelGenerator targetPackage="com.luo.domain" targetProject="kiboy-maven/src/main/java" />
<!-- mapper.xml文件位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="kiboy-maven/src/main/resources" />
<!-- dao文件生成位置 -->
<javaClientGenerator targetPackage="com.luo.dao" targetProject="kiboy-maven/src/main/java" type="XMLMAPPER" />
<!-- domainObjectName对应的POJO的类名,其它true,false去掉一些生成example无用的代码 -->
<table tableName="t_user" domainObjectName="TUser" enableInsert="true"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false"
enableUpdateByExample="false">
<!-- 下面是需要在实体类里面生成的字段 -->
<columnOverride column="id" property="userId" />
<columnOverride column="user_name" property="userName" />
<columnOverride column="user_password" property="userPassword" />
<columnOverride column="address" property="address" />
<columnOverride column="age" property="age" />
</table>
<table tableName="student" domainObjectName="Student" enableInsert="true"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false"
enableUpdateByExample="false">
<columnOverride column="id" property="id" />
<columnOverride column="password" property="password" />
<columnOverride column="sex" property="sex" />
<columnOverride column="idcard" property="idcard" />
</table>
<table tableName="fee" domainObjectName="Fee" enableInsert="true"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false"
enableUpdateByExample="false">
</table>
<table tableName="bookcost" domainObjectName="Bookcost" enableInsert="true"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false"
enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>


2.配置好后右键配置文件点击 Generate MyBatis/iBATIS Artifacts自动生成代码如下


注意:发现实体类多了一个FeeKey.java文件,这是因为fee表里面设置的是两个字段的关联主键。
并且如果再执行一次生成操作,这些java文件就会报错,因为,再次生成对文件做的是追加操作,
所以会有重复方法和变量,就会报错,通过配置文件来配置的解决方式目前还没有找到,百度不到啊(┬_┬)!!!,所以就需要先删除原有的代码文件
3.如有不合理之处,欢迎提意见!此篇完结!

猜你喜欢

转载自blog.csdn.net/qq_21891465/article/details/68954983