常用代码片段

[toc]

一、SSM

1、db.properties(数据库配置信息)

MySQL-5.0:

1
2
3
4
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/myblogs
user=root
password=root

MySQL-8.0:

1
2
3
4
5
6
7
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/helloworld?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
jdbc.user=root
jdbc.pwd=root
jdbc.initialSize=1
jdbc.maxActive=50
jdbc.maxWait=30000

2、web.xml(Servlet 容器的配置文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!--<!DOCTYPE web-app PUBLIC-->
<!-- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"-->
<!-- "http://java.sun.com/dtd/web-app_2_3.dtd" >-->

<web-app>
<display-name>Archetype Created Web Application</display-name>

<!-- 注册Spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring.xml</param-value>
</context-param>

<!-- 编码过滤器-->
<!--
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>-->

<!-- 将特定的post请求 转化为 put 和 delete 请求方式
(需要结合form表单的一些配置来共同完成)
-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--监听器-->
<!-- <listener>-->
<!-- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
<!-- </listener>-->

<!-- DispacherServlet 前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

3、applicationContext.xml(Spring 配置文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">

<!-- 开启IoC的组件扫描,-->
<!--
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
-->
<context:component-scan base-package="com.cyw"/>

<!-- 开启AOP自动代理-->
<aop:aspectj-autoproxy/>

<!-- 导入db.properties文件中的数据库配置谢谢-->
<context:property-placeholder location="classpath:conf/db.properties"/>

<!-- 利用数据库配置信息,来配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="maxWait" value="${jdbc.maxWait}" />
</bean>



<!-- 配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 配置事务管理器的注解驱动-->
<tx:annotation-driven transaction-manager="txManager"/>

<!-- 配置Mybatis 的 sqlSessionFactory(单例)-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:conf/mybatis-config.xml"/>
<!-- <property name="mapperLocations" value="classpath:com/cyw/mapper/*.xml"/>

</bean>

<!-- 扫描dao接口的配置文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cyw.mapper"/>
</bean>

</beans>

4、springMVC.xml(SpringMVC 配置文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">

<!-- 组件扫描:控制器-->
<context:component-scan base-package="com.cyw.controller"/>


<!-- 配置:视图解析器-->
<!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/templates/"/>
<property name="suffix" value=".html"/>
</bean>
-->


<!-- 配置SpringMVC的:注解驱动-->
<mvc:annotation-driven/>


<!-- 用于判断DispacherServlet的请求是否映射-->
<mvc:default-servlet-handler/>


<!-- 放行:静态资源-->
<!-- <mvc:resources mapping="/js/**" location="/js"/>
<mvc:resources mapping="/css/**" location="/css"/>
<mvc:resources mapping="/images/**" location="/images"/>
-->


<!-- 简单的映射:路径与视图 -->
<!-- <mvc:view-controller path="/" view-name="index"/> -->

</beans>

5、Mybatis.xml(Mybatis配置文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<!-- 配置log4j -->
<settings>
<!-- 也可使用MySTDOUT_LOGGING -->
<setting name="logImpl" value="LOG4J"/>

</settings>
<!--
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="true"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
-->

<typeAliases>
<package name="com.cyw.pojo"/>
</typeAliases>


</configuration>

6、SSM的Maven依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?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>

<groupId>com.cyw</groupId>
<artifactId>myWeb-02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>myWeb-02 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

<build>
<finalName>myWeb-02</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

7、自定义返回的JSON结果集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.cyw.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
private int code;
private String msg;
private Object data;

public static Result ok(){
return new Result(200,null,null);
}

public static Result ok(String msg){
return new Result(200,msg,null);
}

public static Result ok(Object data){
return new Result(200,null,data);
}

public static Result ok(String msg,Object data){
return new Result(200,msg,data);
}

public static Result error(String msg){
return new Result(400,msg,null);
}

}

8、Mybatis-Mapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cyw.mapper.UserMapper">

<select id="getUserByUid" resultType="User">
select * from user where uid = #{uid}
</select>

<select id="login" resultType="User">
select * from user where username = #{username} and pwd=#{pwd}
</select>

</mapper>

9、POM.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

10、分页

前端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 查询
function findByPage(condition, pageNum) {
let queryPage = {
"condition": condition,
"curPageNum": pageNum,
"pageSize": 4,
}
$.post("/course/getCourseAll", queryPage, function (resp) {
// 拼接最终的html,渲染到页面
});
}


// 搜索
function search() {
let condition = $("#searchBox").val();
findByPage(condition, 1);
}

后端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public MyResult getClassAll(QueryConditionBean conditionBean, User user) {
Integer roleId = user.getRoleId();
List<Class> classAll = null;
// 分页
Page<Object> page = PageHelper.startPage(conditionBean.getCurPageNum(), conditionBean.getPageSize());

if (user.getRoleId()==0){
classAll = classMapper.getClassAll();
}else if(user.getRoleId()==1){
classAll = classMapper.getClassByTeacher(user.getId());
}else if(user.getRoleId()==2){
classAll = classMapper.getClassByStudent(user.getId());
}
// 没有对应班级信息情况
if (classAll == null || classAll.size() <= 0) {
return MyResult.error("暂无数据!");
}
PageBean<Class> pageBean = new PageBean<>();
pageBean.setTotalRows(page.getTotal());
pageBean.setCurPageNum(page.getPageNum());
pageBean.setPageSize(page.getPageSize());
pageBean.setTotalPages(page.getPages());
pageBean.setDataList(classAll);

return MyResult.ok("查询成功!", pageBean);
}

11、CORSConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.cyw.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
//添加映射路径
registry.addMapping("/**")
//是否发送Cookie
.allowCredentials(true)
//设置放行哪些原始域 SpringBoot2.4.4下低版本使用.allowedOrigins("*")
.allowedOriginPatterns("*")
//放行哪些请求方式
.allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
//.allowedMethods("*") //或者放行全部
//放行哪些原始请求头部信息
.allowedHeaders("*")
//暴露哪些原始请求头部信息
.exposedHeaders("*");
}
}

12、短信验证码

准备工作

由于要使用 SMS 短信验证码功能,需要借助云平台,因为腾讯云平台的审核更容易通过,因此,在这里使用腾讯云。

在申请 SMS 短信验证码发送服务时,需要有备案的网站、APP、微信公众号,为简化过程,此处申请了一个新的微信个人订阅号,用该订阅号的信息申请短信服务。

(1)注册微信订阅号(需要有未使用的邮箱来注册)

(2)实名认证腾讯云平台和微信订阅号

(3)在腾讯云平台,搜索 SMS,选择使用(试用套餐有200条左右的容量,有效期3~4个月)

(4)申请 SMS 的签名,记录签名(也就是微信订阅号的名字,代表是谁发的)

(5)申请 SMS 的模板,记录模板ID(短信的内容,使用标准模板来提高通过率,模板内的变量就是代码中动态输入的内容)

(6)在云平台的访问控制面板中,创建子账户来发送验证码(限制权限,记录下账户的 SecretID SecretKey

(7)在云平台的短信控制台中,应用管理菜单的应用列表中,记录下 SDKAppID

1
2
子账户用户名 cyw-user 
登录密码 x[s?Hs8/

搭建环境

Maven:

1
2
3
4
5
6
<!--腾讯云核心API-->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.560</version>
</dependency>

账户配置(yml):

1
2
3
4
5
6
txsms:
appId: 1400714069
secretId: AKIDGMFtH5fYCblhOpz0oOioAJEwQhjVzGNF
secretKey: gR2QYR7jJk3IQTOBsUwJk6Khx52AD8qI
signName: // 短信签名(小cyw软工学习簿)
templateId: 1490127

SmsUtils 工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.cyw.sms.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
// 注入 yml 配置文件中的值
@ConfigurationProperties(prefix = "txsms")
public class SmsUtils {
private String appId;
private String secretId;
private String secretKey;
private String signName;
private String templateId;


/**
* @param checkCode 验证码
* @param expire 有效时长(分钟)
* @param phoneNumberSet 接收验证码的手机号数组(以+86开头)
* @return 执行结果
*/
public String sendSms(String checkCode, String expire, String... phoneNumberSet) {
String rst = "";
try {
// 使用 secretId 和 secretKey
Credential cred = new Credential(this.secretId, this.secretKey);

// 固定写法
ClientProfile clientProfile = new ClientProfile();
clientProfile.setSignMethod("HmacSHA256");
SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
SendSmsRequest req = new SendSmsRequest();

// 使用appId
req.setSmsSdkAppId(this.appId);
// 使用在腾讯云平台申请的短信签名(也就是验证码短信的中括号中的主体名)
req.setSignName(this.signName);
// 使用在腾讯云平台申请的短信内容模板的 ID
req.setTemplateId(this.templateId);
// 在短信模板中填充验证码和有效时间(模板有几个变量,数组元素个数就是几个)
String[] templateParamSet = {checkCode, expire};
req.setTemplateParamSet(templateParamSet);
// 设置要接收验证码的手机号数组
req.setPhoneNumberSet(phoneNumberSet);

// 开始发送验证码
SendSmsResponse res = client.SendSms(req);
rst = SendSmsResponse.toJsonString(res);
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
return rst;
}
}

执行返回的结果:

1
2
3
4
5
6
7
8
9
10
11
12
{
"SendStatusSet": [{
"SerialNo": "2028:f825dbb27b0db9a84100",
"PhoneNumber": "+86137XXXXX4215",
"Fee": 1,
"SessionContext": "",
"Code": "Ok",
"Message": "send success",
"IsoCode": "CN"
}],
"RequestId": "0344b104-ac66-4576-a66d-16845c657541"
}

13、SpringBoot 输出 SQL日志

application.yml中:

1
2
3
logging:
level:
com.cyw.mapper: debug

二、常用的 Maven 依赖

1、Spring-Maven 依赖

aspectj-weaver(AOP的依赖) + spring-webmvc + spring-jdbc + spring-tx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.15</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.15</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.15</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.15</version>
</dependency>

2、Mybatis-Spring-Maven 依赖

1
2
3
4
5
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>

3、Druid-Maven 依赖

db.properties:

1
2
3
4
5
6
7
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/myblogs?characterEncoding=utf8&useSSL=false
user=root
password=root
initialSize=1
maxActive=50
maxWait=10000

pom.xml:

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>

4、MySQL-Maven 依赖

1
2
3
4
5
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>

5、Jackson-Maven 依赖

1
2
3
4
5
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.1</version>
</dependency>

6、FastJSON-Maven依赖

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>

7、lombok-Maven依赖

1
2
3
4
5
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>

8、Servlet-Maven依赖

(1)

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>

(2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
</exclusion>
</exclusions>
</dependency>

9、POI-Maven依赖

操作Office

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>

10、SpringSecurity-Maven依赖

1
2
3
4
5
6
7
8
9
10
11
<!--        SpringSecurity依赖-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.5.1</version>
</dependency>

11、日志

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

12、Mybatis Plus-maven依赖

1
2
3
4
5
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.3.4</version>
</dependency>

13、SpringBoot 依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
   <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>



<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


三、HTML

1、HTML获取ContextPath

1
2
3
4
5
6
7

function getContextPath(){
var pathName = document.location.pathname;
var index = pathName.substr(1).indexOf("/");
var result = pathName.substr(0,index+1);
return result;
}

2、滑块验证码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<html lang="zh-cn">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>滑块验证码</title>
<style>
* {
margin: 0px;
padding: 0px;
font-family: "微软雅黑";
}

.drag {
width: 300px;
height: 40px;
line-height: 40px;
background-color: #e8e8e8;
position: relative;
margin: 0 auto;
}

.bg {
width: 40px;
height: 100%;
position: absolute;
background-color: #75CDF9;
}

.text {
position: absolute;
width: 100%;
height: 100%;
text-align: center;
user-select: none;
}

.btn {
width: 40px;
height: 38px;
position: absolute;
border: 1px solid #ccc;
cursor: move;
font-family: "宋体";
text-align: center;
background-color: #fff;
user-select: none;
color: #666;
}
</style>
</head>
<body>
<div class="drag">
<div class="bg"></div>
<div class="text" onselectstart="return false">请拖动滑块解锁</div>
<div class="btn">&gt;&gt;</div>
</div>
<script>
//一、定义一个获取DOM元素的方法
var $ = function (selector) {
return document.querySelector(selector);
},
box = $(".drag"),//容器
bg = $(".bg"),//背景
text = $(".text"),//文字
btn = $(".btn"),//滑块
success = false,//是否通过验证的标志

distance = box.offsetWidth - btn.offsetWidth;//滑动成功的宽度(距离)


//二、给滑块注册鼠标按下事件
btn.onmousedown = function (e) {
//1.鼠标按下之前必须清除掉后面设置的过渡属性
btn.style.transition = "";
bg.style.transition = "";

//说明:clientX 事件属性会返回当事件被触发时,鼠标指针向对于浏览器页面(或客户区)的水平坐标。

//2.当滑块位于初始位置时,得到鼠标按下时的水平位置
var e = e || window.event;
var downX = e.clientX;
//三、给文档注册鼠标移动事件
document.onmousemove = function (e) {

// 是为了更好的兼容IE浏览器和非ie浏览器。
// 在ie浏览器中,window.event是全局变量,
// 在非ie中,就需要自己传入一个参数来获取event啦,
// 所以就有了var e = e||window.event
var e = e || window.event;


//1.获取鼠标移动后的水平位置
var moveX = e.clientX;

//2.得到鼠标水平位置的偏移量(鼠标移动时的位置 - 鼠标按下时的位置)
var offsetX = moveX - downX;

//3.在这里判断一下:鼠标水平移动的距离 与 滑动成功的距离 之间的关系
if (offsetX > distance) {
offsetX = distance; //如果滑过了终点,就将它停留在终点位置
} else if (offsetX < 0) {
offsetX = 0; //如果滑到了起点的左侧,就将它重置为起点位置
}

//4.根据鼠标移动的距离来动态设置滑块的偏移量和背景颜色的宽度
btn.style.left = offsetX + "px";
bg.style.width = offsetX + "px";

// 如果鼠标的水平移动距离 = 滑动成功的宽度
if (offsetX == distance) {

//1.设置滑动成功后的样式
text.innerHTML = "验证通过";
text.style.color = "#fff";
btn.innerHTML = "&radic;";
btn.style.color = "green";
bg.style.backgroundColor = "lightgreen";

//2.设置滑动成功后的状态
success = true;
// 成功后,清除掉鼠标按下事件和移动事件(因为移动时并不会涉及到鼠标松开事件)
btn.onmousedown = null;
document.onmousemove = null;

//3.成功解锁后的回调函数
setTimeout(function () {
alert('解锁成功!');
}, 100);
}
}

//四、给文档注册鼠标松开事件
document.onmouseup = function (e) {
// 如果鼠标松开时,滑到了终点,则验证通过
if (success) {
return;
} else {
// 反之,则将滑块复位(设置了1s的属性过渡效果)
btn.style.left = 0;
bg.style.width = 0;
btn.style.transition = "left 1s ease";
bg.style.transition = "width 1s ease";
}
// 只要鼠标松开了,说明此时不需要拖动滑块了,那么就清除鼠标移动和松开事件。
document.onmousemove = null;
document.onmouseup = null;
}

}
</script>
</body>
</html>

3、富文本编辑器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>首页</title>

<link href="css/wangedit.css" rel="stylesheet">
<script src="js/wangedit.js"></script>
<script src="js/jquery.js"></script>

<style>
#editor—wrapper {
margin:50px auto;
width:80%;
border: 1px solid #ccc;
z-index: 100; /* 按需定义 */
}
#toolbar-container {
border-bottom: 1px solid #ccc;
}
#editor-container {
height: 400px;
}
</style>
</head>
<body>

<h1>首页</h1><br>

<div>保存博客-按钮</div><br>

<div id="editor—wrapper">
<!-- 工具栏 -->
<div id="toolbar-container"></div>
<!-- 编辑器 -->
<div id="editor-container"></div>
</div>

<script>
const { createEditor, createToolbar } = window.wangEditor

// 编辑器的配置
const editorConfig = {
placeholder: '在这里输入...',
onChange(editor) {
// 输入的内容转化为HTML格式
const html = editor.getHtml();

// 提交文本内容
$.post("/blog/save",{
"html": html,
},function(res){
console.log(res);
});

}
}

// 创建编辑器
const editor = createEditor({
selector: '#editor-container',
html: '<p><br></p>',
config: editorConfig,
mode: 'simple', // 'default' or 'simple'
});

// 工具栏的配置
const toolbarConfig = {}

// 创建工具栏
const toolbar = createToolbar({
editor,
selector: '#toolbar-container',
config: toolbarConfig,
mode: 'default', // or 'simple'
});
</script>
</body>
</html>