Mybatisplus代码⽣成之SpringBoot适配MYSQL和ORACLE Mybatisplus代码⽣成 之SpringBoot适配MYSQL和ORACLE
⽂章⽬录
前期准备:
MYSQL数据库表
CREATE TABLE `tbl_employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last_name` varchar(50) COLLATE utf8_bin DEFAULT NULL,
`email` varchar(50) COLLATE utf8_bin DEFAULT NULL,
`gender` char(1) COLLATE utf8_bin DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
测试数据
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom','tom@atguigu',1,22);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Jerry','jerry@atguigu',0,25);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Black','black@atguigu',1,30);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('White','white@atguigu',0,35)
user
CREATE TABLE `user` (
`id` bigint(20) NOT NULL COMMENT '主键ID',
`name` varchar(30) COLLATE utf8_bin DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
测试数据
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou'),
(2, 'Jack', 20, 'test2@baomidou'),
(3, 'Tom', 28, 'test3@baomidou'),
(4, 'Sandy', 21, 'test4@baomidou'),
(5, 'Billie', 24, 'test5@baomidou');
看图操作,⼀路下⼀步
搭建SpringBoot基础环境完成
1. 导⼊相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gblfy</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis-plus-generator</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding> <java.version>1.8</java.version>
<mybatisplus.version>3.1.2</mybatisplus.version>
<oracle.version>11.2.0.3</oracle.version>
</properties>
<dependencies>
<!--mybatis-plus Start-->
mysql下载jar包<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<!--mybatis-plus End-->
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--MYSQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- ORACLE数据库驱动-->
<dependency>
<groupId&acle</groupId>
<artifactId>ojdbc6</artifactId>
<artifactId>ojdbc6</artifactId>
<version>${oracle.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. 新建代码⽣成类MysqlGenerator
2.1. MYSQL 代码⽣成部分
package ator;
import batisplus.annotation.DbType;
import batisplus.annotation.IdType;
import lkit.StringPool;
import ator.AutoGenerator;
import ator.InjectionConfig;
import fig.*;
import fig.po.TableInfo;
import fig.rules.NamingStrategy;
import ine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* mysql 代码⽣成器演⽰例⼦
* </p>
*
* @author gblfy
* @since 2019-07-12
*/
public class MysqlGenerator {
/*************************************MYSQL代码⽣成 Start *************************************/
// 全局配置
private final static String OUTPUT_XML_DIR = "/src/main/resources";// ⽣成xml⽂件的输出⽬录
private final static String AUTHOR = "gblfy";// 开发⼈员
// 数据源配置
private final static String MYSQL_DRIVER_NAME = "sql.cj.jdbc.Driver";// MYSQL数据库驱动 private final static String DATABASE_IP = "localhost";// 数据库ip
private final static String MYSQL_DATABASE_NAME = "mp";// 数据库名
private final static String MYSQL_DB_USERNAME = "root";// 数据库⽤户
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论