mybatis代码生成
自动生成 mybatis代码,特别是sqlmap文件,可以有效的减少dao等代码的重复劳动。eclipse中添加插件: mybatis Generator - http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
添加生成代码的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ibatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN" "http://ibatis.apache.org/dtd/ibator-config_1_0.dtd" >
<ibatorConfiguration>
<classPathEntry
location="/home/xxx/xxx.war/WEB-INF/lib/mysql-connector-java-5.1.6.jar" />
<ibatorContext id="DB2Tables" targetRuntime="Ibatis2Java5">
<!-- 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<!-- 是否生成注释代时间戳
<property name="suppressDate" value="true" />
-->
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/test" userId="test"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="test.model"
targetProject="Test-web">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="test.dao"
targetProject="Test-web">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的类文件以及配置文件 -->
<daoGenerator targetPackage="test.dao" targetProject="Test-web"
type="GENERIC-CI" />
<!-- 避免生成一大堆Example方法,代码看去清晰一些 -->
<table tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- 去除一列,这样 PASSWORD这列就不会生成
<ignoreColumn column="PASSWORD"/>
-->
<!-- columnOverride 将数据库字段对应成javabean里面的字段-->
<columnOverride column="USERID" property="userid" />
<columnOverride column="USERNAME" property="username" />
<columnOverride column="PASSWORD" property="password" />
</table>
</ibatorContext>
</ibatorConfiguration>
在文件上点击鼠标右键,可以看到插件生成的选项,选择即可生成代码
daoGenerator 标签的类型,见如下链接.
http://ibatis.apache.org/docs/tools/abator/configreference/daoGenerator.html
IBATISThe generated DAO objects will conform to the iBATIS DAO framework.
GENERIC-CIThe generated DAO objects will rely only on the SqlMapClient. The SqlMapClient will be supplied by constructor dependency injection.
GENERIC-SIThe generated DAO objects will rely only on the SqlMapClient. The SqlMapClient will be supplied by setter dependency injection.
SPRINGThe generated DAO objects will conform to the Spring DAO framework.
页:
[1]