欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例

 更新時(shí)間:2020年07月15日 09:31:41   作者:MarcusRossi  
這篇文章主要介紹了SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1.項(xiàng)目文件目錄一欄

2.開(kāi)始工作

先按照上圖建立好相應(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層文件,就寫(xiě)上*/
@MapperScan(basePackages = {"com.example.learn.Dao"})
@SpringBootApplication
public class LearnApplication {

  public static void main(String[] args) {
    SpringApplication.run(LearnApplication.class, args);
  }

}

配置好以后開(kāi)始從數(shù)據(jù)庫(kù)出發(fā)

用戶表結(jié)構(gòu)(表名:user_info)

根據(jù)用戶表,在entity建立實(shí)體類(lèi)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層寫(xiě)完,在寫(xiě)dao層配套的sql語(yǔ)句(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)類(lèi)

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來(lái)測(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java中ssj框架的項(xiàng)目搭建流程

    java中ssj框架的項(xiàng)目搭建流程

    這篇文章主要介紹了java中ssj框架的項(xiàng)目搭建流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Java?BasePooledObjectFactory?對(duì)象池化技術(shù)的使用

    Java?BasePooledObjectFactory?對(duì)象池化技術(shù)的使用

    這篇文章主要介紹了Java?BasePooledObjectFactory?對(duì)象池化技術(shù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • 通過(guò)實(shí)例解析Socket套接字通信原理

    通過(guò)實(shí)例解析Socket套接字通信原理

    這篇文章主要介紹了通過(guò)實(shí)例解析Socket套接字通信原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • mybatis框架之mybatis中dao層開(kāi)發(fā)的兩種方法

    mybatis框架之mybatis中dao層開(kāi)發(fā)的兩種方法

    這篇文章主要介紹了mybatis框架之mybatis中dao層開(kāi)發(fā)的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 使用 Spring Boot 2.0 + WebFlux 實(shí)現(xiàn) RESTful API功能

    使用 Spring Boot 2.0 + WebFlux 實(shí)現(xiàn) RESTful API功能

    什么是 Spring WebFlux, 它是一種異步的, 非阻塞的, 支持背壓(Back pressure)機(jī)制的Web 開(kāi)發(fā)框架.下面通過(guò)本文給大家介紹使用 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ù)器上傳文件的示例

    這篇文章主要介紹了SpringBoot 如何優(yōu)雅的實(shí)現(xiàn)跨服務(wù)器上傳文件的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2006-11-11
  • Java基于Google zxing生成帶logo的二維碼圖片

    Java基于Google zxing生成帶logo的二維碼圖片

    zxing是一個(gè)開(kāi)放源碼的,用java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫(kù),本文主要介紹了Java基于Google zxing生成帶logo的二維碼圖片,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • Java接口異步調(diào)用優(yōu)化技巧詳解

    Java接口異步調(diào)用優(yōu)化技巧詳解

    本文詳細(xì)介紹了在Java開(kāi)發(fā)中,如何通過(guò)異步調(diào)用等技巧來(lái)優(yōu)化接口的性能,有效避免阻塞和提高并發(fā)處理能力,提升系統(tǒng)的穩(wěn)定性和響應(yīng)速度
    2023-05-05
  • java實(shí)現(xiàn)后臺(tái)處理base64圖片還原為文件

    java實(shí)現(xiàn)后臺(tái)處理base64圖片還原為文件

    這篇文章主要介紹了java實(shí)現(xiàn)后臺(tái)處理base64圖片還原為文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java流程控制語(yǔ)句最全匯總(上篇)

    Java流程控制語(yǔ)句最全匯總(上篇)

    這篇文章主要介紹了Java流程控制語(yǔ)句最全匯總(上篇),本文章內(nèi)容詳細(xì),通過(guò)案例可以更好的理解數(shù)組的相關(guān)知識(shí),本模塊分為了三部分,本次為上篇,需要的朋友可以參考下
    2023-01-01

最新評(píng)論