SpringBoot登录用户(书籍管理系统)

项目目录结构

 application.properties文件

server.port=8080
type=com.alibaba.druid.pool.DruidDataSource
spring.thymeleaf.cache=false

application.yml文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/booksys?serverTimezone=GMT%2B8&useSSL=true
    username: root
    password: vicki1108
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
    content-type: text/html
    cache: false
  Spring:
    resources:
      static-locations: classpath:/
mybatis:
  mapper-locations: classpath:mapping/*.xml

数据库staff_info表

sql语句

DROP TABLE IF EXISTS `staff_info`;
CREATE TABLE `staff_info`  (
  `sno` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '内部员工编号,由系统产生',
  `password` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `sname` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `deptno` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `deptname` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `roleid` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `createtime` DATETIME NOT NULL,
  `status` VARCHAR(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `lastlogintime` DATETIME NULL DEFAULT NULL,
  `libraryid` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`sno`) USING BTREE
) ENGINE = INNODB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

一、用户实体类(User.java)

按照数据库中的字段对应写入,并生成 Getter And Setter 方法

import java.util.Date;

public class User {
    private String sno;
    private String password;
    private String sname;
    private String deptno;
    private String deptname;
    private String roleid;
    private Date createtime;
    private String status;
    private Date lastlogintime;
    private String libraryid;

    //此处忽略 Getter And Setter 方法

}

二、Mapper层

UserMapper接口

import com.lzw.booksys.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface UserMapper {

    //用户登录
    public User login(@Param("sno") String sno,@Param("password") String password);

}

UserMapping.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.lzw.booksys.mapper.UserMapper">
    <sql id="Base_Column_List" >
        sno,password,sname,deptno,deptname,roleid,createtime,status,lastlogintime,libraryid
    </sql>

    <select id="login" resultType="com.lzw.booksys.pojo.User">
        select
        <include refid="Base_Column_List"/>
        from staff_info
        where sno=#{sno} and password=#{password}
    </select>


</mapper>

三、service层

UserService接口

import com.lzw.booksys.pojo.User;

import java.util.List;
import java.util.Map;

public interface UserService {

    /**
     * 根据账号密码查询用户
     */
    public User login(String sno,String password);
}

UserServiceImpl实现类

import com.lzw.booksys.mapper.UserMapper;
import com.lzw.booksys.pojo.User;
import com.lzw.booksys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public User login(String sno, String password) {
        return userMapper.login(sno,password);
    }

}

四、Controller层

import com.lzw.booksys.pojo.User;
import com.lzw.booksys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

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

    @Autowired
    private UserService userService;

    //跳转到登录页面
    @RequestMapping("/toLogin.do")
    public String toLogin() {
        //请求转发登录页面
        return "login";
    }

    //实现登录功能
    @PostMapping("/login.do")
    public String login(@RequestParam("sno") String sno,
                        @RequestParam("password") String password,
                        Map<String,Object> map, HttpSession session){
        User user = userService.login(sno,password);
        if(user==null){
            //登录失败
            map.put("msg","用户名或密码错误");
            return "login";
        }else {
            //登录成功,session中加入登录用户名,用于在成功的首页中展示
            session.setAttribute("USER_INFO",user);
            return "index";
        }
    }

    //用户退出登录
    @RequestMapping("/logout.do")
    public String logout(HttpSession session){
        //清空sessio
        session.invalidate();
        return "login";
    }
}

五、前端页面 

登录页面 login.html    主要部分代码

<body class="login-bg">
    
    <div class="login layui-anim layui-anim-up">
        <div class="message">中国银行书籍管理系统登录</div>
        <div id="darkbannerwrap"></div>

        <!--此处要写成method="POST",不能写成th:method="POST"-->
        <form action="login.html" th:action="@{/user/login.do}" method="POST" class="layui-form" >
            <input name="sno" placeholder="员工号"  type="text" lay-verify="required" class="layui-input" >
            <hr class="hr15">
            <input name="password" lay-verify="required" placeholder="密码"  type="password" class="layui-input">
            <hr class="hr15">
            <input value="登录" lay-submit lay-filter="login" style="width:100%;" type="submit">
            <hr class="hr20" >
            <div class="alert alert-success" role="alert" id="hah">
                <!--@thymesVar id="msg" type="String"-->
                <p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
            </div>
        </form>
    </div>
</body>

运行结果

登录页面

登录失败提示

登录成功进入主页面

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值