Mybatis入门学习篇(一)之Mybatis Generator使用


转载:http://blog.csdn.net/u013769320/article/details/42319631

最近在做一个项目,因为已经把数据库建好了,再一一写model,mapper的话,就显得重复了,于是上网百度,发现可以使用Mybatis Genetator反向生成model,mapper。经实验,确实不错,记录下来,以备后用。

需要的工具

src文件夹存放生成的model,mapper代码;

config.xml配置文件:指定了一系列的属性,如需要使用的数据库表,数据库连接需要的信息,代码生成规范等;

mybatis-generator-core-1.3.2.jar是Mybatis Generator必需的jar包(generator生成器core核心包)

还需要的jar文件即数据库驱动,如这里的Mysql驱动和SQL Server驱动


配置文件

[html]  view plain  copy
  1. <span style="font-size:12px;"><?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.      <span style="color:#ff6600;">
  8.       Sql Server</span>的驱动
  9. -->  
  10.     <classPathEntry  location="sqljdbc4.jar"/>  
  11.     <context id="DB2Tables"  targetRuntime="MyBatis3">  
  12.         <commentGenerator>  
  13.             <property name="suppressDate" value="true"/>  
  14.             <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
  15.             <property name="suppressAllComments" value="true"/>  
  16.         </commentGenerator>  
  17.         <!--数据库驱动,URL,用户名、密码 -->  
  18.         <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://192.168.1.101:1433; DatabaseName=Contract" userId="root" password="root">  
  19.         </jdbcConnection>  
  20.         <javaTypeResolver>  
  21.             <property name="forceBigDecimals" value="false"/>  
  22.         </javaTypeResolver>  
  23.         <!-- 生成model的包名和位置 ,包名应该和工程保存一致,不然等下导入代码时,会报错-->  
  24.         <javaModelGenerator targetPackage="com.<span style="font-family: Arial, Helvetica, sans-serif;">mine</span>.model" targetProject="src">  
  25.             <property name="enableSubPackages" value="true"/>  
  26.             <property name="trimStrings" value="true"/>  
  27.         </javaModelGenerator>  
  28.         <!-- 生成映射文件的包名和位置-->  
  29.         <sqlMapGenerator targetPackage="com.mine.mapper" targetProject="src">  
  30.             <property name="enableSubPackages" value="true"/>  
  31.         </sqlMapGenerator>  
  32.         <!-- 生成DAO的包名和位置-->  
  33.         <javaClientGenerator type="XMLMAPPER" targetPackage="com.mine.mapper" targetProject="src">  
  34.             <property name="enableSubPackages" value="true"/>  
  35.         </javaClientGenerator>  
  36.   
  37.         <!-- 要生成哪些表-->  
  38.         <table schema="zjh" tableName="example" domainObjectName="Example" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">  
  39. <ignoreColumn column="name" />  
  40. <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />  
  41. </table>    
  42. </context>  
  43. </generatorConfiguration></span>  

table标签详解

1. 属性

schema即为数据库名,tableName为对应的数据库表,domainObjectName是要生成的实体类。若要生成例子可将enableCountByExample等设为true, 就会生成一个对应domainObjectName的Example类,false则不生成,默认策略是true。

类似的还有enableUpdateByExample、enableDeleteByExample、enableSelectByExample、selectByExampleQueryId属性
2. 子标签
若要对某些数据库字段进行操作,可以在table标签中加入如下标签
1)忽略某个字段 
<ignoreColumn column="name" />
2)无论数据库字段是何类型,生成的类属性都是varchar
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />

生成代码

打开windows下的Dos窗口或者Linux下的Terminal窗口,使用cd指令进入到图示目录,然后输入指令:java -jar ./mybatis-generator-core-1.3.2.jar -configfile ./config.xml -overwrite

Ok,到此结束。


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值