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...

 

[源碼下載]

 


免責聲明!

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



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