`
hacker_zxf
  • 浏览: 143910 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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
IBATIS	The generated DAO objects will conform to the iBATIS DAO framework.
GENERIC-CI	The generated DAO objects will rely only on the SqlMapClient. The SqlMapClient will be supplied by constructor dependency injection.
GENERIC-SI	The generated DAO objects will rely only on the SqlMapClient. The SqlMapClient will be supplied by setter dependency injection.
SPRING	The generated DAO objects will conform to the Spring DAO framework.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics