Ajax+Springboot+jooq實現登錄功能
Ajax+Springboot+jooq實現登錄功能
簡介
本人是某師範大學剛讀大二的計算機專業小白,下面看效果
Springboot
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成爲領導者。
jooq
jOOQ(Java Object Oriented Querying,即面向Java對象查詢)是一個高效地合併了複雜SQL、類型安全、源碼生成、ActiveRecord、存儲過程以及高級數據類型的Java API的類庫。
ajax代碼
function login() {
//獲取輸入框中的內容
var S_number = $("#S_number").val();
var password = $("#password").val();
//使用$.ajax發送異步請求
$.ajax({
url:"http://localhost:8080/SpringBoot/Login",//請求路徑
type:"POST",
data:{
"S_number": S_number,
"password": password
},
//通過學號登錄,登錄成功顯示姓名
success:function (data) {
var user = JSON.parse(data);
if (user.message == "登錄成功") {
//跳轉到管理系統
alert("歡迎: " + user.username);
document.getElementById("demo").innerHTML = user.message;
}else {
alert(user.message);
document.getElementById("demo").innerHTML = user.message;
}
},//響應成功後的回調函數
error:function (data) {
alert("響應結果出錯"+data)
},//如果請求響應出現錯誤,會執行的回調函數
})
}
java代碼
package com.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.springboot.demo.controller;
import com.alibaba.fastjson.JSON;
import com.springboot.demo.Data.LoginData;
import com.springboot.demo.User.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/SpringBoot")
public class LoginController {
private final String ur = "http://localhost:8080/SpringBoot/index";
private final String url = "http://localhost:8080/SpringBoot/Login";
@ResponseBody
@RequestMapping("/Login")
public String hello(HttpServletRequest request, HttpServletResponse response){
response.setHeader("Access-Control-Allow-Origin", "*");
String S_number=request.getParameter("S_number");
String password=request.getParameter("password");
//jooq返回list,springboot來操作這個list
List<Person> people = new ArrayList<Person>();
people = new LoginData().selectAll();
Person person = new Person();
for (int i = 0;i<people.size();i++){
if (S_number.equals(people.get(i).getNumber())){
if (password.equals(people.get(i).getPassword())){
person.setUsername(people.get(i).getUsername());
person.setMessage("登錄成功");
}else {
person.setMessage("登錄失敗,密碼錯誤");
}
}else {
person.setMessage("登錄失敗,學號不存在");
}
}
String pJson = JSON.toJSONString(person);
System.out.println(pJson);
return pJson;
}
}
jooq代碼
package com.springboot.demo.Data;
import com.springboot.demo.User.Person;
import org.jooq.*;
import org.jooq.impl.DSL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
public class LoginData {
private static String driver="com.mysql.cj.jdbc.Driver";
private static String url="jdbc:mysql://127.0.0.1:3306/testjdbc?serverTimezone=UTC";
private static String username="root";
private static String password="psd_123456";
private static Connection ct=null;
private static DSLContext dslContext=null;
private static Result<Record> result=null;
private static Table<Record> table=null;
static {
try {
Class.forName(driver);
ct= DriverManager.getConnection(url, username, password);
dslContext= DSL.using(ct);
table=DSL.table("Person");
System.out.println("jooq數據庫連接成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static List<Person> selectAll(){
ArrayList<Person> list = null;
// result=dslContext.selectFrom(table).fetch();
result=dslContext.select().from(table).fetch();
if(result!=null) {
list=new ArrayList<Person>();
for(Record record : result) {
Person temp = new Person();
temp.setUsername((String) record.getValue("username"));
temp.setPassword((String) record.getValue("password"));
temp.setNumber((String) record.getValue("number"));
list.add(temp);
}
}
return list;
}
public static Person selectByname(String number){
Person person=null;
result=dslContext.selectFrom(table).where("number = "+number).fetch();
if(result!=null) {
Record record=result.get(0);
person=new Person();
person.setUsername((String) record.getValue("username"));
person.setPassword((String) record.getValue("password"));
person.setNumber((String) record.getValue("number"));
}
return person;
}
}