SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例
1.項(xiàng)目文件目錄一欄

2.開始工作
先按照上圖建立好相應(yīng)的controller,mapper等文件。
接著進(jìn)行一個(gè)配置
首先是application.properties
server.port=8080#啟動(dòng)端口 #加載Mybatis配置文件 mybatis.mapper-locations = classpath:mapper/*.xml #數(shù)據(jù)源必填項(xiàng) spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/studentmanage?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT spring.datasource.username = root spring.datasource.password = 123456
接著是spring啟動(dòng)主函數(shù)java文件
/*掃描mapper,防止找不到dao層文件,就寫上*/
@MapperScan(basePackages = {"com.example.learn.Dao"})
@SpringBootApplication
public class LearnApplication {
public static void main(String[] args) {
SpringApplication.run(LearnApplication.class, args);
}
}
配置好以后開始從數(shù)據(jù)庫(kù)出發(fā)
用戶表結(jié)構(gòu)(表名:user_info)

根據(jù)用戶表,在entity建立實(shí)體類User.java,編譯器都有相應(yīng)的操作可以一鍵生成getter,setter,toString,只需定義好變量即可。
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
接著是Dao層
import com.example.learn.Entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@Mapper
@Component(value = "userDao")//這些都為配置注釋
public interface UserDao {
//登錄
public User login(@Param("username") String username,@Param("password") String password);
}
dao層寫完,在寫dao層配套的sql語句(mybatis功能)
mapper包里面建立.xml文件
<?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.example.learn.Dao.UserDao">
<select id="login" resultType="com.example.learn.Entity.User">
select * from user_info where username=#{username} and password=#{password}
</select>
</mapper>
弄完這些接著到service層,包括impl接口實(shí)現(xiàn)
首先是service接口
import com.example.learn.Entity.User;
public interface UserService {
public User login(String username, String password);
}
接著是Impl下的實(shí)現(xiàn)類
import com.example.learn.Dao.UserDao;
import com.example.learn.Entity.User;
import com.example.learn.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("UserService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User login(String username, String password) {
return userDao.login(username,password);
}
}
最后就是Controller
import com.example.learn.Entity.User;
import com.example.learn.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")//可以換成@RequestMapping,action那里使用的命名
public String login(HttpServletRequest request, User u){
String username=request.getParameter("username");
String password=request.getParameter("password");
u=userService.login(username,password);
if(u!=null){
return "success";//到success的動(dòng)態(tài)網(wǎng)頁(yè)
}
return "redirect:/login.html";//這里是定向到login.html靜態(tài)網(wǎng)頁(yè)
}
}
3.測(cè)試工作
先在表中插入測(cè)試數(shù)據(jù)用戶和密碼
再建立兩個(gè)html來測(cè)試功能
static/login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陸</title>
</head>
<body>
<form method="post" action="/login">
<input type="text" name="username" placeholder="用戶名">
<input type="password" name="password" placeholder="密碼">
<input type="submit" value="登錄">
</form>
</body>
</html>
templates/success
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 成功 </body> </html>
登錄測(cè)試


到此這篇關(guān)于SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例的文章就介紹到這了,更多相關(guān)SpringBoot+mybatis+thymeleaf登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot+MyBatis實(shí)現(xiàn)登錄案例
- SpringBoot+Mybatis實(shí)現(xiàn)登錄注冊(cè)的示例代碼
- IDEA下創(chuàng)建SpringBoot+MyBatis+MySql項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)登錄與注冊(cè)功能
- 如何利用IDEA搭建SpringBoot項(xiàng)目整合mybatis實(shí)現(xiàn)簡(jiǎn)單的登錄功能
- springboot+thymeleaf+druid+mybatis 多模塊實(shí)現(xiàn)用戶登錄功能
- springboot mybatis-plus實(shí)現(xiàn)登錄接口
相關(guān)文章
Java?BasePooledObjectFactory?對(duì)象池化技術(shù)的使用
這篇文章主要介紹了Java?BasePooledObjectFactory?對(duì)象池化技術(shù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
mybatis框架之mybatis中dao層開發(fā)的兩種方法
這篇文章主要介紹了mybatis框架之mybatis中dao層開發(fā)的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
使用 Spring Boot 2.0 + WebFlux 實(shí)現(xiàn) RESTful API功能
什么是 Spring WebFlux, 它是一種異步的, 非阻塞的, 支持背壓(Back pressure)機(jī)制的Web 開發(fā)框架.下面通過本文給大家介紹使用 Spring Boot 2.0 + WebFlux 實(shí)現(xiàn) RESTful API功能,需要的朋友參考下吧2018-01-01
SpringBoot 如何優(yōu)雅的實(shí)現(xiàn)跨服務(wù)器上傳文件的示例
這篇文章主要介紹了SpringBoot 如何優(yōu)雅的實(shí)現(xiàn)跨服務(wù)器上傳文件的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2006-11-11
Java基于Google zxing生成帶logo的二維碼圖片
zxing是一個(gè)開放源碼的,用java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫(kù),本文主要介紹了Java基于Google zxing生成帶logo的二維碼圖片,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
java實(shí)現(xiàn)后臺(tái)處理base64圖片還原為文件
這篇文章主要介紹了java實(shí)現(xiàn)后臺(tái)處理base64圖片還原為文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

