SpringBoot个人博客系统
项目功能设计
分为两大块:
- 游客版块
- 注册登录
- 对博主的文章进行评论
- 管理员版块
- 对游客信息进行增删查改
- 对文章信息进行增删查改
- 对文章分类进行增删查改
- 对单个文章的评论可以实现删除操作
项目目录设计
Java层:entity,mapper,service,controller,config,common
前端层:admin(后端层)+index(前端展示层)
数据库设计
共6张表
admin管理员表
/*
Navicat Premium Data Transfer
Source Server : 114.215.187.97
Source Server Type : MySQL
Source Server Version : 50649
Source Host : 114.215.187.97:3306
Source Schema : sb_blog
Target Server Type : MySQL
Target Server Version : 50649
File Encoding : 65001
Date: 06/11/2020 17:06:34
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for admin
-- ----------------------------
DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键',
`username` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '管理员账户',
`password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
`nickname` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '昵称',
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
`status` int(10) NULL DEFAULT 0 COMMENT '状态',
`is_supper` int(10) NULL DEFAULT 0 COMMENT '是否超级管理员',
`create_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建时间',
`update_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新时间',
`delete_time` varbinary(100) NULL DEFAULT NULL COMMENT '软删除时间',
`isdel` int(10) NULL DEFAULT 0 COMMENT '逻辑删除标记',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
articl文章表
/*
Navicat Premium Data Transfer
Source Server : 114.215.187.97
Source Server Type : MySQL
Source Server Version : 50649
Source Host : 114.215.187.97:3306
Source Schema : sb_blog
Target Server Type : MySQL
Target Server Version : 50649
File Encoding : 65001
Date: 06/11/2020 17:07:34
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for article
-- ----------------------------
DROP TABLE IF EXISTS `article`;
CREATE TABLE `article` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键',
`title` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '标题',
`artdesc` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '概要',
`tags` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '标签',
`content` longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '内容',
`is_top` int(10) NULL DEFAULT 0 COMMENT '是否推荐',
`cate_id` int(10) NOT NULL COMMENT '所属导航id',
`create_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建时间',
`update_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新时间',
`delete_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '软删除',
`isdel` int(10) NULL DEFAULT 0 COMMENT '软删除标记位',
`member_id` int(10) NULL DEFAULT NULL COMMENT '添加人员id',
`viewnum` int(100) NULL DEFAULT 0 COMMENT '浏览次数',
`commentnum` int(100) NULL DEFAULT 0 COMMENT '评论次数',
`authorname` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '文章作者',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
cate专栏表
/*
Navicat Premium Data Transfer
Source Server : 114.215.187.97
Source Server Type : MySQL
Source Server Version : 50649
Source Host : 114.215.187.97:3306
Source Schema : sb_blog
Target Server Type : MySQL
Target Server Version : 50649
File Encoding : 65001
Date: 06/11/2020 17:08:16
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for cate
-- ----------------------------
DROP TABLE IF EXISTS `cate`;
CREATE TABLE `cate` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键',
`catename` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '导航名称',
`sort` int(10) NULL DEFAULT NULL COMMENT '排序',
`create_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建时间',
`update_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新时间',
`delete_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '软删除',
`isdel` int(10) NULL DEFAULT 0 COMMENT '软删除标记',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
comment评论表
/*
Navicat Premium Data Transfer
Source Server : 114.215.187.97
Source Server Type : MySQL
Source Server Version : 50649
Source Host : 114.215.187.97:3306
Source Schema : sb_blog
Target Server Type : MySQL
Target Server Version : 50649
File Encoding : 65001
Date: 06/11/2020 17:08:47
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for comment
-- ----------------------------
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`content` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '评论内容',
`article_id` int(11) NULL DEFAULT NULL COMMENT '评论文章id',
`member_id` int(11) NULL DEFAULT NULL COMMENT '评论用户id',
`create_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建时间',
`update_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新时间',
`delete_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '软删除时间',
`isdel` int(10) NULL DEFAULT 0 COMMENT '软删除标记位',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
member游客表
/*
Navicat Premium Data Transfer
Source Server : 114.215.187.97
Source Server Type : MySQL
Source Server Version : 50649
Source Host : 114.215.187.97:3306
Source Schema : sb_blog
Target Server Type : MySQL
Target Server Version : 50649
File Encoding : 65001
Date: 06/11/2020 17:09:19
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for member
-- ----------------------------
DROP TABLE IF EXISTS `member`;
CREATE TABLE `member` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键',
`username` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名',
`password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
`nickname` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '昵称',
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
`create_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建时间',
`update_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新时间',
`delete_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '软删除时间',
`isdel` int(10) NULL DEFAULT 0 COMMENT '软删除标记位',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
syssetting系统设置表
/*
Navicat Premium Data Transfer
Source Server : 114.215.187.97
Source Server Type : MySQL
Source Server Version : 50649
Source Host : 114.215.187.97:3306
Source Schema : sb_blog
Target Server Type : MySQL
Target Server Version : 50649
File Encoding : 65001
Date: 06/11/2020 17:09:49
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for syssetting
-- ----------------------------
DROP TABLE IF EXISTS `syssetting`;
CREATE TABLE `syssetting` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`webname` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '网站名称',
`copyright` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '版权信息',
`create_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建时间',
`update_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新时间',
`delete_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '软删除时间',
`sign` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '个性签名',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
项目技术
SpringBoot jQuery MyBatis FreeMarker
运行环境
jdk1.8 mysql idea maven
导入依赖
<?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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lee</groupId>
<artifactId>sbblog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sbblog</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- mybatis plus 代码生成器依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 代码生成器模板 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>true</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
MP外部配置
package com.zxy.common;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class MyBatisPlusConfig {
/**
* 配置分页插件
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
/**
* sql执行效率插件
* @return
*/
@Bean
@Profile({"test"})
public PerformanceInterceptor performanceInterceptor(){
return new PerformanceInterceptor();
}
/**
* 逻辑删除用
* @return
*/
@Bean
public ISqlInjector iSqlInjector(){
return new LogicSqlInjector();
}
}
使用MyBatis-plus
package com.zxy;
import com.baomidou.mybatisplus.annotation.FieldFill;
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.TableFill;
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;
public class MysqlGenerator {
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");
// TODO 设置用户名
gc.setAuthor("Zxy");
gc.setOpen(true);
// service 命名方式
gc.setServiceName("%sService");
// service impl 命名方式
gc.setServiceImplName("%sServiceImpl");
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setFileOverride(true);
gc.setActiveRecord(true);
// XML 二级缓存
gc.setEnableCache(false);
// XML ResultMap
gc.setBaseResultMap(true);
// XML columList
gc.setBaseColumnList(false);
mpg.setGlobalConfig(gc);
// TODO 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/sb_blog?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("1234");
mpg.setDataSource(dsc);
// TODO 包配置
PackageConfig pc = new PackageConfig();
//pc.setModuleName(scanner("模块名"));
pc.setParent("com.zxy");
pc.setEntity("entity");
pc.setService("service");
pc.setServiceImpl("service.impl");
mpg.setPackageInfo(pc);
// 自定义需要填充的字段
List<TableFill> tableFillList = new ArrayList<>();
//如 每张表都有一个创建时间、修改时间
//而且这基本上就是通用的了,新增时,创建时间和修改时间同时修改
//修改时,修改时间会修改,
//虽然像Mysql数据库有自动更新几只,但像ORACLE的数据库就没有了,
//使用公共字段填充功能,就可以实现,自动按场景更新了。
//如下是配置
TableFill createField = new TableFill("create_time", FieldFill.INSERT);
TableFill modifiedField = new TableFill("update_time", FieldFill.INSERT_UPDATE);
tableFillList.add(createField);
tableFillList.add(modifiedField);
// 自定义配置
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/mapper/admin/"
+ "/" + 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.setEntityLombokModel(false);
// 设置逻辑删除键
strategy.setLogicDeleteFieldName("isdel");
// TODO 指定生成的bean的数据库表名
strategy.setInclude("syssetting");
//strategy.setSuperEntityColumns("id");
// 驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy);
// 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
实体类结构
实体类采用JSR303数据校验
定制统一返回接口
对异常信息实行统一规范
工具类
DataGrid 作为表格数据结构存储对象
package com.zxy.common;
import java.util.ArrayList;
import java.util.List;
/**
* 表格数据存储对象
* 默认作为bootstrap表格对应的数据结构存储对象
* author:Zxy
*/
public class DataGrid {
private Long total = Long.valueOf(0);
private List rows = new ArrayList();
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
}
DateTimeUtil 时间转换类(网上找的)
package com.zxy.common;
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Title:公共工具类
* Description: 对日期的一些操作
*/
public class DateTimeUtil {
/**
* 由java.util.Date到java.sql.Date的类型转换
*
* @param date
*
* @return Date
*/
public static Date getSqlDate(Date date) {
return new Date(date.getTime());
}
public static Date nowDate() {
Calendar calendar = Calendar.getInstance();
return getSqlDate(calendar.getTime());
}
/**
* 判断给定的年月日,是否为一周的开始
*
* @param year
* @param month
* @param day
* @return
*/
private static boolean getIsWeekBegin(int year, int month, int day) {
boolean flag = false;
Calendar c = Calendar.getInstance();
c.set(year, month - 1, day); // 设置日期
int week = c.get(Calendar.DAY_OF_WEEK); // 获取当前日期星期,英国那边的算法(周日算一周第一天)
if (week == 2) { // 如果是周1就返回true
flag = true;
}
return flag;
}
/**
*
* 给定一个日期,获得这个日期所在周的周一的日期
*
* @param date
* void
* @exception 2012-10-20 下午5:56:58
*/
public static String getMonday(String date, String df) {
if (!StringUtils.isEmpty(date)) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try {
d = format.parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
// 关于DAY_OF_WEEK的说明
// Field number for get and set indicating
// the day of the week. This field takes values
// SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
// and SATURDAY
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
if (!StringUtils.isEmpty(df))
format = new SimpleDateFormat(df);
return format.format(cal.getTime());
} catch (Exception e) {
e.printStackTrace();
return "";
}
} else {
return "";
}
}
/**
* 判断给定的年月日,是否为一周的结束
*
* @param year
* @param month
* @param day
* @return
*/
public static boolean getIsWeekEnd(int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month - 1, day); // 设置日期
int week = c.get(Calendar.DAY_OF_WEEK); // 获取当前日期星期,英国那边的算法(周日算一周第一天)
if (week == 7 || week == 1) { // 如果是周六或周日就返回true
return true;
}
return false;
}
/**
* 获得某月包含的周一个数
*
* @param year
* @param month
* @return
*/
public static int getWeekNumberOfMonth(int year, int month) {
// 周1 的计数器count
int count = 0;
int day = getDayCountOfMonth(year, month);
for (int i = 1; i <= day; i++) {
if (getIsWeekBegin(year, month, i)) {
count += 1;
}
}
return count;
}
/**
* 获得某一日期的后一天
*
* @param date
* @return Date
*/
public static Date getNextDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE, day + 1);
return getSqlDate(calendar.getTime());
}
/**
* 获得某一日期的前一天
*
* @param date
* @return Date
*/
public static Date getPreviousDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE, day - 1);
return getSqlDate(calendar.getTime());
}
/**
* 返回给定日期所在月的第一天
*
* @param date
* @return
* @throws ParseException
*/
public static Date getFirstDayOfMonth(final String date)
throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d = format.parse(date);
Calendar cal = new GregorianCalendar();
cal.setTime(d);
return getFirstDayOfMonth(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH) + 1);
}
/**
* 取得给定日期所在月的天数
*
* @return int
*/
public static int getDayCountOfMonth(final String date) {
Date d = StringToDate(date);
Calendar cal = new GregorianCalendar();
cal.setTime(d);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR));
calendar.set(Calendar.MONTH, cal.get(Calendar.MONTH));
calendar.set(Calendar.DATE, 0);
return calendar.get(Calendar.DATE);
}
/**
* 获取当前月的天数
*
* @return int
* @exception 2012-10-19 下午9:58:07
*/
public static int getDayCountOfMonth() {
return getDayCountOfMonth(Integer.parseInt(yyyyStr()),
Integer.parseInt(mmStr()));
}
/**
* 获得某年某月第一天的日期
*
* @param year
* @param month
* @return Date
*/
public static Date getFirstDayOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DATE, 1);
return calendar.getTime();
}
/**
* 获得某年某月最后一天的日期
*
* @param year
* @param month
* @return Date
*/
public static Date getLastDayOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, 1);
return getPreviousDate(getSqlDate(calendar.getTime()));
}
/**
* 由年月日构建java.sql.Date类型
*
* @param year
* @param month
* @param date
* @return Date
*/
public static Date buildDate(int year, int month, int date) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, date);
return getSqlDate(calendar.getTime());
}
/**
* 取得某月的天数
*
* @param year
* @param month
* @return int
*/
public static int getDayCountOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, 0);
return calendar.get(Calendar.DATE);
}
/**
* 获得某年某季度的最后一天的日期
*
* @param year
* @param quarter
* @return Date
*/
public static Date getLastDayOfQuarter(int year, int quarter) {
int month = 0;
if (quarter > 4) {
return null;
} else {
month = quarter * 3;
}
return getLastDayOfMonth(year, month);
}
/**
* 获得某年某季度的第一天的日期
*
* @param year
* @param quarter
* @return Date
*/
public static Date getFirstDayOfQuarter(int year, int quarter) {
int month = 0;
if (quarter > 4) {
return null;
} else {
month = (quarter - 1) * 3 + 1;
}
return getFirstDayOfMonth(year, month);
}
/**
* 获得某年的第一天的日期
*
* @param year
* @return Date
*/
public static Date getFirstDayOfYear(int year) {
return getFirstDayOfMonth(year, 1);
}
/**
* 获得某年的最后一天的日期
*
* @param year
* @return Date
*/
public static Date getLastDayOfYear(int year) {
return getLastDayOfMonth(year, 12);
}
/**
* String到java.sql.Date的类型转换
*
* @param param
* @return Date
*/
public static Date StringToDate(String param) {
if (StringUtils.isEmpty(param)) {
return null;
} else {
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(param);
return date;
} catch (ParseException ex) {
return null;
}
}
}
/**
* 时间格式: 20050601081202
*/
public static String nowNumStr() {
Calendar now = Calendar.getInstance();
String yyyy = String.valueOf(now.get(Calendar.YEAR));
String mm = String.valueOf(now.get(Calendar.MONTH) + 1);
String dd = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
String hh = String.valueOf(now.get(Calendar.HOUR_OF_DAY));
String ii = String.valueOf(now.get(Calendar.MINUTE));
String ss = String.valueOf(now.get(Calendar.SECOND));
mm = (1 == mm.length()) ? ("0" + mm) : mm;
dd = (1 == dd.length()) ? ("0" + dd) : dd;
hh = (1 == hh.length()) ? ("0" + hh) : hh;
ii = (1 == ii.length()) ? ("0" + ii) : ii;
ss = (1 == ss.length()) ? ("0" + ss) : ss;
String timeStr = yyyy + mm + dd + hh + ii + ss;
return timeStr;
}
/**
* 时间格式: 20050601081202
*/
public static String nowymdNumStr() {
Calendar now = Calendar.getInstance();
String yyyy = String.valueOf(now.get(Calendar.YEAR));
String mm = String.valueOf(now.get(Calendar.MONTH) + 1);
String dd = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
mm = (1 == mm.length()) ? ("0" + mm) : mm;
dd = (1 == dd.length()) ? ("0" + dd) : dd;
String timeStr = yyyy + mm + dd;
return timeStr;
}
/**
* 时间格式:2005-01
*/
public static String ymStr() {
Calendar now = Calendar.getInstance();
String yyyy = String.valueOf(now.get(Calendar.YEAR));
String mm = String.valueOf(now.get(Calendar.MONTH) + 1);
String dd = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
mm = (1 == mm.length()) ? ("0" + mm) : mm;
return yyyy + "-" + mm;
}
/**
* 时间格式:2005-01-20
*/
public static String ymdStr() {
Calendar now = Calendar.getInstance();
String yyyy = String.valueOf(now.get(Calendar.YEAR));
String mm = String.valueOf(now.get(Calendar.MONTH) + 1);
String dd = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
mm = (1 == mm.length()) ? ("0" + mm) : mm;
dd = (1 == dd.length()) ? ("0" + dd) : dd;
return yyyy + "-" + mm + "-" + dd;
}
/**
* 将给定的日期增加相应年
*
* @param date
* 待增加日期
* @param year
* 需要增加的年数
* @return 格式yyyy-MM-dd
* @throws ParseException
*/
public static String addOneYearForYmdStr(String date, int year)
throws ParseException {
Calendar now = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = dateFormat.parse(date);
now.setTime(d);
String yyyy = String.valueOf(now.get(Calendar.YEAR) + year);
String mm = String.valueOf(now.get(Calendar.MONTH) + 1);
String dd = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
mm = (1 == mm.length()) ? ("0" + mm) : mm;
dd = (1 == dd.length()) ? ("0" + dd) : dd;
return yyyy + "-" + mm + "-" + dd;
}
/**
* 时间格式:08:12:45
*/
public static String hisStr() {
Calendar now = Calendar.getInstance();
String hh = String.valueOf(now.get(Calendar.HOUR_OF_DAY));
String ii = String.valueOf(now.get(Calendar.MINUTE));
String ss = String.valueOf(now.get(Calendar.SECOND));
hh = (1 == hh.length()) ? ("0" + hh) : hh;
ii = (1 == ii.length()) ? ("0" + ii) : ii;
ss = (1 == ss.length()) ? ("0" + ss) : ss;
return hh + ":" + ii + ":" + ss;
}
/**
* 时间格式: 2005-01-23 08:12:45
*
* @return
*/
public static String nowTimeStr() {
return ymdStr() + " " + hisStr();
}
/**
* 根据格式获得当前日期
*
* @param pattern
* @return
*/
public static String getNowDateStrForPattern(String pattern) {
return new SimpleDateFormat(pattern).format(new Date());
}
/**
* 获取年 2006
*/
public static String yyyyStr() {
Calendar now = Calendar.getInstance();
now = Calendar.getInstance();
String yyyy = String.valueOf(now.get(Calendar.YEAR));
return yyyy;
}
/**
* 获取月 06
*/
public static String mmStr() {
Calendar now = Calendar.getInstance();
String mm = String.valueOf(now.get(Calendar.MONTH) + 1);
mm = (1 == mm.length()) ? ("0" + mm) : mm;
return mm;
}
/**
* 获取日
*/
public static String ddStr() {
Calendar now = Calendar.getInstance();
String dd = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
dd = (1 == dd.length()) ? ("0" + dd) : dd;
return dd;
}
/**
* 格式化SQL日期
*/
public static String format(String pattern, java.sql.Date date) {
return new SimpleDateFormat(pattern).format(date);
}
/**
* 格式化JAVA日期
*/
public static String format(String pattern, Date date) {
return new SimpleDateFormat(pattern).format(date);
}
/**
* 在当前日期加或减多少天得到新日期
*
* @param i
* 增加的天数
* @param operator
* :+ -
* @return 新日期
*/
public static String strToDateByInt(int i, String operator, String df) {
Date d = nowDate();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(d);
if ("+".equals(operator)) {
gc.add(GregorianCalendar.DATE, i);
} else if ("-".equals(operator)) {
gc.add(GregorianCalendar.DATE, (-i));
}
Date now = gc.getTime();
if (df.equals(""))
df = "yyyy-MM-dd hh:mm:ss"; // 默认输出时间格式
SimpleDateFormat sdf = new SimpleDateFormat(df);
return sdf.format(now);
}
/**
* 在指定日期上增加天数
*
* @param day
* @param operator
* @param df
* @return String
* @exception
*/
public static String strDateByInt(int day, String oldDate, String operator,
String df) {
if (!StringUtils.isEmpty(df)) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
if (!StringUtils.isEmpty(df))
format = new SimpleDateFormat(df);
Date d = null;
try {
d = format.parse(oldDate);
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(d);
if ("+".equals(operator)) {
gc.add(GregorianCalendar.DATE, day);
} else if ("-".equals(operator)) {
gc.add(GregorianCalendar.DATE, (-day));
}
Date now = gc.getTime();
return format.format(now);
} catch (Exception e) {
e.printStackTrace();
return "";
}
} else {
return "";
}
}
/**
* 计算某年某月有多少天
*
* @param year
* @param month
* @return
*/
public static int daysInMonth(int year, int month) {
if (month <= 0 || month > 12) {
return -1;
}
int days = 31;
if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
}
if (month == 2) {
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
days = 29;
} else {
days = 28;
}
}
return days;
}
/**
* 取得下个月
*
*/
public static void getNextMonth() {
try {
Calendar cl = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
cl.setTime(sdf.parse("200601"));
// cl.add(cl.MONTH, 1);
System.out.print(cl.get(Calendar.MONTH + 1));//
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 计算两个日期之间的月差
*/
public static int dispersionMonth2(String strDate1, String strDate2) {
int iMonth = 0;
int flag = 0;
try {
Calendar objCalendarDate1 = Calendar.getInstance();
objCalendarDate1.setTime(DateFormat.getDateInstance().parse(
strDate1));
Calendar objCalendarDate2 = Calendar.getInstance();
objCalendarDate2.setTime(DateFormat.getDateInstance().parse(
strDate2));
if (objCalendarDate2.equals(objCalendarDate1))
return 0;
if (objCalendarDate1.after(objCalendarDate2)) {
Calendar temp = objCalendarDate1;
objCalendarDate1 = objCalendarDate2;
objCalendarDate2 = temp;
}
if (objCalendarDate2.get(Calendar.DAY_OF_MONTH) < objCalendarDate1
.get(Calendar.DAY_OF_MONTH))
flag = 1;
if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1
.get(Calendar.YEAR))
iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1
.get(Calendar.YEAR))
* 12
+ objCalendarDate2.get(Calendar.MONTH) - flag)
- objCalendarDate1.get(Calendar.MONTH);
else
iMonth = objCalendarDate2.get(Calendar.MONTH)
- objCalendarDate1.get(Calendar.MONTH) - flag;
} catch (Exception e) {
}
return iMonth;
}
/**
* 两个时间之间相差距离多少天
* @return 相差天数
*/
public static long getDistanceDays(String str1, String str2){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date one;
Date two;
long days=0;
try {
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff ;
if(time1<time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
days = diff / (1000 * 60 * 60 * 24);
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
/**
* 返回日期中间的所有日期list形式
* @param str1
* @param str2
* @return
*/
public static List<Map<String,Object>> getDistanceDaysList(String str1, String str2){
List<Map<String,Object>> daysList = new ArrayList<Map<String,Object>>();
Map<String,Object> daysMap = null;
long dayFlag = compare_date(str1,str2);
if(dayFlag == 1){//str1大于str2日期(格式不正确)
return null;
}else if(dayFlag == 0){//两个日期一样
daysMap = new HashMap<String,Object>();
daysMap.put("date",str1);
daysMap.put("count","0");
daysList.add(daysMap);
return daysList;
} else{
long daysNum = getDistanceDays(str1,str2);
daysMap = new HashMap<String,Object>();
daysMap.put("date",str1);
daysMap.put("count","0");
daysList.add(daysMap);
for(int i = 1; i < daysNum; i++){
String dayStr = strDateByInt(i,str1,"+","yyyy-MM-dd");
daysMap = new HashMap<String,Object>();
daysMap.put("date",dayStr);
daysMap.put("count","0");
daysList.add(daysMap);
}
daysMap = new HashMap<String,Object>();
daysMap.put("date",str2);
daysMap.put("count","0");
daysList.add(daysMap);
}
return daysList;
}
/**
* 返回日期中间的所有日期Map形式
* @param str1
* @param str2
* @return
*/
public static Map<String,Object> getDistanceDaysMap(String str1, String str2){
Map<String,Object> daysMap = new LinkedHashMap<String,Object>();
long dayFlag = compare_date(str1,str2);
if(dayFlag == 1){//str1大于str2日期(格式不正确)
return null;
}else if(dayFlag == 0){//两个日期一样
daysMap.put(str1, "0");
return daysMap;
} else{
long daysNum = getDistanceDays(str1,str2);
daysMap.put(str1, "0");
for(int i = 1; i < daysNum; i++){
String dayStr = strDateByInt(i,str1,"+","yyyy-MM-dd");
daysMap.put(dayStr, "0");
}
daysMap.put(str2, "0");
}
return daysMap;
}
public static int dispersionDays2(String sEndDate, String sBeginDate)
throws Exception {
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");// 格式很重要:是20051031,还是2005-10-31格式呢?
if (sBeginDate.equals("0")) {
sBeginDate = "19000101";
}
calendar1.setTime(formatter1.parse(sBeginDate));
calendar2.setTime(formatter1.parse(sEndDate));
return (int) ((calendar2.getTimeInMillis() - calendar1
.getTimeInMillis()) / 1000 / 60 / 60 / 24);// 获取天数的差值。
}
public static String GMTToString(String dateGMT) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@SuppressWarnings("deprecation")
Date satrt = new Date(dateGMT);
String startStr = sdf.format(satrt);
return startStr;
}
public static String dateFormat(String dateStr) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat("HH:mm");
String startStr = sdf.format(date);
return startStr;
}
public static String dateFormat(String dateStr, String viewFormat)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat(viewFormat);
String startStr = sdf.format(date);
return startStr;
}
public static String getDateByLong(long LongTime) {
Date date = new Date(LongTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
/**
* 日期比较
*
* @param DATE1
* @param DATE2
* @return 0:两个日期一样 1:dt1 日期大。-1:dt1日期小。
*/
public static int compare_date(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() > dt2.getTime()) {
// System.out.println("dt1 在dt2前");
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
// System.out.println("dt1在dt2后");
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
/**
* 时间比较
* yyyy-MM-dd HH:mm:ss"
* @param DATE1
* @param DATE2
* @return 0:两个日期一样 1:dt1 日期大。-1:dt1日期小。
*/
public static int compare_date_time(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() > dt2.getTime()) {
// System.out.println("dt1 在dt2前");
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
// System.out.println("dt1在dt2后");
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
/**
* 获得当前日期所在季度
*
* @return 1:一季度,2:二季度,3:三季度,4:四季度
*/
public static int nowQuarter() {
Calendar now = Calendar.getInstance();
int month = now.get(Calendar.MONTH) + 1;
if (month == 1 || month == 2 || month == 3) {
return 1;
} else if (month == 4 || month == 5 || month == 6) {
return 2;
} else if (month == 7 || month == 8 || month == 9) {
return 3;
} else {
return 4;
}
}
/**
* 根据当前年月获取上个月时间
* @param date
* @return
* @throws Exception
*/
public static String getLastMonthDate(String date) throws Exception{
Calendar c = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
c.setTime(sf.parse(date));
c.add(Calendar.MONTH,-1);
return sf.format(c.getTime());
}
/**
* 根据当前年月获取几个月前(后)时间
* @param date
* @param number 月数 正数为后 负数为前
* @return
* @throws Exception
*/
public static String getSomeMonthDate(String date,int number) throws Exception{
Calendar c = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
c.setTime(sf.parse(date));
c.add(Calendar.MONTH,number);
return sf.format(c.getTime());
}
/**
* 根据当前年月获取上一年时间
* @param date
* @param format: 例:"yyyy-MM"
* @return
* @throws Exception
*/
public static String getLastYearDate(String date,String format) throws Exception{
Calendar c = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat(format);
c.setTime(sf.parse(date));
c.add(Calendar.YEAR,-1);
return sf.format(c.getTime());
}
/**
* a - b 的秒数
* 时间格式必须为 yyyy-MM-dd HH:mm:ss
* @param a
* @param b
* @return
*/
public static long getDaysFromTwoTime(String a,String b){
long days = 0;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date d1 = df.parse(a);
Date d2 = df.parse(b);
long diff = d1.getTime() - d2.getTime();
days = diff/(1000);
} catch (Exception e) {
e.printStackTrace();
}
return days;
}
public static String addDays(String date,int days) throws Exception{
Calendar c = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
c.setTime(sf.parse(date));
c.add(Calendar.DATE,days);
return sf.format(c.getTime());
}
/**
* 将yyyy-MM-dd转换为yyyy年MM月dd日
* @param date
* @return yyyy年MM月dd日
* @throws Exception
*/
public static String ymdprintStr(String date){
String printYmd="";
if(!StringUtils.isEmpty(date)){
if(date.length()>10){
date = date.substring(0, 10);
}
String[] dates =date.split("-");
printYmd=dates[0]+"年"+dates[1]+"月"+dates[2]+"日";
}
return printYmd;
}
/**
* 将yyyy-MM-dd转换为yyyyMMdd
* @param date
* @return yyyyMMdd
* @throws Exception
*/
public static String yyyymmddprintStr(String date){
String printYmd="";
if(!StringUtils.isEmpty(date)){
if(date.length()>10){
date = date.substring(0, 10);
}
String[] dates =date.split("-");
printYmd=dates[0]+dates[1]+dates[2];
}
return printYmd;
}
/**
* 开发区补贴发放记录导入功能
* 验证身份证号合法性
* 非空、18位合法
* @param idnumber
* @return
*/
public static boolean validateFfjlIdnumber(String idnumber){
if(!StringUtils.isEmpty(idnumber)&&idnumber.trim().length()==18){
return true;
}
return false;
}
/**
* 开发区补贴发放记录导入功能
* 验证发放金额合法性
* 非空、数字合法
* @param money
* @return
*/
public static boolean validateFfjlMoney(String money){
if(!StringUtils.isEmpty(money) && money.trim().matches("\\d+(\\.\\d+)?")){
return true;
}
return false;
}
/**
* 检验日期是否符合格式
* 支持格式: yyyy-M, yyyy-MM, yyyy.M, yyyy.MM, yyyy/M, yyyy/MM
* @param date
* @return
*/
public static boolean validateDateFormat(String date){
String regex = "^(\\d{4})-(0{0,1}\\d{1}|1[0-2])"
+ "|(\\d{4})\\.(0{0,1}\\d{1}|1[0-2])"
+ "|(\\d{4})/(0{0,1}\\d{1}|1[0-2])$";
Pattern pat = Pattern.compile(regex);
Matcher matcher = pat.matcher(date);
return matcher.find();
}
/**
* 检验日期是否符合格式
* 支持格式:yyyy-MM, yyyy.MM, yyyy/MM
* @param date
* @return
*/
public static boolean validateDateFormatYMD(String date){
String regex = "^(\\d{4})-(0{0,1}\\d{1}|1[0-2])-(0{0,1}\\d{1}|[12]\\d{1}|3[01])"
+ "|(\\d{4})\\.(0{0,1}\\d{1}|1[0-2])\\.(0{0,1}\\d{1}|[12]\\d{1}|3[01])"
+ "|(\\d{4})/(0{0,1}\\d{1}|1[0-2])/(0{0,1}\\d{1}|[12]\\d{1}|3[01])$";
Pattern pat = Pattern.compile(regex);
Matcher matcher = pat.matcher(date);
return matcher.find();
}
}
Message 消息返回接口(状态,信息,url,数据,状态码)
package com.zxy.common;
import java.io.Serializable;
/*
* 定制统一消息返回接口
* */
public class Message implements Serializable {
private boolean success;
private String message;
private String url;
private Object data;
private int code;
public Message(boolean success, String message, String url, Object data, int code) {
this.success = success;
this.message = message;
this.url = url;
this.data = data;
this.code = code;
}
public static Message success(String msg){
return new Message(true,msg,"",null,200);
}
public static Message success(String msg,Object data){
return new Message(true,msg,"",data,200);
}
public static Message success(String msg,Object data,String url){
return new Message(true,msg,url,data,200);
}
public static Message fail(String msg){
return new Message(false,msg,"",null,400);
}
public static Message fail(String msg,Object data){
return new Message(false,msg,"",data,200);
}
public static Message fail(String msg,Object data,String url){
return new Message(false,msg,url,data,200);
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
GlobalExceptionHandler 异常处理
package com.zxy.common;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolationException;
/*
* 公共异常处理器
* */
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* post请求参数校验抛出的异常
* @param e
* @return
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public Message methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
//获取异常中随机一个异常信息
String defaultMessage = e.getBindingResult().getFieldError().getDefaultMessage();
return Message.fail(defaultMessage);
}
/**
* get请求参数校验抛出的异常
* @param e
* @return
*/
@ExceptionHandler(BindException.class)
public Message bindExceptionHandler(BindException e){
//获取异常中随机一个异常信息
String defaultMessage = e.getBindingResult().getFieldError().getDefaultMessage();
return Message.fail(defaultMessage);
}
/**
* 请求方法中校验抛出的异常
* @param e
* @return
*/
@ExceptionHandler(ConstraintViolationException.class)
public Message constraintViolationExceptionHandler(ConstraintViolationException e){
//获取异常中第一个错误信息
String message = e.getConstraintViolations().iterator().next().getMessage();
return Message.fail(message);
}
}
配置登录拦截器
package com.zxy.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 注册登录拦截配置
* 拦截/admin下的路径,除了/admin/login和 /admin/register
*/
@Configuration
public class AdminMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AdminLoginHandlerInterceptor()).
addPathPatterns("/admin/**").
excludePathPatterns("/admin/login", "/admin/register");
}
}
package com.zxy.config;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* admin拦截器
* 验证session中是否登录admin
* 已登录,继续访问
* 未登录,返回登录页面
* author:Zxy
*/
public class AdminLoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object admin = request.getSession().getAttribute("admin");
if (admin != null) {
return true;
}
String url = "/admin/login";
response.sendRedirect(url);
// request.getRequestDispatcher(url).forward(request,response);
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}