authorization

master
bseayin 2019-09-15 21:34:22 +08:00
parent a76d4d728b
commit 9b704c9eed
13 changed files with 555 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package com.zz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* --run as application mainspringboot
SpringBoot tomcat main tomcat
* @author jiyu
*
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(App.class, args);
}
}

View File

@ -0,0 +1,21 @@
package com.zz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* controller
* 1 @RestController json
* 2 @Controller
* @author jiyu
*
*/
@Controller
public class IndexController {
@RequestMapping("ddd")
public String toIndex(){
return "index.html";
}
}

View File

@ -0,0 +1,24 @@
package com.zz.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("hello")
public String t1(){
return "hello SpringBoot";
}
@RequestMapping("test2")
public Map t2(){
// 多态
//变量类型是父类,构造方法是子类
Map map=new HashMap();
map.put("key1", "你好");
return map;
}
}

View File

@ -0,0 +1,35 @@
package com.zz.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zz.entity.User;
import com.zz.service.UserService;
@RestController
public class UserController {
@RequestMapping("all")
public List<User> getAll(){
UserService us=new UserService();
List<User> userlist=us.selectAll();
return userlist;
}
@RequestMapping("delete")
public Map remove(HttpServletRequest request){
UserService us=new UserService();
String id=request.getParameter("uid");
boolean rs=us.deleteById(id);
Map map=new HashMap();
map.put("result", rs);
return map;
}
}

View File

@ -0,0 +1,41 @@
package com.zz.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zz.entity.User;
import com.zz.service.UserService;
@RestController
//在类的上面,配置一个拦截路径,那么这个类里面所有方法的
//路径前面必须加上这个us2. 避免和其他类的路径重复
@RequestMapping("us2")
public class UserController2 {
//使用spring的ioc 控制反转。让spring容器帮我们创建对象
@Resource
UserService us;
//拦截路径是http://localhost:9080/a/us2/all
@RequestMapping("all")
public List<User> getAll(){
List<User> userlist=us.selectAll();
return userlist;
}
@RequestMapping("delete")
public Map remove(HttpServletRequest request){
String id=request.getParameter("uid");
boolean rs=us.deleteById(id);
Map map=new HashMap();
map.put("result", rs);
return map;
}
}

View File

@ -0,0 +1,43 @@
package com.zz.entity;
public class User {
private String id;
private String name;
private String pwd;
private String sex;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,134 @@
package com.zz.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.zz.entity.User;
import com.zz.util.JDBCComon;
@Service
public class UserService {
JDBCComon jdbc=new JDBCComon();
//登录
public User login(String name,String pwd){
User user=null;
try {
Connection conn=jdbc.getConnection();
Statement st=conn.createStatement();
String sql="select * from user where name='"+name+"' and pwd='"+pwd+"'";
System.out.println("sql--->"+sql);
ResultSet rss=st.executeQuery(sql);
//rss.next()只要 结果集合里至少有一条记录next方法就会返回true
if(rss.next()){
user=new User();
user.setId(rss.getString("id"));
user.setName(rss.getString("name"));
}
//从下往上关
rss.close();
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
//验证用户名是否存在
public User checkName(String name){
User user=null;
try {
Connection conn=jdbc.getConnection();
Statement st=conn.createStatement();
String sql="select * from user where name='"+name+"' ";
System.out.println("sql--->"+sql);
ResultSet rss=st.executeQuery(sql);
//rss.next()只要 结果集合里至少有一条记录next方法就会返回true
if(rss.next()){
user=new User();
user.setId(rss.getString("id"));
user.setName(rss.getString("name"));
}
//从下往上关
rss.close();
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
public List<User> selectAll(){
List<User> ulist=new ArrayList();
try {
Connection conn=jdbc.getConnection();
Statement st=conn.createStatement();
String sql="select * from user ";
System.out.println("sql--->"+sql);
ResultSet rss=st.executeQuery(sql);
//rss.next()只要 结果集合里至少有一条记录next方法就会返回true
while(rss.next()){
User user=new User();
user.setId(rss.getString("id"));
user.setName(rss.getString("name"));
user.setAge(rss.getInt("age"));
ulist.add(user);
}
//从下往上关
rss.close();
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ulist;
}
public boolean deleteById(String id){
Connection conn;
boolean flag=false;
try {
conn = jdbc.getConnection();
String sql="delete from user where id=?";
PreparedStatement st=conn.prepareStatement(sql);
st.setString(1, id);
if(st.executeUpdate()>0){
flag=true;
};
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}

View File

@ -0,0 +1,23 @@
package com.zz.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCComon {
public Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.cj.jdbc.Driver");
//java10是数据库的名字
String url="jdbc:mysql://localhost:3306/java10?useSSL=false&serverTimezone=Asia/Shanghai";
//登录数据库用户名
String username="root";
//登录数据库密码
String pwd="Java20190713*yy";
Connection conn = DriverManager.getConnection(url,username,pwd);
return conn;
}
}

View File

@ -0,0 +1,70 @@
package com.zz.util;
import java.time.LocalDateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TimerDemo1 {
/**
* fixedRate
使
使
@Scheduled(fixedRate = 3000)
public void scheduledTask() {
System.out.println("Task executed at " + LocalDateTime.now());
}
*/
/**https://www.jianshu.com/p/73784dff0b0e
* Cron
96
2018.08.01 22:23* 580 201 0 1
Cron Spring @Scheduled
Cron 6 7 使
6 Cron
Seconds Minutes Hours DayofMonth Month DayofWeek
7 Cron
Seconds Minutes Hours DayofMonth Month DayofWeek Year
使
Seconds0~59 , - * /
Minutes Seconds
Hours0~23 , - * /
DayofMonth0~31 , - * / ? L W C
Month1~12 JAN ~ DEC , - * /
DayofWeek1~7 SUN ~ SAT , - * / ? L C # 1
Year1970~2099 , - * /
*/
/**
* (1) ,
Seconds 使 10,15,25 10 15 25
*/
@Scheduled(cron = "10,15,25 * * * * ?")
public void scheduledTask() {
System.out.println("Task executed at " + LocalDateTime.now());
}
//每个月的30号凌晨执行
@Scheduled(cron = "0 0 0 30 * ? ")
public void scheduledTask2() {
System.out.println("Task executed at " + LocalDateTime.now());
}
}

View File

@ -0,0 +1,16 @@
server.port=9080
server.servlet.context-path=/a
#Redis
#spring.redis.host=127.0.0.1
redis.host=127.0.0.1
## Redis服务器连接端口
redis.port=6379
## 连接超时时间(毫秒)
redis.timeout=3
## Redis服务器连接密码默认为空
#redis.password=135246
## 连接池中的最大连接数
redis.poolMaxTotal=10
## 连接池中的最大空闲连接
redis.poolMaxIdle=10
## 连接池最大阻塞等待时间(使用负值表示没有限制)

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
hello controller
</body>
</html>

View File

@ -0,0 +1,84 @@
$(document).ready(function(){
// 在这里写你的代码...
$.getJSON("all", function(json){
console.log(json);
$("#tbodymainbtn").empty();
for(var i=0;i<json.length;i++){
$("#tbodymainbtn").append(
"<tr id='tridval"+i+"'>"
+"<td>"+ json[i].id
+"</td>"
+"<td>"+ json[i].name
+"</td>"
+"<td>"+ json[i].age
+"</td>"
+"<td><button type='button' name='btn001' class='btn btn-info btn-sm' id='btn1"+i+"'>修改</button>" +"&nbsp&nbsp&nbsp<button type='button' name='btn003' class='btn btn-danger btn-sm' id='btn3"+json[i].id +"'>删除</button>"
+"</td></tr>"
);
$("#tbodymainbtn").append(
"<tr style='display:none' id='tridval2"+i+"'><form>"
+"<td><input type='text' value='"+ json[i].id
+"'/></td>"
+"<td><input type='text' id='name2"+i+"' value='"+ json[i].name
+"'/></td>"
+"<td><input type='text' id='age2"+i+"' value='"+ json[i].age
+"'/></td>"
+"<td><button type='button' name='btn002' class='btn btn-primary btn-sm' id='btn2"+i+"'>保存</button>"
+"</td></form></tr>"
);
}
//jquery 样式查找 “点+样式名字”
$("button[name='btn001']").click(function(){
var id=this.id;
//截取剩余
var numb = id.slice(4);
console.log("****************"+id);
$("#tridval"+numb).hide();
$("#tridval2"+numb).show();
});
$("button[name='btn002']").click(function(){
var id=this.id;
//截取剩余
var numb = id.slice(4);
console.log("****************"+id);
var nval=$("#name2"+numb).val();
var aval=$("#age2"+numb).val();
console.log("********name2********"+nval);
console.log("********age2********"+aval);
$("#tridval"+numb).show();
$("#tridval2"+numb).hide();
});
$("button[name='btn003']").click(function(){
var id=this.id;
//截取剩余
var numb = id.slice(4);
console.log("****************"+id);
console.log("****************"+numb);
$.getJSON("delete", { uid: numb}, function(json){
console.log("******delete**********"+numb,json);
window.location.href="table.html"
});
});
});//$.getJSON("UserServlet", function(json){
});

View File

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>用户管理</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>用户管理</h2>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<tbody id="tbodymainbtn">
</tbody>
</table>
</div>
</div>
<script type="text/javascript" src="js/table.js"></script>
</body>
</html>