Springboot實現登錄功能


SpringBoot簡介

  Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。

SpringBoot特性

  1. SpringBoot並不是對Spring功能上的增強,而是提供了一種快速創建獨立的Spring應用程序的框架

  2. 嵌入的Tomcat,無需部署WAR文件

  3. 簡化Maven配置

  4. 自動配置Spring

  5. 絕對沒有代碼生成和對XML沒有要求配置

  6.備受關注,是下一代框架,已經是不爭的事實,不需要學習springmvc

  7.微服務的入門級微框架,springboot是springcloud的基礎

結構

  

導入依賴

<?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.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wn</groupId>
    <artifactId>springbootproject02</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootproject02</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</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <!-- 核心依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 可以實現熱部署,在IDEA上實現熱部署還需一些額外的配置,請查閱資料 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>runtime</scope>
        </dependency>

        <!-- JDBC for mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- mybatis -->
        <!--mybatis-->
        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!--fastJson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!--druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!--thymeleaf  新的模板引擎,比jsp要出色-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- 分頁插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <!--xml配置,此是為了將來整合Hibernate或者mybatis
            默認沒有需要配置
        -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

    </build>

</project>

Users實體類

package com.wn.springbootproject02.entity;

public class Users {
    private Integer uid;private String userName;

    private String password;

    private String realName;

    public Users(Integer uid, String username, String password, String realname) {
        this.uid = uid;
        this.userName = username;
        this.password = password;
        this.realName = realname;
    }

    public Users() {
        super();
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }
}

dao層接口

@Repository("iUserDao")
public interface IUserDao{

    //登錄
    public Users login(@Param("username") String username, @Param("userpassword") String userpassword);

}

dao層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指定Dao接口的完整類名
mybatis會依據這個接口動態創建一個實現類去實現這個接口,
而這個實現類是一個Mapper對象-->

<mapper namespace="com.wn.springbootproject02.dao.IUserDao">
    <resultMap id="MapList" type="com.wn.springbootproject02.entity.Sale">
        <id property="sid" column="sid"></id>
        <result property="price" column="price"></result>
        <result property="quantity" column="quantity"></result>
        <result property="totalPrice" column="totalPrice"></result>
        <result property="saleDate" column="saleDate"></result>
        <result property="userId" column="userId"></result>
        <result property="productId" column="productId"></result>
        <association property="users" javaType="com.wn.springbootproject02.entity.Users">
            <id property="uid" column="uid"></id>
            <result property="userName" column="userName"></result>
            <result property="password" column="password"></result>
            <result property="realName" column="realName"></result>
        </association>
        <association property="product" javaType="com.wn.springbootproject02.entity.Product">
            <id property="pid" column="pid"></id>
            <result property="productName" column="productName"></result>
            <result property="quantity" column="quantity"></result>
        </association>
    </resultMap>

    <!--登錄-->
    <select id="login" resultType="com.wn.springbootproject02.entity.Users">
        SELECT * FROM users WHERE username=#{username} AND PASSWORD=#{userpassword}
    </select>
 
</mapper>

UserService接口層

public interface IUserService {

    //登陸
    public Users login(String username, String userpassword);

}

UserService接口實現層

@Service("iUserService")
public class IUserServiceImpl implements IUserService {

    //植入dao層對象
    @Resource(name = "iUserDao")
    private IUserDao dao;

    @Override
    public Users login(String username, String userpassword) {
        return dao.login(username,userpassword);
    }
}

UserController層

@Controller
@RequestMapping("/user")
public class IUserController {

    //植入對象
    @Resource(name = "iUserService")
    private IUserService service;

    /*返回頁面*/
    @RequestMapping("/getlogin")
    public String getlogin(){
        return "login";
    }

    /*登錄*/
    @RequestMapping("/login")
    public ModelAndView login(Users user, ModelAndView mv, HttpServletRequest request, Model model){
        Users login = service.login(user.getUserName(),user.getPassword());
        System.out.println(login);
        if (login!=null){
            request.getSession().setAttribute("login",login);
            System.out.println("成功!!");
            mv.setViewName("index");
        }else{
            System.out.println("失敗!!");
            mv.setViewName("login");
        }
        return mv;
    }

}

application.properties文件

 

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///invoicingsystem
spring.datasource.username=root
spring.datasource.password=123

#Spring Data JPA配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent-output=true
spring.jpa.database=mysql

spring.main.allow-bean-definition-overriding=true

login.html頁面

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>系統登錄 - 小型進銷存系統</title>
    <link rel="stylesheet" href="css/style.css"/>
    <style>
        #parent{
            width:500px;
            height:200px;
            margin-top:20%;
            margin-left:50%;
            transform:translate(-50%,-50%) ;
            background:#009688;
        }
        .password,.subBtn{
            margin-top: 2%;
            margin-left: 3%;
        }
        .loginHeader{
            padding-top: 1%;
        }
    </style>
</head>
<body class="login_bg">
<div id="parent">
    <section class="loginBox">
        <header class="loginHeader" style="text-align:center; ">
            <h1>小型進銷存系統</h1>
        </header>
        <section class="loginCont">
            <form class="loginForm" action="/user/login" method="post" onsubmit="return check()" >
                <div class="inputbox"  style="text-align:center; ">
                    <label for="user">用戶名:</label>
                    <input id="user" type="text" name="userName" placeholder="請輸入用戶名" required="required" />
                </div>
                <div class="password"  style="text-align:center; " >
                    <label for="mima">密碼:</label>
                    <input id="mima" type="password" name="password" placeholder="請輸入密碼" required="required" />
                </div>
                <div class="subBtn"  style="text-align:center; ">
                    <input type="submit" value="登錄" />
                    <input type="reset" value="重置"/>
                </div>
            </form>
        </section>
    </section>
</div>
</body>
</html>

index.html頁面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en" >
    <meta charset="UTF-8">
    <title>小型進銷存系統</title>
<!--    <link rel="stylesheet" th:href="@{/css/public.css}">
    <link rel="stylesheet" th:href="@{/css/style.css}">-->
    <style>
        #parent{
            margin-top:13%;
            margin-left:45%;
            transform:translate(-50%,-50%) ;
        }
    </style>
</head>
<body>
<div id="parent">
    <!--頭部-->
    <header class="publicHeader">
        <div class="publicHeaderR">
            <p><span></span><span style="color: red" th:text="${session.login.getRealName()}"></span> , 歡迎你!  <a href="/user/remover">退出登錄</a></p>
        </div>
    </header>
    <!--主體內容-->
    <section class="publicMian">
        <div class="left">
            <nav>
                <ul class="list">
                    <li ><a href="/user/prodectAdd">銷售</a></li>
                    <li><a href="/user/saleList">銷售信息查詢</a></li>
                    <li><a href="/user/view">查看庫存</a></li>
                </ul>
            </nav>
        </div>
        <div class="right" style="border: 3px solid blue;width: 400px;height: 200px;margin-top: -100px;margin-left: 200px"  >
            <img class="wColck" src="img/clock.jpg" alt=""/>
            <div class="wFont" style="text-align:center;margin-top:10% ">
                <p>歡迎使用小型進銷存系統系統!</p>
            </div>
        </div>
    </section>
</div>
</body>
</html>

實現

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM