〇、參考資料
1、hutool介紹
https://blog.csdn.net/abst122/article/details/124091375
2、Spring Boot+Mybatis實作登錄注冊
https://www.cnblogs.com/wiki918/p/16221758.html
3、Spring Boot讀取自定義組態檔
https://www.yisu.com/zixun/366877.html
4、Spring Boot讀取properties組態檔的兩種方式
https://blog.csdn.net/weixin_42352733/article/details/121830775
一、概述
1、技術堆疊
Spring Boot+Mybatis+Lombok+Hutool+Slf4j+thymeleaf
2、專案截圖

二、登錄注冊(后臺)
1、資料庫設計
表結構:

表資料:

建表陳述句:
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '用戶表id',
`username` varchar(50) NOT NULL COMMENT '用戶名',
`password` varchar(50) NOT NULL COMMENT '用戶密碼,MD5加密',
`email` varchar(50) DEFAULT NULL COMMENT '郵箱',
`phone` varchar(20) DEFAULT NULL COMMENT '手機號',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后一次更新時間',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用戶表'
2、POJO(Entity)撰寫-UserBean.java
package com.boulderaitech.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
@Data //提供了set、get方法及toString
@AllArgsConstructor
@NoArgsConstructor
public class UserBean implements Serializable {
private Integer id; //為什么用Integer,不用int
private String username;
private String password;
private String email;
private String phone;
@JsonFormat(pattern = "yyyy-MM-mm HH:mm:ss")
private Date create_time;
@JsonFormat(pattern = "yyyy-MM-mm HH:mm:ss")
private Date update_time;
}
3、Controller撰寫-UserController.java
package com.boulderaitech.controller;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import com.boulderaitech.entity.UserBean;
import com.boulderaitech.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Slf4j
@Controller // 不能用@RestController
public class UserController {
//將Service注入Web層
@Autowired
UserService userService;
//用戶測驗
@RequestMapping("/hello")
public String hello() {
return "login";
}
@RequestMapping("/register")
public String register() {
return "signup";
}
@RequestMapping(value = "https://www.cnblogs.com/login", method = RequestMethod.POST)
public String login(String username,String password) {
UserBean userBean = userService.login(username,password);
log.info("username:{}",username);
log.info("password:{}",password);
//hutool-core 核心,包括Bean操作、日期、各種Util等
if(StrUtil.isNotEmpty(username)) {
if(userBean != null) {
return "success";
//方法參考-遍歷集合
//Opt.ofEmptyAble(userBean).ifPresent(System.out::println);
}
} else {
return "用戶名不允許為空";
}
return "error";
}
@RequestMapping(value = "https://www.cnblogs.com/signup", method = RequestMethod.POST)
public String signup(String username,String password) {
userService.insert(username,password);
return "success";
}
}
4、Service撰寫-UserService.java
package com.boulderaitech.service;
import com.boulderaitech.entity.UserBean;
import com.boulderaitech.mapper.UserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
//將dao層屬性注入service層,為什么不用Autowired
@Resource
private UserMapper userMapper;
public UserBean login(String username, String password) {
return userMapper.getInfo(username,password);
}
public void insert(String username, String password) {
userMapper.saveUser(username,password);
}
}
5、Mapper撰寫-UserMapper.java
package com.boulderaitech.mapper;
import com.boulderaitech.entity.UserBean;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper //添加Mapper注解,就不用寫xml的mapper映射檔案了
public interface UserMapper {
//多個引數要加@Param修飾
//思考:xml中的<include>代碼片段怎么配
@Select("SELECT * FROM user WHERE username=#{username} AND password= #{password}")
UserBean getInfo(@Param("username") String username,@Param("password") String password);
@Insert("INSERT INTO user(username,password) VALUE(#{username},#{password})")
void saveUser(@Param("username") String username,@Param("password") String password);
}
6、組態檔撰寫-application.properties
# Spring Boot埠號
server.port=9088
# 資料源配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=qaz123
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://192.168.40.111:3306/visualization?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.thymeleaf.prefix=classpath:/templates/
7、啟動類撰寫-KettleProcessorApplication.java
package com.boulderaitech;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot啟動類,加Spring Boot注解,呼叫Spring的靜態run方法
*/
@SpringBootApplication
public class KettleProcessorApplication {
public static void main(String[] args) {
SpringApplication.run(KettleProcessorApplication.class);
}
}
三、登錄注冊(前臺)
1、登錄頁面-login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form role="form" action = "/login" method="post">
賬號:<input type="text" id="username" name = "username"> <br>
密碼:<input type="password" id = "password" name = "password"> <br>
<input type="submit" id = "login" value = "https://www.cnblogs.com/liujinhui/archive/2022/11/30/登錄">
</form>
<a href="https://www.cnblogs.com/register">注冊</a>
</body>
</html>
2、注冊頁面-signup.html
<!--注冊頁面-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注冊</title>
</head>
<body>
<form role="form" action="/signup" method="post">
請輸入姓名:<input type="text" name="username" id="name"><br>
請輸入密碼:<input type="password" name="password" id="password"><br>
<input type="submit" name="sign" value="https://www.cnblogs.com/liujinhui/archive/2022/11/30/提交">
</form>
</body>
</html>
3、成功頁面-success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<h1>歡迎,恭喜登錄成功/注冊成功</h1>
</body>
</html>
4、失敗頁面-error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1>登錄失敗!</h1>
</body>
</html>
四、配置讀取
1、配置撰寫-kettle.properties
# 讀取properties的兩種方式:https://blog.csdn.net/weixin_42352733/article/details/121830775
environment=xuelei-www
kettle.repository.type=database
kettle.repository.username=admin
kettle.repository.password=admin
2、POJO(Entity)撰寫-KettleRepositoryBean.java
package com.boulderaitech.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "kettle.repository")
public class KettleRepositoryBean {
private String type;
private String username;
private String password;
}
3、Controller撰寫-PropertiesController.java
package com.boulderaitech.controller;
import com.boulderaitech.entity.KettleRepositoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController //Controller和RestCOntroller的區別
@PropertySource("classpath:kettle.properties") //默認是application.properties
public class PropertiesController {
@Value("${environment}")
private String envName;
@Autowired
private KettleRepositoryBean kettleRepositoryBean;
@RequestMapping("/getEnv")
public String getEnv() {
return "hello " + envName;
}
@RequestMapping("/getRepoInfo")
public String getRepoInfo() {
return "hello " + kettleRepositoryBean.toString();
}
}
五、驗證
1、登錄


2、注冊


3、讀取單個配置

4、讀取物體類配置(多個)

本文來自博客園,作者:哥們要飛,轉載請注明原文鏈接:https://www.cnblogs.com/liujinhui/p/16937229.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/538708.html
標籤:其他
