MBG-逆向工程
一、介绍
MBG:MyBatis Generator:代码生成器;
MyBatis官方提供的代码生成器;帮我们逆向生成;
正向:
table----javaBean---BookDao---dao.xml---xxx
逆向工程:
根据数据表table,逆向分析数据表,自动生成javaBean---BookDao---dao.xml---xxx
二、配置
1、导包:mbg的核心包
….. mybatis-generator-core-1.3.2.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> <!-- MyBatis3Simple:基础班CRUD MyBatis3:复杂版CRUD --> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- jdbcConnection:指导连接到哪个数据库 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis_0325" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- javaModelGenerator:生成pojo targetPackage:生成的pojo放在哪个包 targetProject:放在哪个工程下 --> <javaModelGenerator targetPackage="com.atguigu.bean" targetProject=".src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--sqlMapGenerator:sql映射文件生成器;指定xml生成的地方 --> <sqlMapGenerator targetPackage="com.atguigu.dao" targetProject=".conf"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- javaClientGenerator:dao接口生成的地方 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.dao" targetProject=".src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- table:指定要逆向生成哪个数据表 tableName="t_cat":表名 domainObjectName="":这个表对应的对象名 --> <table tableName="t_cat" domainObjectName="Cat"></table> <table tableName="t_employee" domainObjectName="Employee"></table> <table tableName="t_teacher" domainObjectName="Teacher"></table> </context> </generatorConfiguration>
三、测试
public class MBGTest { public static void main(String[] args) 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); System.out.println("生成ok了!"); } }