Mybatis———逆向工程
简单点说,就是通过数据库中的表单,自动生成java代码,省去了自己写xml的部分代码。Mybatis官方提供了逆向工程,可以针对单表自动生成mybatis代码(mapper.java\mapper.xml\po类)。
下载jar包
创建Java项目
导入jar包
将主要jar包导入lib里,Build Path
创建核心代码
在下载的jar包下找到
mybatis-generator-core-1.3.2——>docs——>index.html
复制这是Generator.java代码
这是generator.xml代码
配置文件代码
log4j.properties
1
2
3
4
5
6
7
8
9log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
database.properties
1
2
3
4jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/shop?useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
generator.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47<?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="testTables" targetRuntime="MyBatis3">
<!-- 序列化model -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<commentGenerator>
<!-- 是否去除自动生成的注解true:是 false:否 -->
<!-- <property name="suppressDate" value="false"/> -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/shop?useSSL=false&serverTimezone=UTC"
userId="root"
password="root"/>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="net.zzqd.mybatisshop.pojo"
targetProject=".\src">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="net.zzqd.mybatisshop.mapper"
targetProject=".\src">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="net.zzqd.mybatisshop.mapper"
targetProject=".\src" >
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table schema="" tableName="category"></table>
<table schema="" tableName="orderitem"></table>
<table schema="" tableName="orders"></table>
<table schema="" tableName="product"></table>
<table schema="" tableName="user"></table>
</context>
</generatorConfiguration>
Generator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35package net.zzqd.generator;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
/**
* @author zzq
* @version 创建时间:2019年6月4日 下午1:23:06
*
*/
public class Generator {
public static void main(String[] args) throws Exception, InterruptedException
{
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);
}
}
运行
运行Generator.java文件
运行结果