Mybatis-generator应用实例

1 核心文件
generator.xml指定数据库jar包位置、数据库连接信息、生成包的位置、表名等关键信息。该文件放在任意位置。

<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  <!-- 数据库的JDBC驱动的jar包地址 -->
  <classPathEntry location="F:\xy\jars\mysql-connector-java-5.0.7-bin.jar" />
  
  <context id="DB2Tables" targetRuntime="MyBatis3">
 <!-- 是否去除自动生成的注释 -->
 <commentGenerator>
<property name="suppressAllComments" value="true" />
 </commentGenerator>
 
 <!-- 数据库连接的信息 -->
 <jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/db_MybatisTest" 
userId="root" password="mysqltest">
 </jdbcConnection>
 
 <!-- false:JDBC DECIMAL、NUMERIC类型解析为Integer,默认方式 -->
 <!-- true: JDBC DECIMAL、NUMERIC类型解析为java.math.BigDecimal -->
 <javaTypeResolver>
<property name="forceBigDecimals" value="false" />
 </javaTypeResolver>
 
 <!-- 生成模型的包名和位置 -->
 <javaModelGenerator targetPackage="com.xy.model" targetProject="F:\xy\mybatis-generator\src">
 <!-- 是否让schema作为包的后缀 -->
 <property name="enableSubPackages" value="true" />
 <!-- 从数据库返回的值被清理前后的空格 -->
 <property name="trimStrings" value="true" />
 </javaModelGenerator>
 
 <!-- 生成映射文件的包名和位置 -->
 <sqlMapGenerator targetPackage="com.xy.mapping"  targetProject="F:\xy\mybatis-generator\src">
  <property name="enableSubPackages" value="false" />
 </sqlMapGenerator>
 
 <!-- 生成DAO的包名和位置 -->
 <javaClientGenerator type="XMLMAPPER" targetPackage="com.xy.dao" targetProject="F:\xy\mybatis-generator\src">
<property name="enableSubPackages" value="true" />
 </javaClientGenerator>

 <!-- tableName:数据库表 -->
 <!-- domainObjectName:对应于数据库表的javaBean类名 -->
 <table tableName="t_student" domainObjectName="Student" enableCountByExample="false" 
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"  selectByExampleQueryId="false">
<!-- 忽略该字段(可省略) -->
<ignoreColumn column="name" />
 </table>
</context>
</generatorConfiguration>

2 标签说明
(1) 属性
schema即为数据库名,tableName为对应的数据库表,domainObjectName是要生成的实体类。
若要生成例子可将enableCountByExample等设为true, 就会生成一个对应domainObjectName的Example类,false则不生成,默认策略是true。
类似的还有enableUpdateByExample、enableDeleteByExample、enableSelectByExample、selectByExampleQueryId属性。

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

3 生成
mybatis-generator-core-1.3.2.jar是核心jar包,可在网上自行下载。命令窗口执行语句,执行成功后就会在generator.xml文件中指定的位置找到代码了。
java -jar F:\xy\jars\mybatis-generator-core-1.3.2.jar -configfile F:\xy\generator.xml -overwrite

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值