好了,初始化项目完成之后,开始给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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.cmh</groupId>
<artifactId>fml-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>fml-manager</name>
<description>这里写上你的项目描述</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--springboot-jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springboot-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<!--Mybatis-Plus逆向工程-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.1</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.3</version>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.1</version>
</dependency>
<!--freemaker-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
<!--如果有配置mybatisPlus,需要以下配置-->
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--hutool工具集-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.我的项目结构,老规矩,可以根据自己的取舍,需要自己手动创建的,根据自己要求来创建,切记Application是启动类,是项目的启动入口,一定要要和这些包同目录下,保证启动的时候能正常扫描到这些配置类、控制器等…
package com.cmh;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
/**
* 启动通用主程序
* @EnableCaching开启缓存,我这边使用了redis..如果不用,自行取舍
* @author chenPC
*/
@MapperScan("com.cmh.mapper")
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.yml
spring:
profiles:
active: dev
application-dev.yml(根据配置取舍)
server:
port: 8080
spring:
#图片上传
servlet:
multipart:
max-request-size: 10MB
file-size-threshold: 10MB
#数据源
datasource:
url: jdbc:mysql://localhost:3306/yours_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
#缓存模式为redis
cache:
type: redis
#redis
redis:
host: XX.XX.XXX.XXx
port: 6379
database: 0
password: xxxxx
timeout: 100s
#连接池配置
jedis:
pool:
#最大空闲连接数
max-idle: 500
#最小空闲连接数
min-idle: 50
#等待可用连接最大时间,负数为无限制
max-wait: -1s
#最大活跃连接时间
max-active: -1
#thymeleaf
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
mode: HTML
servlet:
content-type: text/html
#json返回时间格式设置
jackson:
date-format: yyyy-MM-dd
time-zone: GMT+8
#关闭title的小图标,使用自己的图标,需要在resources导入自己的图标--logo.ico
mvc:
favicon:
enabled: false
#mybatis-plus
mybatis-plus:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.cmh.entity
configuration:
map-underscore-to-camel-case: true #使用驼峰法映射属性
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
id-type: 0
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: 2
#驼峰下划线转换
db-column-underline: true
#刷新mapper 调试神器
refresh-mapper: true
#数据库大写下划线转换
capital-mode: true
#逻辑删除配置
logic-delete-value: 0
logic-not-delete-value: 1
#分页插件
pagehelper:
helper-dialect: mysql
params: count= countSql
reasonable: true
support-methods-arguments: true
#显示SQL语句日志,扫描项目mapper包下的所有类,对SQL语句进行控制台打印
logging:
level:
com.cmh.mapper: debug
创建项目的时候已经创建好了配置类的包,所以在com.cmh.config下创建配置类
1.静态资源处理配置(配置类必须加@Configuration注解)
package com.cmh.config.staticresource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author chenPC
*/
@Configuration
public class StaticResourceConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}
2.mybatisPlus分页插件配置
package com.cmh.config.mybatisplusconfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* Mybatis-Plus 分页配置
*
* @author chenPC
*/
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
3.swagger2配置(不知道可以自行百度)
package com.cmh.config.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* swagger2配置类,用于配置swagger2分组
*
* @author chenPC
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
List<Parameter> pars = new ArrayList<Parameter>();
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("这里写你的项目名")
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(pars)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("文档的标题")
.description("接口文档描述")
.termsOfServiceUrl("服务条框url")
.version("给项目定义版本,比如1.0")
.build();
}
}
3.mybatisPlus逆向工程(自己留着,可以生成控制器、服务层、mapper层接口,很方便)
package com.cmh.config.mpgenerator;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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 java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author chenPC
*/
public class MpGenerator {
/**
* 逆向先引入:mybatisPlus包、逆向工程包、freemarker包
*
* 读取控制台内容
*
*/
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 MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
//outputDir:生成文件的输出目录,默认值:D 盘根目录
gc.setOutputDir("F://allTemplates//javaTemp");
//IdType设置主键生成策略,默认值null
gc.setIdType(IdType.UUID);
//Author设置作者
gc.setAuthor("陈明华");
// XML文件中的通用结果映射ResultMap
gc.setBaseResultMap(true);
// XML通用的columList
gc.setBaseColumnList(true);
//Open:是否打开输出目录
gc.setOpen(false);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/your_db?serverTimezone=UTC&&characterEncoding=utf-8&useSSL=false");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
//设置数据库类型,默认MYSQL
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// 输入生成逆向文件的目录名
pc.setModuleName(scanner("模块名"));
//父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setParent("com.cmh");
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 + "/src/main/resources/mapping/" + pc.getModuleName()
+ "/" + 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);
//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//【实体】是否为lombok模型(默认 false)
strategy.setEntityLombokModel(true);
//生成 @RestController 控制器
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名").split(","));
//驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
//表名前缀
strategy.setTablePrefix(pc.getModuleName() + "_");
//是否生成实体时,生成字段注解
strategy.entityTableFieldAnnotationEnable(true);
//设置自定义继承的Entity类全称,带包名
// strategy.setSuperEntityClass("com.lj.common.BaseEntit");
//设置自定义继承的Controller类全称,带包名
// strategy.setSuperControllerClass("com.lj.common.BaseController");
//设置自定义基础的Entity类,公共字段
// strategy.setSuperEntityColumns("id");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
至此,框架搭建完毕,后续想测试框架的,自己添加controller,对框架进行测试…
package com.cmh.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author chenPC
*/
@RestController
@RequestMapping("/test")
public class TestController {
/**
* 测试
*
* @return
*/
@RequestMapping("/test1")
public String test1() {
return "启动成功";
}
}
启动项目
打开浏览器,输入:http://localhost:8080/test/test1,显示“启动成功”,即框架成功搭建啦…