MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类、Dao接口类甚至Mapping映射文件。

生成代码需要的文件和jar包:

(上图文件下载地址:http://download.csdn.net/detail/u012909091/7206091

其中有mybatis框架的jar包,数据库驱动程序jar包以及MyBatis生成器jar包。其中的generatorConfig.xml是需要我们来配置的文件,配置如下:

 

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <!DOCTYPE generatorConfiguration  
 3   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
 4   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
 5 <generatorConfiguration>  
 6 <!--数据库驱动-->
 7     <classPathEntry  location="mysql-connector-java-5.1.25-bin.jar"/>  
 8     <context id="DB2Tables"  targetRuntime="MyBatis3">  
 9         <commentGenerator>  
10             <property name="suppressDate" value="true"/>  
11             <!--是否去除自动生成的注释 true:是 : false:否 -->
12             <property name="suppressAllComments" value="true"/>  
13         </commentGenerator>  
14         <!--数据库链接URL,用户名、密码 -->
15         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.7.201:12340/program_shopcart" userId="sa" password="1234">  
16         </jdbcConnection>  
17         <javaTypeResolver>  
18             <property name="forceBigDecimals" value="false"/>  
19         </javaTypeResolver>  
20         <!--生成模型的包名和位置-->
21         <javaModelGenerator targetPackage="wyp.db.test.domain" targetProject="src">  
22             <property name="enableSubPackages" value="true"/>  
23             <property name="trimStrings" value="true"/>  
24         </javaModelGenerator>  
25         <!--生成映射文件的包名和位置-->
26         <sqlMapGenerator targetPackage="wyp.db.test.mapping" targetProject="src">  
27             <property name="enableSubPackages" value="true"/>  
28         </sqlMapGenerator>  
29         <!--生成DAO的包名和位置-->
30         <javaClientGenerator type="XMLMAPPER" targetPackage="wyp.db.test.IDao" targetProject="src">  
31             <property name="enableSubPackages" value="true"/>  
32         </javaClientGenerator>  
33         <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
34         <table tableName="account_group" domainObjectName="Account_group" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
35         <table tableName="account_grouppermissionassign" domainObjectName="Account_grouppermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
36         <table tableName="account_permission" domainObjectName="Account_permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
37         <table tableName="account_role" domainObjectName="Account_role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
38         <table tableName="account_rolepermissionassign" domainObjectName="Account_rolepermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
39         <table tableName="account_user" domainObjectName="Account_user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
40         <table tableName="account_usergroupassign" domainObjectName="Account_usergroupassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
41         <table tableName="account_userpermissionassign" domainObjectName="Account_userpermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
42         <table tableName="account_userroleassign" domainObjectName="Account_userroleassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
43     </context>  
44 </generatorConfiguration>  
generatorConfig.xml

 

 当以上这些完成之后,只需要打开控制台,进入lib目录下,执行脚本:

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

 即可。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值