Mybatis逆向工程之Mybatis Generator的使用

一、引言

对于使用 Mybatis 的开发者来说,绑定 Xml 和 Dao 接口的对应关系,需要在 Mybatis 的全局配置文件中进行配置,每个 Sql 也需要自己来逐一编写。但 Mybatis 也提供了逆向工程来生成这些文件,可以根据 Database 来逆向的生成这些文件,下面来介绍一下。下面附上 Mybatis Generator 的官方文档,也可以参考着看一下。

http://www.mybatis.org/generator/configreference/xmlconfig.html

二、使用

(1)、maven工程下,加入jar的配置,不是maven工程的话,要下载相应的jar包
这里写图片描述

(2)、在项目目录下创建mybatisGenerator.xml配置文件,具体配置如下图:
这里写图片描述这里写图片描述这里写图片描述

(3)、编写mybatisGenerator.xml的测试代码:
这里写图片描述

运行上面的测试代码,就可以逆向生成 Mybatis 的实体类、Dao接口文件和mapper文件,但此方式生成的文件并不完整,可以根据功能的需要对文件进行改写。

三、详细代码

(1)、mybatisGenerator.xml代码

<?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>

  <context id="DB2Tables" targetRuntime="MyBatis3">
	<!--生成实例、dao、mapper时,不产生注释-->
	<commentGenerator>
	 	<property name="suppressAllComments" value="true" />
	</commentGenerator>
<!--   driverClass="com.mysql.jdbc.Driver" -->
<!--   connectionURL="jdbc:mysql://localhost:3306/ssm_crud" -->
<!--   userId="root" -->
<!--   password="root"> -->
    <jdbcConnection 
    	driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://124.207.3.80:3308/ble"
        userId="ble"
        password="b8w_&amp;8UaW21%,8">
    </jdbcConnection>

    <javaTypeResolver >
      	<property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

	<!-- 指定javaBean生成的位置 -->
    <javaModelGenerator 
    	targetPackage="com.scorpios.bean" 
    	targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

	<!-- 指定映射文件生成的位置 -->
    <sqlMapGenerator 
    	targetPackage="mapper"  
    	targetProject=".\src\main\resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

	<!-- 指定dao接口生成的位置,mapper接口 -->
    <javaClientGenerator type="XMLMAPPER" 
    	targetPackage="com.scorpios.dao"  
    	targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

	<!-- table指定每个表的生成策略 -->
<!--     <table tableName="tbl_emp" domainObjectName="Employee" ></table> -->
<!--     <table tableName="tbl_dept" domainObjectName="Department" ></table> -->
<!--     <table tableName="ctpass_app_relation_info" domainObjectName="CtpassAppRelationInfo" ></table> -->
<!--     <table tableName="ctpass_app_key_info" domainObjectName="CtpassAppKeyInfo" ></table> -->
<!--     <table tableName="ctpass_mobile_info" domainObjectName="CtpassMobileInfo" ></table> -->
<!--     <table tableName="mobile_temp" domainObjectName="MobileTemp" ></table> -->
<!--     <table tableName="par_area" domainObjectName="ParArea" ></table> -->
<!--     <table tableName="par_area_country" domainObjectName="ParAreaCountry" ></table> -->
<!--     <table tableName="par_sequence" domainObjectName="ParSequence" ></table> -->
<!--     <table tableName="sys_third_access_config" domainObjectName="SysThirdAccessConfig" ></table> -->
<!--     <table tableName="user_cert_detail" domainObjectName="UserCertDetail" ></table> -->
    <table tableName="user_cert_his" domainObjectName="UserCertHis" ></table>

  </context>
</generatorConfiguration>
(2)、测试代码
public class MybatisGeneratorTest {

	public static void main(String[] args) throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("mybatisGenerator.xml");
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(configFile);
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		myBatisGenerator.generate(null);
	}

}

(3)、目录结构图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

止步前行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值