这里先感谢博主:https://blog.csdn.net/qq_41153943/article/details/107610454 解决了IDEA Error:java:无效的源发行版:11 错误 问题
接下来进入我们的本篇博客感谢环节,感谢博主提供的思路:https://www.cnblogs.com/liuyj-top/p/12976396.html
新建一个SpringBoot项目(教程很多),我的项目目录如下(请忽略红线,后面会有问题解决):
1.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itcmor</groupId>
<artifactId>qsgl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>qsgl</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>com.oracle</groupId>-->
<!--<artifactId>ojdbc12</artifactId>-->
<!--<version>12.2.0.1.0</version>-->
<!--<scope>runtime</scope>-->
<!--</dependency>-->
<!-- gxq+20210624 MyBatis-plus 逆向生成配置-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.66</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.在src\main\resources下新建一个application.yml文件,并进行配置。(说实话,我把这个件内容给注释了也没啥影响(生成代码的java中有连接数据库的配置),可能是我用的Oracle?不过如果不着急配置一下也不错,增加点见识)
server:
port: 8088
servlet:
context-path: /
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@localhost:1521:ORCL
username: ****** # 连接Oracle的用户
password: ****** # 连接Oracle的密码
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
serialization:
write-dates-as-timestamps: false
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*Mapper.xml
global-config:
db-config:
logic-not-delete-value: 1
logic-delete-value: 0
3.MyBatis-plus分页插件配置-就是新建一个.java文件,我在test\java\com.itcmor.qsgl下新建MyBatisPlusConfig.java
package com.itcmor.qsgl; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyBatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor(){ return new PaginationInterceptor(); } }
4.MyBatis-plus的逆向工程配置(即生成代码的配置)-CodeGenerator.java,这借鉴的博主的,应该是基本模式生成,mapper文件没有基本的增删查sql语句(准备使用freemarker模板引擎设定)。
package com.itcmor.qsgl; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.generator.AutoGenerator; 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 com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.generator.InjectionConfig; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * MyBatis-Plus 逆向工程 生成controllor、entity、mapper等代码 */
public class CodeGenerator { 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)){ //Mybatis-plus 3.4.3 没有方法isNotEmpty(),我又改回了博主使用的3.2.0
return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } public static void main(String[] args){ // 代码生成器初始化
AutoGenerator mpg = new AutoGenerator(); // 全局配置
GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("gxq"); gc.setOpen(false); // 实体属性 Swagger2 注解
gc.setSwagger2(false); mpg.setGlobalConfig(gc); // 数据源配置
DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:oracle:thin:@localhost:1521:ORCL"); dsc.setDriverName("oracle.jdbc.OracleDriver"); dsc.setUsername("C##gxq_qs_terminal"); dsc.setPassword("gxq@2021"); mpg.setDataSource(dsc); // 包配置
PackageConfig pc = new PackageConfig(); pc.setParent("com.itcmor.qsgl"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.setService("service"); pc.setServiceImpl("service.impl"); mpg.setPackageInfo(pc); // 自定义配置
InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing
} }; // 如果模板引擎是 freemarker // String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 velocity // String templatePath = "/templates/mapper.xml.vm"; // 自定义输出配置 (gxq:如若注释,则按照默认生成) // List<FileOutConfig> focList = new ArrayList<>(); // 自定义配置会被优先输出 // focList.add(new FileOutConfig(templatePath) { // @Override // public String outputFile(TableInfo tableInfo) { // // 自定义输出文件名,如果entity设置了前后缀,此处应注意xml的名称会跟着发生变化! // return projectPath + "/src/main/resources/mybatis/mapper/" // + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; // } // });
/* cfg.setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { // 判断自定义文件夹是否需要创建 checkDir("调用默认方法创建的目录"); return false; } }); */
// cfg.setFileOutConfigList(focList); // mpg.setCfg(cfg); // 配置模板
TemplateConfig templateConfig = new TemplateConfig(); // 配置自定义输出模板 // 指定自定义模板路径,注意不要带上.ftl/.vm,会根据使用的模板引擎自动识别 //templateConfig.setEntity("templates/entity2.java"); // templateConfig.setMapper("/templates/maper.xml.ftl"); //templateConfig.setService(); //templateConfig.setController();
templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 或者叫数据库表配置?
StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setEntityLombokModel(true); // 公共父类 // strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController"); // 写于父类中的公共字段 // strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
Ok,右键运行这个文件文件就行了。假如遇到了JDK版本问题,如Error Java:无目标版本 11,推荐个解决问题的网址,very very感谢作者:https://www.jianshu.com/p/9a3d5258fff8 解决理念:百度到的能改的地方都改了
问题解决环节
1.注解标红问题1 (找不到@RestController、@RequestMapping),如下图:
解决:在pom.xml文件中加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.注解标红问题2 (找不到lombok插件 @Data、@RestController、@RequestMapping),如下图:
解决:File-->Settings...-->Plugins,搜索lombok会发现找不到,然后点击下方的Browse Repositories,如下图所示,再次搜索lombok,点击Install
安装后,在pom.xml文件中进行配置
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
至于测试,直接运行Spring Boot的应用程序即可(QsglApplication),然后在网页上输入localhost:8088/+@RequestMapper或者@PostMapper后配置的路径 (注解函数非注解类)
本篇结束,但是mapper没有满足需求,接下来摸索freemarker模板-ftl,想着能生成基本的增删查,由于Mybatis的mapper.xml能生成,希望不仅Mybatis-plus的mapper.xml能生成,其mapper.java也能生成。
Over!