利用MyBatis Generator 可快速高效生成,Dao,mapper.xml ,实体类以及实体类对应的Example 实类。
关于 MyBatis Generator 来快速实现代码生成,可利用Eclipse装插件,插件下载地址(http://download.csdn.net/download/sinat_27406925/9996352),把里面的文件的jar包复制到eclipse对应的jar包里面,然后重启eclipse,然后按住项目右键 new—>other 找Mybatis 即可,再就是配置generator.xml。
还是来看看怎么配置 generator.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>
<!-- 数据库驱动包位置 -->
<!-- D:\maven\repository\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar-->
<classPathEntry
location="D:\maven\repository\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar" />
<context id="context1">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://数据库地址(自己改):3307/数据库名(自己改)" userId="用户名(自己改)" password="密码(自己改)" />
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.epipe.user.dal.db.po" targetProject="user-impl/src/main/java" />
<!-- 生成的映射文件报名和位置 -->
<sqlMapGenerator targetPackage="com.mapping" targetProject="user-impl/src/main/resources" />
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator targetPackage="com.epipe.user.dal.db.mapper" targetProject="user-impl/src/main/java" type="XMLMAPPER" />
<!-- 要生成的那些表(更改tableName 和domainObjectName 就可以了) -->
<!--
<table schema="ssm1" tableName="FILMINFO" domainObjectName="FilmInfo" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<columnOverride column="FILMID" property="FILMID" />
<columnOverride column="FILMNAME" property="FILMNAME" />
<columnOverride column="TYPEID" property="TYPEID" />
<columnOverride column="ACTOR" property="ACTOR" />
<columnOverride column="DIRECTOR" property="DIRECTOR" />
<columnOverride column="TICKETPRICE" property="TICKETPRICE" />
</table>
-->
<!--
domainObjectName="myuser" enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"
当这些为true时 生成对应的实体类的Example类,如果不想要这些Example类,可以将true 改为false即可
--->
<table tableName="sys_user" domainObjectName="myuser" enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<columnOverride column="TYPEID" property="TYPEID" />
<columnOverride column="TYPENAME" property="TYPENAME" />
</table>
</context>
</generatorConfiguration>
这就是运行后生成的代码。