使用Springboot+Mybatis+Mysql进行演示,使用maven导入Jar包,开发工具STS3.9
新建一个项目,使用springboot可以快速搭建出一个开发框架,项目搭建基本配置如下:![在这里插入图片描述](https://img-blog.csdnimg.cn/20181116163811565.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdURSYWlu,size_16,color_FFFFFF,t_70)
POM.XML 配置
4.0.0
com.lidong
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
5.1.26
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
druid
1.1.0
org.mybatis.generator
mybatis-generator-core
1.3.5
system
${project.basedir}/lib/mybatis-generator.jar
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.5
log4j
log4j
1.2.17
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.5
org.mybatis.generator
mybatis-generator-core
1.3.6
mysql
mysql-connector-java
5.1.34
com.xxg
mybatis-generator-plugin
1.0.0
false
true
pom.xml文件最重要的需要jar包是![在这里插入图片描述](https://img-blog.csdnimg.cn/20181116162045146.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdURSYWlu,size_16,color_FFFFFF,t_70)
基本配置已经完成了,但是需要使用generator还需要编写generatorConfig.xml。为了是能让代码读取到相关配置,如数据库连接、代码生成策略。
demo:
现在数据库里已经有三个表,user、dept、kpi,分别表示,用户、部门和kpi。用户和部门是一对一关系,用户和kpi是一对多关系。 user表: dept表: kpi表: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181127131308171.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdURSYWlu,size_16,color_FFFFFF,t_70)
因为在原生的mybatis-generator中没有一对一和一对多的关联关系的配置,说不反向生成代码的时候不会产生关联关系,不过感谢大神@半导体郗 编写的jar包在原生的基础上添加了一对一、一对多和关联添加、更新。下面我详细说一下使用。
generatorConfig.xml
这部分是反向生成,运行时需要读取的配置,其中properties resource标签里是你的数据库连接的储存位置。 在springboot中可以有两种方式写配置文件,一种是.properties,另一种是.yml。两种方式都可以,但是.properties的优先级高于yml。在加载过程先加载yml,后加载.properties,如果里面有相同的属性配置,则后者覆盖前者,所以优先级越高,就最后才加载。一般情况下都是会使用一种方式,我是在做练习两种方式都尝试一下。
不过在数据库连接的配置方面,generator只能读取.properties的配置,不能读取.yml中的配置。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181116163515830.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdURSYWlu,size_16,color_FFFFFF,t_70)
GeneratorMain.java
package com.lidong.demo;
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;
public class GeneratorMain {
public static void main(String[] args) {
List warnings = new ArrayList();
boolean overwrite = true;
String genCfg = "/generatorConfig.xml";
File configFile = new File(GeneratorMain.class.getResource(genCfg).getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("代码反向生成完毕-----");
}
}
运行上面代码。 刷新一下在你之前配置文件中设置好的生成文件的存放位置,就出现生成的代码。
User对象 **
Dept对象: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181127131530477.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdURSYWlu,size_16,color_FFFFFF,t_70)
Kpi对象 UserMapper.xml
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181127131753537.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdURSYWlu,size_16,color_FFFFFF,t_70)
关键词作用association一对一collection一对多
在以上两个关联关系中采用的方式是select方法进行映射。所以在进行关联映射的时候需要两个表一起配置
重点
generatorConfig.xml中的一对一、一对多type 插件是@半导体郗 自己编写的,需要自己导入到项目中。本着学习原则,免费分享给大家。 链接: https://pan.baidu.com/s/1ffTsh7FkBXjehOk1xv15YA 提取码: 4vy3generatorConfig.xml中的table下的生成的方法按需求添加。进行关联查询的时候配置文件映射到对象上,但是关联插入的时候不能,只能先保存dept对象,在保存user对象,最后保存kpi对象。在配置文件中标签出现编译器报错,提示没有是正常的,但是并不影响使用,先试一下能否运行成功。
其他可以关注微信公众号:马拉松程序员
|