zoukankan      html  css  js  c++  java
  • SpringBoot的简单登陆开发例子

    1:这个例子用spirngboot整合mybatis,jdbc等技术开发的

    2:步骤

    2.1:新建一个工程

    主要的两个步骤已经贴图了,第二张图是直接在pom.xml文件中加入依赖

    2.2:新建完项目,就创建一个数据表

    1.  
      CREATE TABLE `tuser` (
    2.  
      `id` int(11) NOT NULL AUTO_INCREMENT,
    3.  
      `loginname` varchar(20) DEFAULT NULL,
    4.  
      `password` varchar(20) DEFAULT NULL,
    5.  
      PRIMARY KEY (`id`)
    6.  
      ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

    创建一个名为tuser的数据表

    2.3:把项目所需的包和文件夹补充完整

    2.4整合jdbc,在application.yml中加入jdbc的支持

    spring:
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8
        driver-class-name: com.mysql.jdbc.Driver
        username: root
        password: 123456

    2.5整合springmvc的前端控制器,在application.yml中加入

    spring:
      mvc:
        view:
          prefix: /WEB-INF/jsp/
          suffix: .jsp

    2.6整合mybatis,添加mapper.xml的目录

    mybatis:
      mapper-locations: classpath:mapper/*.xml

    2.7整个yml文件如下

    spring:
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8
        driver-class-name: com.mysql.jdbc.Driver
        username: root
        password: 123456
      mvc:
        view:
          prefix: /WEB-INF/jsp/
          suffix: .jsp
    mybatis:
      mapper-locations: classpath:mapper/*.xml

    2.8实体类的编写,也就是model

    和数据表对应的User类

    1.  
      public class User {
    2.  
       
    3.  
      private int id;
    4.  
      private String loginname;
    5.  
      private String password;
     //省略构造方法和get/set方法
    }

    返回数据给页面的封装类ResultEntity

    1.  
      public class ResultEntity {
    2.  
       
    3.  
      private int code;
    4.  
      private String message;
    5.  
      //省略构造方法和get/set方法
    6.  
       
    7.  
      }

    2.9 控制层的编写,controller

    1.  
      @Controller
    2.  
      @SessionAttributes("user")
    3.  
      public class LoginController {
    4.  
       
    5.  
      @Autowired
    6.  
      private LoginService loginService;
    7.  
       
    8.  
      @RequestMapping("/index")
    9.  
      public String index() {
    10.  
      return "login";
    11.  
      }
    12.  
       
    13.  
      @RequestMapping("/success")
    14.  
      public String success() {
    15.  
      return "success";
    16.  
      }
    17.  
       
    18.  
      @RequestMapping("/login")
    19.  
      @ResponseBody
    20.  
      public ResultEntity login(User user, Model model) {
    21.  
      ResultEntity resultEntity = loginService.login(user);
    22.  
      if (resultEntity.getCode() == 200)
    23.  
      model.addAttribute("user", user);//将user存放到session
    24.  
      return resultEntity;
    25.  
      }
    26.  
       
    27.  
      }

    2.10Service实现层的编写

    1.  
      @Service
    2.  
      class LoginServiceImpl implements LoginService {
    3.  
       
    4.  
      @Autowired
    5.  
      private UserMapper userMapper;
    6.  
       
    7.  
      private ResultEntity resultEntity;
    8.  
       
    9.  
      @Override
    10.  
      public ResultEntity login(User user) {
    11.  
      User login = userMapper.login(user);
    12.  
      if (login != null) {
    13.  
      resultEntity = new ResultEntity(200, "登陆成功");
    14.  
      } else {
    15.  
      resultEntity = new ResultEntity(201, "用户名或密码错误");
    16.  
      }
    17.  
      return resultEntity;
    18.  
      }
    19.  
      }

    接口层就忽略啦  我懒。。就一个 ResultEntity login(User user);

    2.11 mapper.xml

    1.  
      <?xml version="1.0" encoding="UTF-8"?>
    2.  
      <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3.  
      <mapper namespace="net.stxy.boot.dome.mapper.UserMapper">
    4.  
       
    5.  
      <select id="login" parameterType="net.stxy.boot.dome.model.User" resultType="net.stxy.boot.dome.model.User">
    6.  
      select * from tuser where loginname = #{loginname} and password = #{password}
    7.  
      </select>
    8.  
       
    9.  
      </mapper>

    mapper接口也不写了。。。QAQ..

    2.12 到页面了,采用jsp开发,所以要在pom.xml中添加对jsp的支持

    1.  
      <!--springboot tomcat jsp 支持开启-->
    2.  
      <dependency>
    3.  
      <groupId>org.apache.tomcat.embed</groupId>
    4.  
      <artifactId>tomcat-embed-jasper</artifactId>
    5.  
      </dependency>
    6.  
      <dependency>
    7.  
      <groupId>javax.servlet</groupId>
    8.  
      <artifactId>jstl</artifactId>
    9.  
      <scope>compile</scope>
    10.  
      </dependency>

    因为在yml中指定了前端控制器,所以直接在WEB-INF的jsp下读取jsp页面,静态资源文件就可以在路径前面添加,例如jquery文件放在static/js/jquery.min.js,读取直接读js/jquery.min.js即可,因为默认静态资源路径为/static/

    2.13 登陆页面用ajax提交表单,判断返回的code是否==200,等于200则跳转登陆成功页面,否则,提示失败.

    ajax代码:

    1.  
      //提交表单
    2.  
      $.ajax({
    3.  
      url: "/login",
    4.  
      type: "post",
    5.  
      dataType: "json",
    6.  
      data: {
    7.  
      loginname: account,
    8.  
      password: password
    9.  
      },
    10.  
      success: function (result) {
    11.  
      console.log(result);
    12.  
      if (result.code == 200) {
    13.  
      window.location.href = "success";
    14.  
      } else {
    15.  
      alert(result.message);
    16.  
      }
    17.  
      }
    18.  
      })

    3.效果

    3.1地址栏输入 localhost:8080/index,出现登陆页面

    3.2登陆失败

    3.3登陆成功

    这是一个非常简单的springboot登陆例子,新手一枚,勿喷i....QAQ...

    [源码下载]

  • 相关阅读:
    Linux进阶之Linux中的标准输入输出
    PermCheck
    FrogRiverOne
    PermMissingElem
    FrogJmp
    TapeEquilibrium
    恒生电子长沙2016实习生笔试题
    接口和抽象类的异同点?
    C#实现二叉树
    C#实现栈和队列
  • 原文地址:https://www.cnblogs.com/yelanggu/p/10314960.html
Copyright © 2011-2022 走看看