Springboot+MyBatist實現(xiàn)前后臺交互登陸功能方式
整體實現(xiàn)的流程
如下:
1 用戶輸入用戶名密碼
2 獲取用戶輸入的用戶名以及密碼,傳遞到后臺數(shù)據(jù)庫,進行信息查詢,如果用戶的用戶名和密碼在數(shù)據(jù)庫中存在,則登陸成功,跳轉(zhuǎn)至登陸成功的界面。反之登陸失敗,返回登陸界面,重新登陸
整個系統(tǒng)中功能實現(xiàn)的流程
如下:
前臺發(fā)送請求即要實現(xiàn)哪種功能,然后service層傳遞到mapper層,進行數(shù)據(jù)庫的交互,然后將數(shù)據(jù)庫查詢的結(jié)果進行一層一層的返回,最后在前臺頁面展示返回的結(jié)果
整個系統(tǒng)的項目結(jié)構(gòu)圖
如下:
第一步
在pom.xml文件中,添加相關(guān)依賴,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>First</groupId> <artifactId>First</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
添加相關(guān)依賴后,為放置啟動類會報錯,進行以下操作:
右擊pom.xml文件pom.xml->Maven會出現(xiàn)以下頁面:
里面有Reimport和Download Sources and Documention選項,兩個都點擊下。
第二步
修改application.yml配置文件,進行數(shù)據(jù)庫連接
配置文件如下:
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/sqltest?useUnicode=true&characterEncoding=UTF-8 username: root password: root thymeleaf:
注意:sqltest是數(shù)據(jù)庫名字,sqltest后面帶問號的一串代碼,不要省去,該代碼是防止在前臺顯示數(shù)據(jù)時出現(xiàn)亂碼
第三步
書寫啟動類,在整個系統(tǒng)中只需要啟動啟動類,整個系統(tǒng)便能夠跑起來,啟動類代碼如下:
package test; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("test.mapper") @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }
@MapperScan進行掃描,掃描的位置就是mapper包的路徑,因為該包下面存放的都是與數(shù)據(jù)庫交互的操作。
如果啟動類中的代碼出現(xiàn)紅色情況,請回到第一步進行Reimport和Download Sources and Documention,該問題便會消失。
第四步
編寫實體類,代碼如下:
在entity包里面建立實體類Stu,Stu類中的屬性和數(shù)據(jù)庫中stu表的屬性一樣。
(定義基本的幾個屬性后,可以通過鍵盤上的“alt+insert”鍵,來進行自動添加該類自帶的get和set方法,以及toString方法)
package test.entity; public class Stu { public String sno; public String sname; public String password; public String tno; public String tname; public String tgrade; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTno() { return tno; } public void setTno(String tno) { this.tno = tno; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public String getTgrade() { return tgrade; } public void setTgrade(String tgrade) { this.tgrade = tgrade; } @Override public String toString() { return "Stu{" + "sno='" + sno + '\'' + ", sname='" + sname + '\'' + ", password='" + password + '\'' + ", tno='" + tno + '\'' + ", tname='" + tname + '\'' + ", tgrade='" + tgrade + '\'' + '}'; } }
書寫mapper類,實現(xiàn)數(shù)據(jù)庫交互,操作數(shù)據(jù)庫(增刪改查)
代碼如下:
package test.mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Component; @Component public interface Common { @Select("select sname from stu where sno=#{sno} and password=#{password}") public String login(@Param("sno") String sno,@Param("password") String password); }
其中,#{sno}是用戶輸入的用戶名,通過@Param參數(shù)的的方式傳遞。
同過用戶在前臺輸入的用戶名和密碼與數(shù)據(jù)庫比較,返回查詢到的sname屬性。
引用@Select注解進行查詢。
如果要進行更新操作,則需要使用@Update注解,括號里面是相關(guān)的sql語句,通過調(diào)用注解下面的方法,便可以實現(xiàn)該sql語句的功能。
第五步
書寫service,業(yè)務(wù)層,控制業(yè)務(wù),主要負責(zé)業(yè)務(wù)邏輯模塊的邏輯應(yīng)用設(shè)計
package test.service; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import test.mapper.Common; @Service public class CommonService { @Autowired public Common commonmapper; public String login(String sno, String password){ return commonmapper.login(sno, password); } }
第六步
controller控制層,負責(zé)具體的業(yè)務(wù)模塊流程的控制,即頁面訪問控制,調(diào)用service層里面的接口控制具體的業(yè)務(wù)流程
代碼如下:
package test.controller; 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; import test.service.CommonService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; /** * Created by MWL on 2017/11/25. */ @Controller public class CommonController { @Autowired public CommonService commonservice; @RequestMapping(value = "/", method = {RequestMethod.POST, RequestMethod.GET}) public String login() { return "/login/login"; } @RequestMapping(value = "/loginPage", method = {RequestMethod.POST, RequestMethod.GET}) public String login(HttpServletRequest request, HttpSession session) { String tno = request.getParameter("tno"); String password = request.getParameter("password"); System.out.println("你輸入的用戶名為:" + tno); System.out.println("你輸入的密碼為:" + password); String tname = commonservice.login(tno, password); session.setAttribute("tname", tname); if (tname == null) { return "redirect:/"; } else { return "redirect:/index"; } } @RequestMapping(value = "/index", method = {RequestMethod.POST, RequestMethod.GET}) public String loginindex() { return "/login/test"; } }
代碼解釋:
1 @RequestMapping(value = “/”)
即在地址欄輸入localhost:8080/ 的時候會跳轉(zhuǎn)到login文件夾下面的login.html頁面
2 @RequestMapping(value = “/loginPage”, method = {RequestMethod.POST, RequestMethod.GET}) HttpServletRequest request
該value的值loginPage要與login.html頁面中form中的action值一樣,是通過form表單提交數(shù)據(jù),即點擊登錄按鈕時,提交數(shù)據(jù),發(fā)送請求
String tno = request.getParameter(“tno”);改代碼是獲取前臺頁面中用戶輸入的用戶名的值,getParameter后面的tno要與form表單中tno中的name值相同。
即獲取的值為用戶名后面輸入框中的值。假設(shè)name的是a,getParameter(“a”),獲取的就是該文本框的值。(不會擅長表達,不過意思相信都能夠理解)
3 “redirect:/”
進行重定向,redirect后面跟的是什么,就返回到哪一個requestmapping的value值,也就是說"redirect:/"重新定向到了
@RequestMapping(value = “/”, method = {RequestMethod.POST, RequestMethod.GET})這串代碼的請求。
4 表單中的提交按鈕類型為submit
5 session進行綁定,用于后面登陸成功的時候知道是哪個用戶登陸的,顯示更直觀。
前臺登陸頁面代碼如下:
<!DOCTYPE html> <html> <head> <div> <div>登錄系統(tǒng)</div> <form action="loginPage" method="post"> <table> <tr> <td>用戶名:</td> <td><input type="text" name="tno" ></input></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password" ></input></td> </tr> <tr> <td></td> <td><input type="submit" value="登陸"></input></td> </tr> </table> </form> </div> </head> </html>
登陸后主頁面代碼如下:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <div> <h3 th:text="'歡迎您::' + ${session.tname}"></h3> <p> 你好! </p> <p>如果你看到這個頁面,代表你是登陸成功了</p> </div> </head> </html>
頁面自己寫的丑,不過功能倒是實現(xiàn)了
最后,運行啟動類
在瀏覽器中輸入localhost:8080回車便出現(xiàn)以下頁面:
輸入用戶名以及密碼便可以登陸了
數(shù)據(jù)庫信息如下圖所示:
請根據(jù)數(shù)據(jù)庫中的信息進行登陸,登陸失敗還是此界面。輸入用戶名111密碼123456,顯示登陸成功會出現(xiàn)以下界面:
總結(jié)
寫了這么長的時間,簡單功能實現(xiàn)了,感興趣的小伙伴可以根據(jù)我寫的親手試驗一下。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring-boot原理及spring-boot-starter實例和代碼
spring-boot的starter是一個通過maven完成自包含并通過annotation配置使得可被spring上下文發(fā)現(xiàn)并實例化的一個可插拔的組件或服務(wù)。這篇文章主要介紹了Spring-boot原理及spring-boot-starter實例和代碼 ,需要的朋友可以參考下2019-06-06Spring的UnsatisfiedDependencyException異常的解決
在使用Spring框架開發(fā)應(yīng)用程序時,我們經(jīng)常會遇到各種異常,本文主要介紹了Spring的UnsatisfiedDependencyException異常的解決,感興趣的可以了解一下2023-11-11Spring Boot多數(shù)據(jù)源及其事務(wù)管理配置方法
本篇文章主要介紹了Spring Boot多數(shù)據(jù)源及其事務(wù)管理配置方法,具有一定的參考價值,有興趣的可以了解一下。2017-04-04Java實現(xiàn)樹形結(jié)構(gòu)管理的組合設(shè)計模式
Java組合模式是一種結(jié)構(gòu)型設(shè)計模式,它允許將對象組合成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。組合模式使得用戶可以使用統(tǒng)一的方式處理單個對象和對象組合,從而簡化了系統(tǒng)的設(shè)計和維護2023-04-04springboot整合shiro多驗證登錄功能的實現(xiàn)(賬號密碼登錄和使用手機驗證碼登錄)
這篇文章給大家介紹springboot整合shiro多驗證登錄功能的實現(xiàn)方法,包括賬號密碼登錄和使用手機驗證碼登錄功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2021-07-07