您好,登錄后才能下訂單哦!
怎么在SpringBoot利用Mybatis實現一個登錄注冊頁面?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
1.環境
jdk8 : “1.8.0_281”
Maven: 3.6.3
Idea:2020.2
Mysql:5.6.40
Navicat:10.1.7
選擇依賴如下:
Web下的Spring Web。
Template Engines下的Thymeleaf。
SQL下的JDBC API、Spring Data JDBC、MySQL Driver。
項目的結構:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.haungkd</groupId> <artifactId>weblogindemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>weblogindemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</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-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <!--druid連接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.xmlunit</groupId> <artifactId>xmlunit-core</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
yml文件配置:
其中url中的數據庫記得對應,我的庫為login
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/login?serverTimezone=GMT%2B8&useSSL=true username: root #數據庫 password: 123456 #密碼
properties文件配置:
#端口號 server.port=8088 #druid數據庫連接池 type=com.alibaba.druid.pool.DruidDataSource #清除緩存 spring.thymeleaf.cache=false #配置mapper mybatis.mapper-locations=classpath:mapper/*.xml
package com.huangkd.weblogindemo.pojo; import javax.persistence.Table; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data // get/set 方法 @AllArgsConstructor//有參構造器 @NoArgsConstructor//無參構造器 @Table(name = "userlogin") public class UserLogin { private String username; private String password; public String getUsername() { return username; } }
我創建login庫,創建userlogin表,字段為username和password
在IDEA右邊選擇:
Database——> ——> Data Source——> Mysql
如果測試不通:出現如下:
Server returns invalid timezone. Need to set ‘serverTimezone' property.
是數據庫時區問題
解決方法:
打開開最右側 Advanced,找到 serverTimezone,在右側value處填寫 GMT,保存即可
在mapper包下建UserLoginMapper接口
package com.huangkd.weblogindemo.mapper; import com.huangkd.weblogindemo.pojo.UserLogin; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface UserLoginMapper { //定義增刪改查方法 //查詢所有 public List<UserLogin> queryAll(); //添加數據 public int add(UserLogin userLogin); //根據用戶名查詢 public UserLogin queryByUsername(String username); }
在resources下建UserLoginMapper.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.huangkd.weblogindemo.mapper.UserLoginMapper"> <select id="queryAll" resultType="com.huangkd.weblogindemo.pojo.UserLogin"> select * from userlogin </select> <insert id="add" parameterType="com.huangkd.weblogindemo.pojo.UserLogin"> insert into userlogin values (#{username},#{password}) </insert> <select id="queryByUsername" resultType="com.huangkd.weblogindemo.pojo.UserLogin"> select * from userlogin where username=#{username} </select> </mapper>
在services下新建接口UserLoginServicesI和類UserLoginServicesImpl
UserLoginServicesI接口:
import com.huangkd.weblogindemo.pojo.UserLogin; import java.util.List; public interface UserLoginServicesl { //查詢所有 public List<UserLogin> queryAll(); //添加數據 public int add(UserLogin userLogin); //根據用戶名查詢 public UserLogin queryByUsername(String username); }
UserLoginServicesImpl類:
package com.huangkd.weblogindemo.services; import com.huangkd.weblogindemo.mapper.UserLoginMapper; import com.huangkd.weblogindemo.pojo.UserLogin; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserLoginServicesImpl implements UserLoginServicesl { @Autowired //Service層調用dao層/mapper層 一般在service層中創建一個dao/mapper層對象 //用對象調用對應的增刪改查方法 UserLoginMapper userLoginMapper; @Override public List<UserLogin> queryAll() { return userLoginMapper.queryAll(); } @Override public int add(UserLogin userLogin) { return userLoginMapper.add(userLogin); } @Override public UserLogin queryByUsername(String username) { return userLoginMapper.queryByUsername(username); } }
在controller包下創建MyController類:
package com.huangkd.weblogindemo.controller; import com.huangkd.weblogindemo.pojo.UserLogin; import com.huangkd.weblogindemo.services.UserLoginServicesImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyController { //controller層調用service層 //創建一個service層對象 @Autowired UserLoginServicesImpl userLoginServices; //登錄 @RequestMapping("/toLogin") public String toLogin() { return "login"; } @RequestMapping("/loginSuccess") public String loginSuccess(Model model, UserLogin userLogin) { //查詢用戶名是否存在 UserLogin userLogin1 = userLoginServices.queryByUsername(userLogin.getUsername()); if (userLogin1 != null) { //如果用戶名存在 if (userLogin1.getPassword().equals(userLogin.getPassword())) { System.out.println(userLogin1.toString()); return "success"; } else { model.addAttribute("data", "密碼不正確"); return "login"; } } else { //用戶名不存在,直接返回到登錄頁面 model.addAttribute("data", "該用戶不存在,請先注冊"); return "login"; } } //注冊 @RequestMapping("/toRegister") public String toRegister() { return "register"; } @RequestMapping("/toRegisterSuccess") public String toRegisterSuccess(Model model,UserLogin userLogin) { //添加一條記錄到數據庫中 int add = userLoginServices.add(userLogin); System.out.println("插入數據成功"); model.addAttribute("data","注冊成功,請登錄"); return "login"; } }
login.html:登錄頁面
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body background="back.jpg"> <div align="center"> <br><br><h3>登錄界面</h3><br><br> <span th:text="${data}" ></span> <form method="get" action="/loginSuccess"> 用戶名:<input type="text" name="username" placeholder="請輸入用戶名" required/><br><br> 密碼:<input type="text" name="password" placeholder="請輸入密碼" required/><br><br> <input type="submit" value="登錄"> </form> <br> <form method="get" action="/toRegister"> <input type="submit" value="注冊"> </form> </div> </body> </html>
regsister.html:注冊頁面
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>register</title> </head> <body background="back.jpg" > <div align="center"> <br><br><h2>注冊界面</h2><br><br> <form method="get" action="/toRegisterSuccess"> 用戶名:<input type="text" name="username" placeholder="請輸入用戶名" required/><br><br> 密碼:<input type="text" name="password" placeholder="請輸入密碼" required/><br><br> 確認密碼:<input type="text" name="password2" placeholder="請輸入密碼" required/><br><br> <input type="submit" value="注冊"> </form> </div> </body> </html>
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。