概述
mybatis的逆向工程是通过数据库已经存存在的数据表,反向生成java中的实体类(生成对应的ORM持久层代码)
流程
代码生成器作为商城项目的组件存在,maven项目不使用模板创建
- 1.引入逆向工程依赖
- mysql-connector-core-x.x.xx.jar
- mybatis-generator-core-1.4.0.jar
到http://mvnrepository.com搜索依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
- 2.配置生成规则
- 逆向工程生成规则,就是描述数据库中的那些表,生成对应的java中的实体类,同时生成映射配置文件。这个生成规则就是一个普通的配置文件
- 在项目主目录中创建一个配置文件generator.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="DBTables" targetRuntime="MyBatis3">
<!--配置数据库的链接信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/xiaomi?useUnicode=true&characterEncoding=UTF-8&severTimezone=PRC"
userId="root"
password="123456"/>
<!--数据库BigDecimals字段在java中定义-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--实体类生成信息-->
<javaModelGenerator targetPackage="edu.yjsj.entry" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--mybatis 的mapper文件地址-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--mybatis的mapper接口-->
<javaClientGenerator targetPackage="edu.yjsj.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--要生成的表结构-->
<table tableName="consumer" domainObjectName="Consumer"/>
<table tableName="goods" domainObjectName="Goods"/>
<table tableName="goods_cart" domainObjectName="GoodsCarts"/>
<table tableName="goods_configure" domainObjectName="GoodsConfigure"/>
<table tableName="goods_images" domainObjectName="GoodsImages"/>
<table tableName="goods_order" domainObjectName="GoodsOrder"/>
<table tableName="goods_order_item" domainObjectName="GoodsOrderItem"/>
<table tableName="goods_package" domainObjectName="GoodsPackage"/>
<table tableName="goods_service" domainObjectName="GoodService"/>
<table tableName="goods_shipping_address" domainObjectName="GoodsShippingAdress"/>
<table tableName="goods_type" domainObjectName="GoodsType"/>
</context>
</generatorConfiguration>
- 3.逆向工程
使用官方文档提供的java代码生成需要的文件
mybatis官网:http://mybatis.org/generator/
package edu.yjsj.utils;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Genarator {
public static void main(String[] args) throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generator.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);
}
}
结果