说明
本文代码基于 5.基于 JWT(Json web token) 验证权限 项目代码进行修改,不过基本没关系
开发环境
名称 | 版本 |
---|---|
操作系统 | Windows 10 X64 |
JDK | JDK1.8(jdk-8u151-windows-x64) |
IntelliJ IDEA | IntelliJ IDEA 2018.3 |
Maven | Maven 3.6.0 |
根据开发文档创建表
1.开发文档
2.pom
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
3.Java 代码
public static void main(String[] args) {
ExcelReader reader = ExcelUtil
.getReader(FileUtil.file("C:\\Users\\luoma\\Desktop\\融资贷款\\融资数据表v1.1.xlsx"),
4);//配置路径和sheet页
int startRow = 202;//表名那行
int endRow = 218;//逻辑删除那行
List<List<Object>> read = reader.read(startRow-1,endRow-1);
String text = "CREATE TABLE `" + read.get(0).get(0) + "` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键ID',";
for (List<Object> o : read){
if (o.equals(read.get(0)) || o.equals(read.get(1)) || o.equals(read.get(2))){
continue;
}
if ("VARCHAR".equalsIgnoreCase(String.valueOf(o.get(1))) || "text".equalsIgnoreCase(String.valueOf(o.get(1)))){
String b = "`" + o.get(0) + "`" + " " + o.get(1) + "(" + o.get(2) + ") " + "COLLATE utf8mb4_bin DEFAULT NULL COMMENT " + "'" + o.get(3) + "',";
text += b;
}else if ("datetime".equalsIgnoreCase(String.valueOf(o.get(1))) || "date".equalsIgnoreCase(String.valueOf(o.get(1)))){
String b = "`" + o.get(0) + "`" + " " + o.get(1) + " " + "DEFAULT NULL COMMENT " + "'" + o.get(3) + "',";
text += b;
}else {
String b = "`" + o.get(0) + "`" + " " + o.get(1) + "(" + o.get(2) + ") " + "DEFAULT NULL COMMENT " + "'" + o.get(3) + "',";
text += b;
}
}
text += "PRIMARY KEY (`id`) USING BTREE,\n"
+ " KEY `tenant_id` (`tenant_id`) USING BTREE\n"
+ ") ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC COMMENT='" + read.get(0).get(0) + "';";
System.err.println(text);
}
Mybatis plus 代码生成器
测试表-t_rbt_test
创建子模块-test-invoice-generator
test-invoice-cloud
上右键,新建模块
左侧面板选择 Maven
,不勾选 Create from archetype
选项,如下图,点击 下一个
即可。
ArtifactId test-invoice-common
模块名称 test-invoice-common
创建类和文件
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>test-invoice-cloud</artifactId>
<groupId>com-test-invoice</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-invoice-generator</artifactId>
<name>test-invoice-generator</name>
<dependencies>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<!--<version>2.3.20</version>-->
<version>2.3.27-incubating</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
App.java
package com.test.invoice;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 代码生成类
*
* @author:
* @version:
* @date: 2019-10-15 19:43
*/
public class App {
/*
代码生成路径
*/
private static final String outdir="D:\\finance-generator-framework-code\\";
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new RuntimeException("请输入正确的" + tip + "!");
}
/**
* RUN THIS
*/
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
//String projectPath = System.getProperty("user.dir");
//gc.setOutputDir(projectPath + "/finance-generator-exp/src/main/java");
gc.setOutputDir(outdir);
gc.setAuthor("luoma");//作者
gc.setOpen(false);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setMapperName("%sRepository");
gc.setServiceName("I%sService");
gc.setEntityName("%sEntity");
gc.setEnableCache(false);
gc.setFileOverride(true);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/luoma_test?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));
pc.setParent("com.test");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输入文件名称
/* return projectPath + "/finance-generator-exp/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;*/
return outdir +"com/test/invoice/"+pc.getModuleName()+"/mapper"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null));
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//字段名生成策略
strategy.setSuperEntityClass("com.test.invoice.model.base.SuperEntity");// 自定义实体父类
strategy.setEntityLombokModel(true);
strategy.setInclude(scanner("表名")); // 需要生成的表
strategy.setSuperEntityColumns("id");// 自定义实体,公共字段
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");// 表前缀
strategy.setRestControllerStyle(true);
mpg.setStrategy(strategy);
// 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
测试
1.运行 App main()
2.输入模块名称和表名称
"D:\Program Files\Java\jdk1.8.0_151\bin\java.exe"
请输入模块名:
invoice
请输入表名:
t_rbt_test
20:23:40.415 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
20:23:41.411 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [D:\finance-generator-framework-code\com\test\invoice\entity]
20:23:41.425 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [D:\finance-generator-framework-code\com\test\invoice\controller]
20:23:41.426 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [D:\finance-generator-framework-code\com\test\invoice\service]
20:23:41.426 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [D:\finance-generator-framework-code\com\test\invoice\mapper]
20:23:41.426 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [D:\finance-generator-framework-code\com\test\invoice\service\impl]
20:23:41.524 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/mapper.xml.ftl; 文件:D:\finance-generator-framework-code\com/test/invoice/invoice/mapper/TRbtTestEntityMapper.xml
20:23:41.615 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/entity.java.ftl; 文件:D:\finance-generator-framework-code\com\test\invoice\entity\TRbtTestEntity.java
20:23:41.620 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/mapper.java.ftl; 文件:D:\finance-generator-framework-code\com\test\invoice\mapper\TRbtTestRepository.java
20:23:41.625 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/service.java.ftl; 文件:D:\finance-generator-framework-code\com\test\invoice\service\ITRbtTestService.java
20:23:41.635 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/serviceImpl.java.ftl; 文件:D:\finance-generator-framework-code\com\test\invoice\service\impl\TRbtTestServiceImpl.java
20:23:41.640 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/controller.java.ftl; 文件:D:\finance-generator-framework-code\com\test\invoice\controller\TRbtTestController.java
20:23:41.640 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================
进程已结束,退出代码 0
3.查看生成的文件
D:\finance-generator-framework-code\com\test\invoice
(1)TRbtTestController.java
D:\finance-generator-framework-code\com\test\invoice\controller
package com.test.invoice.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 前端控制器
* </p>
*
* @author luoma
* @since 2019-10-15
*/
@RestController
@RequestMapping("/invoice/t-rbt-test-entity")
public class TRbtTestController {
}
(2)TRbtTestEntity.java
D:\finance-generator-framework-code\com\test\invoice\entity
package com.test.invoice.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import java.time.LocalDateTime;
import com.test.invoice.model.base.SuperEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author luoma
* @since 2019-10-15
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("t_rbt_test")
public class TRbtTestEntity extends SuperEntity {
private static final long serialVersionUID = 1L;
/**
* 名称
*/
private String name;
/**
* 版本号
*/
private String version;
/**
* 创建人
*/
private String createBy;
/**
* 创建时间
*/
private LocalDateTime createTime;
/**
* 修改人
*/
private String updateBy;
/**
* 修改时间
*/
private LocalDateTime updateTime;
/**
* 是否删除
*/
private Integer isDelete;
/**
* 系统时间戳
*/
private LocalDateTime bizTime;
}
(3)ITRbtTestService.java
D:\finance-generator-framework-code\com\test\invoice\service
package com.test.invoice.service;
import com.test.invoice.entity.TRbtTestEntity;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 服务类
* </p>
*
* @author luoma
* @since 2019-10-15
*/
public interface ITRbtTestService extends IService<TRbtTestEntity> {
}
(4)TRbtTestServiceImpl.java
D:\finance-generator-framework-code\com\test\invoice\service\impl
package com.test.invoice.service.impl;
import com.test.invoice.entity.TRbtTestEntity;
import com.test.invoice.mapper.TRbtTestRepository;
import com.test.invoice.service.ITRbtTestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 服务实现类
* </p>
*
* @author luoma
* @since 2019-10-15
*/
@Service
public class TRbtTestServiceImpl extends ServiceImpl<TRbtTestRepository, TRbtTestEntity> implements ITRbtTestService {
}
(5)TRbtTestRepository.java
D:\finance-generator-framework-code\com\test\invoice\mapper
package com.test.invoice.mapper;
import com.test.invoice.entity.TRbtTestEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author luoma
* @since 2019-10-15
*/
public interface TRbtTestRepository extends BaseMapper<TRbtTestEntity> {
}
(6)TRbtTestEntityMapper.xml
D:\finance-generator-framework-code\com\test\invoice\invoice\mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.invoice.mapper.TRbtTestRepository">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.test.invoice.entity.TRbtTestEntity">
<result column="id" property="id" />
<result column="name" property="name" />
<result column="version" property="version" />
<result column="create_by" property="createBy" />
<result column="create_time" property="createTime" />
<result column="update_by" property="updateBy" />
<result column="update_time" property="updateTime" />
<result column="is_delete" property="isDelete" />
<result column="biz_time" property="bizTime" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id,
name, version, create_by, create_time, update_by, update_time, is_delete, biz_time
</sql>
</mapper>
mybatis-generator 插件使用
安装插件
Database 工具操作
连接数据库
这里我连接的是 Oracle
属性 | 描述 |
---|---|
名称 | 根据业务情况起一个易记的名称 |
主机 | 数据库 IP 地址 |
端口 | 数据库端口号 |
SID | 服务名称,TNS 中的 SERVICE_NAME |
用户 | 用户名 |
密码 | 密码 |
url | jdbc:oracle:thin:@IP:端口号:服务名 |
找到对应的表,右键 mybatis-generator
选择对应选项
对应目录下生成了代码
Mybatis X 插件
https://baomidou.com/guides/mybatis-x/
MybatisX 是一款专为 IntelliJ IDEA 设计的快速开发插件,旨在提升 MyBatis 与 MyBatis-Plus 框架的开发效率。
IDEA DB 连接 Oracle 错误总结
ORA-12505, TNS:listener does not currently know of SID
参考:idea链接oracle数据库报错:[66000][12505] Listener refused the connection with the following error:
错误描述
[66000][12505] Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor (CONNECTION_ID=Bn8THbnmRYmuHin4mmnWzw==) oracle.net.ns.NetException: Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor (CONNECTION_ID=Bn8THbnmRYmuHin4mmnWzw==).
查询 sid
select INSTANCE_NAME from v$instance;
上图中 SID
选项替换为查询结果即可
ORA-01005: 给出空口令; 登录被拒绝
问题描述
[72000][1005] ORA-01005: 给出空口令; 登录被拒绝
找了半天,妈的原来是密码输入错误了导致的,输入正确的密码即可