新建springboot项目,选择Spring Initializr,更改Group和Artifact,然后选maven,选Java8,导入五个依赖,
分别是Lombok,Spring Web,thymeleaf,MySql,Mybatis-Plus,前四个直接选,最后一个在官网导入,此项目要利用到redis数据库,redis数据库一般用于缓存
详细说明
1,lombok是java自动生成代码的插件。它能提高开发效率,减少自己编写繁琐的代码,让代码看起来更整洁简略,比如getter、setter、equals以及construct等方法。其也有val、var这种自动判断变量类型的变量定义方式(类似javascript中的let、const)。
2,Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。
然后编辑配置文件,用yml文件格式进行的编辑
spring:
#thymeleaf配置
thymeleaf:
#关闭缓存
cache: false
#数据源配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
hikari:
#连接池名
pool-name: DateHikariCP
#最小空闲连接数
minimum-idle: 5
#空闲连接存活最大时间,默认600000(10分钟)
idle-timeout: 1800000
#最大连接数,默认10
maximum-pool-size: 10
#从连接池返回的连接自动提交
auto-commit: false
#连接最大存活时间,0表示永久存活,默认1800000(30分钟)
max-lifetime: 1800000
#连接超时时间,默认30000(30秒)
connection-timeout: 3000
#测试连接是否可用的查询语句
connection-test-query: SELECT 1
#mybatis-plus配置
mybatis-plus:
#配置Mapper.xml映射文件
mapper-locations: classpath*:/mapper/*Mapper.xml
#配置Mybatis数据返回类型别名(默认别名是类名)
type-aliases-package: com.xxx.seckilldemo.pojo
#MyBatis SQL打印(方法接口所在的包,不是Mapper.xml所在的包)
logging:
level:
com.xxx.seckill.mapper: debug
在上述配置文件中,hikari是springboot自带的据说响应很快的数据库连接池
然后创建了四个包,分别是controller,service,mapper,pojo,接着在启动类中加入了注解
@MapperScan("com.xxx.seckilldemo.pojo")
@MapperScan 是一个注解,通常在使用Spring框架的应用中与MyBatis结合使用。它的作用是告诉Spring在哪里扫描MyBatis的Mapper接口,并将它们注册为Spring的Bean,以便在应用中使用。
配置完成后需要进行测试,在controller文件夹中创建DemoController测试类,测试配置是否有问题
package com.xxx.seckilldemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author chenyingcai
* @version 1.0
* @project seckill-demo
* @description
* @date 2023/10/7 20:18:55
*/
@Controller
@RequestMapping("/demo")
public class DemoController {
/**
* 测试页面跳转
*/
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("name","xxxx");
return "hello";
}
}
既然用了thymeleaf,那么所有的页面必定在templates中,因此创建hello.html,代码如下
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>测试</title> </head> <body> <p th:text="'hello ' + ${name}"></p> </body> </html>
其中xmlns:th="http://www.thymeleaf.org"为thymeleaf命名空间,可以参考https://blog.csdn.net/WH_13_14_0_/article/details/113530572
或者参考thymeleaf命名空间和依赖导入_th命名空间怎么导-CSDN博客
<p th:text="'hello ' + ${name}"></p>是其中具体的内容
