首页 > 代码库 > Ibatis自动生成dao sqlmapper文件和domain文件过程

Ibatis自动生成dao sqlmapper文件和domain文件过程

generator自动生成mybatis的xml配置、model、map等信息: 
1、下载mybatis-generator-core-1.3.2.jar包。 
       网址:http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DGenerator,下载mybatis-generator-core-1.3.2-bundle.zip,解压 
       找到lib下的需要jar包。 
2、新建工程项目
 
3、编写genertor的xml文件,名下:generator.xml
 
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
  3. <generatorConfiguration>
  4. <classPathEntry location="D:\apache-tomcat-7.0.52\webapps\traceSys\WEB-INF\lib\mysql-connector-java-5.1.29.jar" />
  5. <context id="context1">
  6. <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/trace_sys" userId="root" password="" />
  7. <javaModelGenerator targetPackage="com.trace.domain"
  8. targetProject="D:\cpn-work\traceSys\src\main\java" />
  9. <sqlMapGenerator targetPackage="com.trace.dao" targetProject="D:\cpn-work\traceSys\src\main\java"></sqlMapGenerator>
  10. <javaClientGenerator targetPackage="com.trace.dao"
  11. targetProject="D:\cpn-work\traceSys\src\main\java" type="XMLMAPPER" />
  12. <table tableName="company" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
  13. <table tableName="goods" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
  14. <table tableName="goods_discuss" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
  15. <table tableName="goods_production" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
  16. <table tableName="goods_profile" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
  17. <table tableName="goods_record" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
  18. <table tableName="type" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
  19. <table tableName="user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
  20. </context>
  21. </generatorConfiguration>
table其他属性: 
enableCountByExample="false" 
enableUpdateByExample="false" 
enableDeleteByExample="false" 
enableSelectByExample="false" 
selectByExampleQueryId="false" 
schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类, 
如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true, 
这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时, 
就不会生成对应的Example类了. 



 
4、命令行方式生成
  1. java -jar E:\Websoft\mybaits\mybatis-generator-core-1.3.2\lib\mybatis-generator-core-1.3.2.jar -configfile E:\WebWorkSpace\workspace_js\downAttachdemo\src\com\mochasoft\down\generator.xml -overwrite
成功时输出:MyBatis Generator finished successfully. 




 


来自为知笔记(Wiz)


Ibatis自动生成dao sqlmapper文件和domain文件过程