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ù)庫出發(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)頁
}
return "redirect:/login.html";//這里是定向到login.html靜態(tài)網(wǎng)頁
}
}
3.測試工作
先在表中插入測試數(shù)據(jù)用戶和密碼
再建立兩個(gè)html來測試功能
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>
登錄測試


到此這篇關(guān)于SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例的文章就介紹到這了,更多相關(guān)SpringBoot+mybatis+thymeleaf登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot+MyBatis實(shí)現(xiàn)登錄案例
- SpringBoot+Mybatis實(shí)現(xiàn)登錄注冊的示例代碼
- IDEA下創(chuàng)建SpringBoot+MyBatis+MySql項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)登錄與注冊功能
- 如何利用IDEA搭建SpringBoot項(xiàng)目整合mybatis實(shí)現(xiàn)簡單的登錄功能
- springboot+thymeleaf+druid+mybatis 多模塊實(shí)現(xiàn)用戶登錄功能
- springboot mybatis-plus實(shí)現(xiàn)登錄接口
相關(guān)文章
看動(dòng)畫學(xué)算法之Java實(shí)現(xiàn)doublyLinkedList
這篇文章主要介紹Java實(shí)現(xiàn)doublyLinkedList,LinkedList:doublyLinkedList相對比較復(fù)雜,今天就來簡單學(xué)習(xí)一下doublyLinkedList的基本操作和概,感興趣的小伙伴可以參考下面具體文章內(nèi)容2021-10-10
Java計(jì)算兩個(gè)時(shí)間段的差的實(shí)例詳解
在本篇內(nèi)容中,我們給大家整理了關(guān)于Java計(jì)算兩個(gè)時(shí)間段的差的實(shí)例內(nèi)容,并做了詳細(xì)分析,有需要的朋友們學(xué)習(xí)下。2022-11-11
Spring Cache @Cacheable 緩存在部分Service中不生效的解決辦法
這篇文章主要介紹了Spring Cache @Cacheable 緩存在部分Service中不生效的解決辦法2023-10-10
IDEA解決maven包沖突easypoi NoClassDefFoundError的問題
這篇文章主要介紹了IDEA解決maven包沖突easypoi NoClassDefFoundError的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Java實(shí)現(xiàn)FTP文件與文件夾的上傳和下載
本文主要分享了Java實(shí)現(xiàn)文件上傳和下載的具體實(shí)例,分為單個(gè)文件的上傳與下載和整個(gè)文件夾的上傳與下載。具有很好的參考價(jià)值,需要的朋友一起來看下吧2016-12-12
java之向linux文件夾下寫文件無權(quán)限的問題
這篇文章主要介紹了java之向linux文件夾下寫文件無權(quán)限的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
mybatis.type-aliases-package之巨坑的解決
這篇文章主要介紹了mybatis.type-aliases-package之巨坑的解決,具有很好的參考價(jià)值,希望對大家有所幫助。2021-09-09
JVM性能調(diào)優(yōu)實(shí)戰(zhàn):讓你的IntelliJ Idea縱享絲滑
這篇文章主要介紹了JVM性能調(diào)優(yōu)實(shí)戰(zhàn):讓你的IntelliJ Idea縱享絲滑的相關(guān)資料,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
SWT(JFace) FTP客戶端實(shí)現(xiàn)
SWT(JFace)小制作:FTP客戶端實(shí)現(xiàn)2009-06-06

