1、Mybatis 逆向工程是什么?
MyBatis Generator
简称mbg,是一个专门为Mybatis框架使用者定制的代码生成器,可以快速的根据表生成对应的
映射文件、接口和bean类。支持基本的增删改查,以及QBC(Query By Criteria)风格的条件查询。
2、如何使用
首先说一下,以下配置必须放在一个完整的mybatis项目下,别犯傻哈!
(即引入了mybatis相关的jar包和 具备mybatis全局配置文件)
1)导入mybatis逆向工程相应的jar包
2)编写全局配置文件mbg.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> <!-- targetRuntime="MyBatis3Simple":生成简单版的CRUD MyBatis3:豪华版 --> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- jdbcConnection:指定如何连接到目标数据库 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true" userId="root" password="houchen"> </jdbcConnection> <!-- --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- javaModelGenerator:指定javaBean的生成策略 targetPackage="test.model":目标包名 targetProject="MBGTestProjectsrc":目标工程 --> <javaModelGenerator targetPackage="com.atguigu.mybatis.bean" targetProject=".src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- sqlMapGenerator:sql映射生成策略: --> <sqlMapGenerator targetPackage="com.atguigu.mybatis.dao" targetProject=".conf"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- javaClientGenerator:指定mapper接口所在的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao" targetProject=".src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 指定要逆向分析哪些表:根据表要创建javaBean --> <table tableName="tbl_dept" domainObjectName="Department"></table> <table tableName="tbl_employee" domainObjectName="Employee"></table> </context> </generatorConfiguration>
<?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> <!-- targetRuntime="MyBatis3Simple":生成简单版的CRUD MyBatis3:豪华版 --> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- jdbcConnection:指定如何连接到目标数据库 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true" userId="root" password="houchen"> </jdbcConnection> <!-- --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- javaModelGenerator:指定javaBean的生成策略 targetPackage="test.model":目标包名 targetProject="MBGTestProjectsrc":目标工程 --> <javaModelGenerator targetPackage="com.atguigu.mybatis.bean" targetProject=".src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- sqlMapGenerator:sql映射生成策略: --> <sqlMapGenerator targetPackage="com.atguigu.mybatis.dao" targetProject=".conf"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- javaClientGenerator:指定mapper接口所在的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao" targetProject=".src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 指定要逆向分析哪些表:根据表要创建javaBean --> <table tableName="tbl_dept" domainObjectName="Department"></table> <table tableName="tbl_employee" domainObjectName="Employee"></table> </context> </generatorConfiguration>
3) 测试逆向工程
@Test public void testMybatisGenerate() throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("mbg.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); }
确实生成了相应的实体、接口和 mapper: