SpringBoot
SpringBoot可以让你简单、快速、方便的搭建一个Spring Web项目。
Mybatis-Plus
MP是国内人员开发的一个Mybatis的增强工具,在Mybatis的基础上只做增强不做改变,为简化开发、提高效率而生。支持通用的 CRUD、代码生成器与条件构造器
项目搭建
- 初始化SpringBoot项目
首先创建一个maven项目,删除src文件夹
创建springboot 项目模块
分别添加core、common、practice模块。删除多余文件,配置父子模块间依赖关系及相应jar。如下图
主POM
<?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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yc</groupId>
<artifactId>DeepLearning</artifactId>
<version>1.0.0</version>
<modules>
<module>dl-common</module>
<module>dl-core</module>
<module>dl-practice</module>
</modules>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<!-- jar -->
<druid-spring-boot-starter.version>1.1.10</druid-spring-boot-starter.version>
<mybatis-plus.version>3.1.0</mybatis-plus.version>
<lombok.version>1.16.4</lombok.version>
<swagger.version>2.7.0</swagger.version>
<bootstrap.version>4.0.0</bootstrap.version>
<common-codec.version>1.9</common-codec.version>
<commons-lang3.version>3.9</commons-lang3.version>
<itextpdf.version>5.5.13.1</itextpdf.version>
<zxing-core.version>3.4.0</zxing-core.version>
<fastjson.version>1.2.58</fastjson.version>
<json-lib.version>1.0.2</json-lib.version>
<freemarker.version>2.3.29</freemarker.version>
<jwt.version>3.7.0</jwt.version>
<hutool.version>5.0.0</hutool.version>
<shiro.version>1.4.0</shiro.version>
</properties>
<dependencies>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-spring-boot-starter.version}</version>
</dependency>
<!--lombok jar -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</project>
common POM
<?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>com.yc</groupId>
<artifactId>DeepLearning</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>dl-common</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.yc</groupId>
<artifactId>dl-core</artifactId>
<version>1.0.0</version>
</dependency>
<!--AOP场景启动器:支持面向切面的编程,包括spring-aop和AspectJ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
core POM
<?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>com.yc</groupId>
<artifactId>DeepLearning</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>dl-core</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<dependencies>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
</dependencies>
</project>
practice POM
<?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>com.yc</groupId>
<artifactId>DeepLearning</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>dl-practice</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.yc</groupId>
<artifactId>dl-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.yc</groupId>
<artifactId>dl-common</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Web 场景 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--Swagger 相关:核心包:扫描程序,生成文档数据;swagger生成可视化界面-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
</project>
- 配置application.yml及启动类
server:
port: 8081
tomcat:
uri-encoding: UTF-8
servlet:
context-path: /deepLearning
spring:
#=====DataBase 相关===========
datasource:
url: jdbc:mysql://127.0.0.1:3306/deeplearning?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: xieyc@mysql
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙,此处是filter修改的地方
druid:
filters: stat,wall,config
use-global-data-source-stat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
max-active: 100
initial-size: 1
max-wait: 60000
min-idle: 1
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: select 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-open-prepared-statements: 50
max-pool-prepared-statement-per-connection-size: 20
#======静态资源相关 ===========
resource:
static-locations: classpath:/static/,classpath:/public/
#=====Mybatis plus 相关=====
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*Mapper.xml
type-aliases-package: com.lh.modules.*.entity,com.lh.system.entity
configuration:
map-underscore-to-camel-case: true # 驼峰
global-config:
banner: false #关闭MP 3.0 自带的banner
package com.yc.practice;
import com.yc.common.utils.LocalHostUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
/**
* 功能描述:
*
* <p>版权所有:</p>
* 未经本人许可,不得以任何方式复制或使用本程序任何部分
*
* @Company: 紫色年华
* @Author: xieyc
* @Datetime: 2019-05-30
*/
@SpringBootApplication
@ComponentScan({"com.yc"})
@MapperScan({"com.yc.core.*.mapper"})
@EnableTransactionManagement(proxyTargetClass = true)
@Slf4j
public class DlPracticeApplication {
public static void main(String[] args) {
ConfigurableApplicationContext application = SpringApplication.run(DlPracticeApplication.class, args);
Environment env = application.getEnvironment();
String ip = LocalHostUtil.getIpAddress();
String port = env.getProperty("server.port");
String path = env.getProperty("server.servlet.context-path");
log.info("\n----------------------------------------------------------\n\t" +
"Application DeepLearning is running! Access URLs:\n\t" +
"Local: \t\thttp://localhost:" + port + path + "/\n\t" +
"External: \thttp://" + ip + ":" + port + path + "/\n\t" +
"swagger-ui: http://" + ip + ":" + port + path + "/swagger-ui.html\n\t" +
"----------------------------------------------------------");
}
}
附:Mybatis generator 配置文件
package com.yc.core;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
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.HashMap;
import java.util.List;
import java.util.Map;
/**
* 功能描述:MyBatis-plus 代码生成器
*
* <p>版权所有:</p>
* 未经本人许可,不得以任何方式复制或使用本程序任何部分
*
* @Company: 紫色年华
* @Author: xieyc
* @Datetime: 2019-06-28 17:17
* @Version: 1.0.0
*/
public class MyBatisGenerator {
/**
* 生成文件所在项目路径
*/
private static final String BASE_PATH = "D:\\workSpace\\DeepLearning\\dl-core";
/**
* 基本包名
*/
private static final String BASE_PACKAGE = "com.yc.core";
/**
* 文件夹名
*/
private static final String MODEL_NAME = "mall";
private static final String AUTHOR = "xieyc";
/**
* 要生成的表名
*/
private static final String[] TABLES = {"mall_brand"};
/**
* 数据库配置四要素
*/
private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
private static final String URL =
"jdbc:mysql://127.0.0.1:3306/deeplearning?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8";
private static final String USERNAME = "root";
private static final String PASSWORD = "xieyc@mysql";
public static void main(String[] args) {
//代码生成器
AutoGenerator gen = new AutoGenerator();
// 全局配置
gen.setGlobalConfig(new GlobalConfig()
// 主键自增
.setIdType(IdType.UUID)
// 是否覆盖文件
.setFileOverride(true)
// 输出目录
.setOutputDir( BASE_PATH + "/src/main/java")
// 作者
.setAuthor(AUTHOR)
// 生成后打开文件夹
.setOpen(false)
// 开启 activeRecord 模式
// .setActiveRecord(true)
// XML 二级缓存
.setEnableCache(false)
// XML 生成基本的resultMap
.setBaseResultMap(false)
// XML 生成基本的sql片段
.setBaseColumnList(false)
// 自定义文件命名,%s会自动填充表实体属性!默认IXXXService
.setMapperName("%sMapper")
.setXmlName("%sMapper")
.setServiceName("%sService")
.setServiceImplName("%sServiceImpl")
.setControllerName("%sController")
);
// 数据库配置
gen.setDataSource(new DataSourceConfig()
.setUrl(URL)
.setDriverName(DRIVER_NAME)
.setUsername(USERNAME)
.setPassword(PASSWORD)
.setDbType(DbType.MYSQL)
);
// 包配置
gen.setPackageInfo(new PackageConfig()
.setModuleName(MODEL_NAME)
// 自定义包路径
.setParent(BASE_PACKAGE)
// 这里是控制器包名,默认 web
.setController("controller")
.setEntity("entity")
.setMapper("mapper")
.setService("service")
.setServiceImpl("service.impl")
.setXml("resource")
);
// 注入自定义配置 可以在 VM 中使用 cfg.abc 设置的值
InjectionConfig abc = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
//自定义文件输出位置(非必须)
List<FileOutConfig> fileOutList = new ArrayList<>();
fileOutList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
return BASE_PATH + "/src/main/resources/mapper/"+ MODEL_NAME +"/" + tableInfo.getEntityName() + "Mapper.xml";
}
});
abc.setFileOutConfigList(fileOutList);
gen.setCfg(abc);
// 指定模板引擎 默认是VelocityTemplateEngine ,需要引入相关引擎依赖
gen.setTemplateEngine(new FreemarkerTemplateEngine());
// 模板配置 关闭默认 xml 生成,调整生成 至 根目录
gen.setTemplate(new TemplateConfig()
.setXml(null)
.setService("/mybatis/service.java")
.setServiceImpl("/mybatis/serviceImpl.java")
.setController("/mybatis/controller.java")
.setMapper("/mybatis/mapper.java")
.setEntity("/mybatis/model.java")
);
// 策略配置
gen.setStrategy(new StrategyConfig()
// 表名生成策略
.setNaming(NamingStrategy.underline_to_camel)
.setColumnNaming(NamingStrategy.underline_to_camel)
.setEntityLombokModel(true)
.setRestControllerStyle(true)
// 需要生成的表
.setInclude(TABLES)
// 自定义实体,公共字段
.setSuperEntityColumns("id")
.setControllerMappingHyphenStyle(true)
);
// 执行生成
gen.execute();
}
}