SpringBoot+Mybatis+FreeMarker项目配置
字段时间格式设置
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8") //设置时间格式
private Date lastLoginDate;
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.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yumbo</groupId>
<artifactId>yumbo-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 设置打包类型为 war包 -->
<packaging>war</packaging>
<name>yumbo-web</name>
<description>yumbo-web</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 排除内部Tomcat的影响 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- web 环境 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- aop -->
<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>
<scope>test</scope>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<!-- devtools热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- mysql数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 阿里数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- c3p0 -->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!-- commons-lang3 -->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<!-- json -->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 测试支持 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- 打包名称配置 -->
<finalName>yumbo-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<!-- 这一定要引入数据依赖 -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.48</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<!-- 如果没有该配置,热部署的devtools不生效 -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<!-- mysql的jar包(根路径) -->
<!-- <classPathEntry location="D:\maven\repository\mysql\mysql-connector-java\5.1.48\mysql-connector-java-5.1.48.jar"/> -->
<context id="store" targetRuntime="MyBatis3" defaultModelType="flat">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
<!-- 是否去除所有自动生成的文件的时间戳,默认为false -->
<property name="suppressDate" value="true"/>
</commentGenerator>
<!--数据库配置信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/yumbo-web"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- targetPackage:包路径名称(自定义) -->
<javaModelGenerator targetPackage="com.yumbo.entity" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 配置生成相应的实体Mapper.xml,对于Mapper3.X我们需要把type="XMLMAPPER" -->
<!-- targetPackage:包路径名称(自定义) -->
<sqlMapGenerator targetPackage="mapper_xml" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 配置生成相应的接口类,对应与Mapper.xml中的一系列CRUD方法SQL语句 -->
<!-- targetPackage:包路径名称(自定义) -->
<javaClientGenerator targetPackage="com.yumbo.mapper" targetProject="src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
<property name="exampleMethodVisibility" value="public"/>
</javaClientGenerator>
<!-- 数据库表-->
<table tableName="t_user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"
></table>
</context>
</generatorConfiguration>
启动类
/**
* @Author: GrainFull
* @Date: 2021/3/16 1:14
* @Description: 启动类
*/
@SpringBootApplication
@MapperScan(basePackages = {"com.yumbo.mapper"})
public class YumBoWebApplication {
public static void main(String[] args) {
SpringApplication.run(YumBoWebApplication.class, args);
}
}
application.yml
## 配置服务器端括号
server:
port: 8081
## 编码格式
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
spring:
# 环境 dev|test|prod
profiles:
active: dev
## 启用热部署
devtools:
restart:
enabled: true
additional-paths: src/main/java
## mybatis 配置
mybatis:
mapper-locations: classpath*:/mapper_xml/*.xml
type-aliases-package: com.yumbo.entity;com.yumbo.dto
configuration:
map-underscore-to-camel-case: true
## pageHelper 分页
pagehelper:
helper-dialect: mysql
## 设置 dao 日志打印级别
logging:
level:
com:
yumbo:
mapper: debug
application-dev.yml
spring:
## 配置数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/yumbo-web?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
WebMvc配置类
/**
* @Author: GrainFull
* @Date: 2021/3/24 11:12
* @Description: WebMvc配置类
*/
@Configuration // 配置类
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Bean // 将方法的返回值交给IOC维护
public NoLoginInterceptor getNoLoginInterceptor() {
return new NoLoginInterceptor();
}
/**
* 添加拦截器
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 需要一个实现了拦截器功能的实例对象,这里使用的是NoLoginInterceptor
registry.addInterceptor(getNoLoginInterceptor())
// 默认拦截所有的资源
.addPathPatterns("/**")
// 设置不需要被拦截的资源
.excludePathPatterns("/index.html");
}
}
拦截器
/**
* @Author: GrainFull
* @Date: 2021/3/24 0:49
* @Description: 自定义拦截器
* <p>
* 非法访问拦截
* 继承HandlerInterceptor适配器
*/
public class NoLoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
/*
思路:
获取登录用户信息
判断是否有数据,不存在,跳转登录页。
*/
}
}