逆向工程
-
项目结构
-
依赖
<dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.7</version>
</dependency>
- mgb.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><!-- targetRuntime创建一个myBatis容器 有MyBatis3Simple(简单版的CURD)和MyBatis3带有动态sql --><context id="DB2Tables" targetRuntime="MyBatis3Simple"><!-- 指定如何连接目标数据库 --><jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"userId="root"password="8888.216"></jdbcConnection><!-- 类型解析器 是否强制转换forceBigDecimals --><javaTypeResolver ><property name="forceBigDecimals" value="false" /></javaTypeResolver><!--javaModelGenerator:指定javaBean的生产策略--><!-- 定义java模型属性 形成javabean targetPackage目标包--><!-- targetProject 目标工程--><javaModelGenerator targetPackage="com.atguigu.mybatis.bean" targetProject=".\src"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!-- 定义sql映射 --><!--sqlMapGenerator sql映射生产策略--><sqlMapGenerator targetPackage="com.atguigu.mybatis.dao"targetProject=".\resources"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- javaClientGenerator 指定Mapper的接口所在的位置 --><javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao" targetProject=".\src"><property name="enableSubPackages" value="true" /></javaClientGenerator><!--table 指定逆向分析那些表,根据表创建javabean(Departmanet) --><table tableName="tb1_dept" domainObjectName="Department"></table><table tableName="tb1_employee" domainObjectName="Employee"></table></context>
</generatorConfiguration>
- java
@Testpublic void mybatisGeneratorTest() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {List<String> warnings = new ArrayList<String>();boolean overwrite = true;ConfigurationParser cp = new ConfigurationParser(warnings);Reader reader = Resources.getResourceAsReader("mgb.xml");Configuration config = cp.parseConfiguration(reader);DefaultShellCallback callback = new DefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);myBatisGenerator.generate(null);}