【MyBatis】MyBatis Generator插件自动生成文件,generatorConfig.xml配置详解

Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,

由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成实体类、DAO接口和Mapping映射文件


1.

MyBatis Generator 插件安装好后,即可配置generatorConfig.xml文件

插件安装参见 http://write.blog.csdn.net/postedit/72850645


2.

拷贝generatorConfig.xml文件到config下


3.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration >
  <!-- 数据库驱动 -->
  <classPathEntry  location="D:/MyCommonApps/mysql-connector-java-5.1.18-bin.jar"/>
  <context id="mybatis" >
  
	<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
	<!-- 使用criteria代替Examples自动生成结尾 如这里会在dao下生成MyInfoCriteria 否则默认的为MyInfoExample-->
	<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
		<property name="searchString" value="Example$" />
		<property name="replaceString" value="Criteria" /> 
	</plugin>
  	<!-- 是否去除自动生成的注释 true:是 : false:否 -->
   	<commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator> 
   	<!-- 数据库链接URL,用户名、密码 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/version?characterEncoding=utf8" userId="root" password="xiazhang" />
   	<!-- 转BigDecimal -->
   	<javaTypeResolver >
   		<property name="forceBigDecimals" value="true" />
   	</javaTypeResolver>
    
    <!-- 生成模型的包名和位置-->
    <javaModelGenerator targetPackage="com.biu.wifi.weekend.dao.model" targetProject="DemoProject">
    	<property name="enableSubPackages" value="true" />  
        <property name="trimStrings" value="true" /> 
    </javaModelGenerator>
    
    <!-- 生成映射文件的包名和位置-->
    <sqlMapGenerator targetPackage="com.biu.wifi.weekend.dao.mappers" targetProject="DemoProject" >
    	<property name="enableSubPackages" value="false" />
    </sqlMapGenerator>
    
    <!-- 生成dao的包名和位置 -->
    <javaClientGenerator targetPackage="com.biu.wifi.weekend.dao" targetProject="DemoProject" type="XMLMAPPER" >
    	<property name="enableSubPackages" value="true" />  
    </javaClientGenerator>
    
    <!-- 表配置  -->
    <table schema="version" tableName="my_test" domainObjectName="MyInfo"
			enableCountByExample="false"  
			enableDeleteByExample="false" 
			enableSelectByExample="false"  
            enableUpdateByExample="false">  
            <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 
			enable*ByExample 是否生成example类   -->  
              
            <!-- 忽略列,不生成bean 字段 -->  
            <ignoreColumn column="name" />  
            <!-- 指定列的java数据类型 -->  
            <columnOverride column="sex" jdbcType="VARCHAR" /> 
	</table>
    
  </context>
</generatorConfiguration>



补充:


<javaModelGenerator targetPackage="com.biu.wifi.weekend.dao.model"
	targetProject="DemoProject">
	<property name="enableSubPackages" value="false" />
	<!-- 表示自动生成类之后自动继承CoreEntity -->
	<property name="rootClass" value="com.biu.wifi.core.base.CoreEntity" />
	<property name="trimStrings" value="true" />
</javaModelGenerator

<javaClientGenerator type="XMLMAPPER"
	targetPackage="com.biu.wifi.weekend.dao" targetProject="DemoProject">
	<property name="enableSubPackages" value="false" />
	<!-- 表示自动生成类之后自动实现接口CoreDao -->
	<property name="rootInterface" value="com.biu.wifi.core.base.CoreDao" />
</javaClientGenerator>


4.注意

(1) 使用时哟去除xml注释,否则报错

(2)  数据库驱动要引入正确 <classPathEntry  location="D:/MyCommonApps/mysql-connector-java-5.1.18-bin.jar"/>

       否则报错:Project jdbc does not exist

(3)  targetProject="你的项目名称" 使用相对路径 

       否则报错:project does not exist



5.自动生成

(1) 在项目中的该配置文件上右击,选择下图选项即可


生成的文件



(2) 使用命令在目录中生成(需要使用到的相关文件及jar包)

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

 
 

在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可




















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值