IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能

沙海 2021年6月14日09:46:19Java评论37字数 9995阅读33分19秒阅读模式
摘要

IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能 Java后端编程

IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能

Java后端编程 文章源自JAVA秀-https://www.javaxiu.com/32201.html

来源:blog.csdn.net/caoxiaohong1005文章源自JAVA秀-https://www.javaxiu.com/32201.html

文章源自JAVA秀-https://www.javaxiu.com/32201.html

Just Code It文章源自JAVA秀-https://www.javaxiu.com/32201.html

一、搭建SpringBoot项目

1.1、file ——> new ——> project——> Spring Initializr——> next——> next——> next——> finish文章源自JAVA秀-https://www.javaxiu.com/32201.html

注意选择包依赖关系Web下的Spring Web。Template Engines下的Thymeleaf。SQL下的JDBC API、Spring Data JDBC、MySQL Driver。IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能文章源自JAVA秀-https://www.javaxiu.com/32201.html

二、springboot整合mybatis.mysql

2.1、整体结构

IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能文章源自JAVA秀-https://www.javaxiu.com/32201.html

2.2、设置所需要的依赖

即pom.xml文件文章源自JAVA秀-https://www.javaxiu.com/32201.html

    <?xml version="1.0" encoding="UTF-8"?><projectxmlns="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.2</version><relativePath/><!-- lookup parent from repository --></parent><groupId>springboot-web04</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</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-jdbc</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><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-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>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.3</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version><scope>provided</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.1</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>
    文章源自JAVA秀-https://www.javaxiu.com/32201.html

    2.3、设置application.yml文件与pplication.properties文件

    在resources目录下新建yml文件,用于存放数据库连接需要的一些数据文章源自JAVA秀-https://www.javaxiu.com/32201.html

      spring:datasource: driver-class-name: com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8&useSSL=trueusername: root #数据库    password: sm1208 #密码
      文章源自JAVA秀-https://www.javaxiu.com/32201.html

      在application.properties文件中加入文章源自JAVA秀-https://www.javaxiu.com/32201.html

        #端口号server.port=8080#druid数据库连接池type=com.alibaba.druid.pool.DruidDataSource#清除缓存spring.thymeleaf.cache=false#配置mappermybatis.mapper-locations=classpath:mapper/*.xml
        文章源自JAVA秀-https://www.javaxiu.com/32201.html

        2.4、在pojo下的新建类UserLogin

        文章源自JAVA秀-https://www.javaxiu.com/32201.html

          package springbootweb04.demo.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublicclass UserLogin {privateString username;privateString password;publicString getUsername() {return username; }}
          文章源自JAVA秀-https://www.javaxiu.com/32201.html

          2.5、新建数据库,名为mybatis,创建用户表,名为userLogin,创建username、password字段

          2.5.1、数据库名可以随意,不过要与application.yml文件中的一致

          IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能文章源自JAVA秀-https://www.javaxiu.com/32201.html

          2.5.2、IDEA中连接数据库

          Database——> +——> Data Source——> Mysql文章源自JAVA秀-https://www.javaxiu.com/32201.html

          IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能文章源自JAVA秀-https://www.javaxiu.com/32201.html

          2.6、mapper层

          新建UserLoginMapper接口文章源自JAVA秀-https://www.javaxiu.com/32201.html

            package springbootweb04.demo.mapper;import org.apache.ibatis.annotations.Mapper;import org.springframework.stereotype.Repository;import springbootweb04.demo.pojo.UserLogin;import java.util.List;@Mapper@RepositorypublicinterfaceUserLoginMapper{//查询public List<UserLogin> queryAll();//添加数据publicintadd(UserLogin userLogin);//根据用户名查询数据public UserLogin queryByName(String username);}
            文章源自JAVA秀-https://www.javaxiu.com/32201.html

            2.7、resources目录下的mapper目录

            在resources目录下新建mapper目录,并在这个目录下新建UserLoginMapper.xml文件文章源自JAVA秀-https://www.javaxiu.com/32201.html

              <?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"><mappernamespace="springbootweb04.demo.mapper.UserLoginMapper"><selectid="queryAll"resultType="springbootweb04.demo.pojo.UserLogin"> select * from userLogin</select><insertid="add"parameterType="springbootweb04.demo.pojo.UserLogin"> insert into userLogin values (#{username},#{password})</insert><selectid="queryByName"resultType="springbootweb04.demo.pojo.UserLogin"> select * from userLogin where username = #{username}</select></mapper>
              文章源自JAVA秀-https://www.javaxiu.com/32201.html

              2.8、测试

              在test.Java.springbootweb04.demo类中,测试是否能联通数据库,没有报错说明成功。文章源自JAVA秀-https://www.javaxiu.com/32201.html

                package springbootweb04.demo;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import springbootweb04.demo.mapper.UserLoginMapper;import springbootweb04.demo.pojo.UserLogin;import org.springframework.beans.factory.annotation.Autowired;import javax.sql.DataSource;import java.sql.Connection;import java.sql.SQLException;import java.util.List;@SpringBootTestclassDemoApplicationTests{ @AutowiredDataSource dataSource; @Test void contextLoads() throwsSQLException {System.out.println(dataSource.getClass());Connection connection = dataSource.getConnection();System.out.println(connection);//template模板,拿来即用 connection.close(); } @AutowiredUserLoginMapper userLoginMapper; @Testpublic void toTest(){List<UserLogin> userLogins = userLoginMapper.queryAll(); userLogins.forEach(e-> System.out.println(e)); }}
                文章源自JAVA秀-https://www.javaxiu.com/32201.html

                2.9、services层

                在services下新建接口UserLoginServicesI和类UserLoginServicesImpl文章源自JAVA秀-https://www.javaxiu.com/32201.html

                UserLoginServicesI接口:文章源自JAVA秀-https://www.javaxiu.com/32201.html

                  package springbootweb04.demo.services;import springbootweb04.demo.pojo.UserLogin;import java.util.List;publicinterfaceUserLoginServicesI{//查询public List<UserLogin> queryAll();//添加数据publicintadd(UserLogin userLogin);//根据用户名查询数据public UserLogin queryByName(String username);}
                  文章源自JAVA秀-https://www.javaxiu.com/32201.html

                  UserLoginServicesImpl类文章源自JAVA秀-https://www.javaxiu.com/32201.html

                    packagespringbootweb04.demo.services;importspringbootweb04.demo.mapper.UserLoginMapper;importspringbootweb04.demo.pojo.UserLogin;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@Servicepublic class UserLoginServicesImpl implements UserLoginServicesI { @Autowired UserLoginMapper userLoginMapper; @Override public List<UserLogin> queryAll() {returnuserLoginMapper.queryAll(); } @Override public int add(UserLogin userLogin) {returnuserLoginMapper.add(userLogin); } @Override public UserLogin queryByName(String username) {returnuserLoginMapper.queryByName(username); }}
                    文章源自JAVA秀-https://www.javaxiu.com/32201.html

                    2.A、controller层

                    编写MyController类文章源自JAVA秀-https://www.javaxiu.com/32201.html

                      package springbootweb04.demo.controller;import springbootweb04.demo.pojo.UserLogin;import springbootweb04.demo.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;@ControllerpublicclassMyController{@Autowired UserLoginServicesImpl userLoginServicesImpl;@RequestMapping("/toLogin")public String toLogin(){return"login"; }@RequestMapping("/LoginSuccess")public String LoginSuccess(Model model, UserLogin userLogin){//先查询看该用户名是否存在 UserLogin userLogin1 = userLoginServicesImpl.queryByName(userLogin.getUsername());if(userLogin1 != null){ // 如果查询的用户不为空 System.out.println(userLogin1.toString());return"success"; }else{//返回到登录页面 model.addAttribute("data","该用户不存在,请先注册");return"login"; } }//登录界面@RequestMapping("/toRegister")public String toRegister(){return"register"; }@RequestMapping("/RegisterSuccess")public String toRegisterSuccess(Model model,UserLogin userLogin){//将账号密码加入到数据库中 int add = userLoginServicesImpl.add(userLogin); System.out.println("数据插入成功!"); model.addAttribute("data","注册成功,请登录!");return"login"; }}
                      文章源自JAVA秀-https://www.javaxiu.com/32201.html

                      三、编写前端页面

                      将以下三个页面放在templates下面文章源自JAVA秀-https://www.javaxiu.com/32201.html

                      login.html:登录页面文章源自JAVA秀-https://www.javaxiu.com/32201.html

                        <!DOCTYPE html><htmllang="en"xmlns:th="http://www.thymeleaf.org"><head><metacharset="UTF-8"><title>Title</title></head><bodystyle="background: aqua"><divalign="center"><br><br><h2>登录界面</h2><br><br><spanth:text="${data}"style="text-color:red;font-size: 10px"></span><formmethod="get"action="/LoginSuccess"> 用户名:<inputtype="text"name="username"placeholder="请输入用户名"required/><br><br> 密码:<inputtype="text"name="password"placeholder="请输入密码"required/><br><br><inputtype="submit"value="登录"></form><br><formmethod="get"action="/toRegister"><inputtype="submit"value="注册"></form></div></body></html>
                        文章源自JAVA秀-https://www.javaxiu.com/32201.html

                        register.html:注册页面文章源自JAVA秀-https://www.javaxiu.com/32201.html

                          <!DOCTYPE html><htmllang="en"xmlns:th="http://www.thymeleaf.org"><head><metacharset="UTF-8"><title>Title</title></head><bodystyle="background: aqua"><divalign="center"><br><br>注册界面<br><br><formmethod="get"action="/RegisterSuccess"> 用户名:<inputtype="text"name="username"placeholder="请输入用户名"required/><br><br> 密码:<inputtype="text"name="password"placeholder="请输入密码"required/><br><br> 确认密码:<inputtype="text"name="password2"placeholder="请输入密码"required/><br><br><inputtype="submit"value="注册"></form></div></body></html>
                          文章源自JAVA秀-https://www.javaxiu.com/32201.html

                          success.html:成功界面文章源自JAVA秀-https://www.javaxiu.com/32201.html

                            <!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body></body></html>
                            文章源自JAVA秀-https://www.javaxiu.com/32201.html

                            本篇项目的搭建给大家准备了视频教程文章源自JAVA秀-https://www.javaxiu.com/32201.html

                            地址:https://zhuanlan.zhihu.com/p/355628487文章源自JAVA秀-https://www.javaxiu.com/32201.html

                            PS:如果觉得我的分享不错,欢迎大家随手点赞、在看。文章源自JAVA秀-https://www.javaxiu.com/32201.html

                            文章源自JAVA秀-https://www.javaxiu.com/32201.html

                            -END-PS:欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,欢迎转发分享给更多人。Java后端编程交流群已成立公众号运营至今,离不开小伙伴们的支持。为了给小伙伴们提供一个互相交流的平台,特地开通了官方交流群。扫描下方二维码备注 进群 或者关注公众号 Java后端编程 后获取进群通道。

                            —————END—————文章源自JAVA秀-https://www.javaxiu.com/32201.html

                            推荐阅读:再见!收费的 XShell,我改用这款国产良心工具!一个帮你轻松搞定第三方登陆的 Java 开源组件拒绝 ! = null ,大神有更好的方法!一个低级错误,生产数据库崩溃了将近半个小时.....重磅推荐:一套开源的网校系统,附源码!IDEA 2021.1 的 Win 和 Mac 快捷键大全!最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:关注公众号并回复 java 领取,更多内容陆续奉上。
                            文章源自JAVA秀-https://www.javaxiu.com/32201.html

                            明天见(。・ω・。)ノ♡文章源自JAVA秀-https://www.javaxiu.com/32201.html

                            继续阅读
                            速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
                            weinxin
                            资源分享QQ群
                            本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
                            沙海
                            匿名

                            发表评论

                            匿名网友 填写信息

                            :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

                            确定