authorization
parent
a76d4d728b
commit
9b704c9eed
|
@ -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 运行正启动类的main方法,就可以启动这个springboot项目。
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
|
||||
有效字符范围
|
||||
每个域可使用的有效字符包括:
|
||||
|
||||
Seconds:0~59 的整数,或 , - * / 四个字符
|
||||
Minutes:同 Seconds 域一致
|
||||
Hours:0~23的整数,或 , - * / 四个字符
|
||||
DayofMonth:0~31的整数,或 , - * / ? L W C 八个字符
|
||||
Month:1~12的整数,或 JAN ~ DEC,或 , - * / 四个字符
|
||||
DayofWeek:1~7的整数,或 SUN ~ SAT,或 , - * / ? L C # 八个字符,注意整数 1 代表星期日,每周第一天从周日开始
|
||||
Year:1970~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());
|
||||
}
|
||||
}
|
|
@ -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
|
||||
## 连接池最大阻塞等待时间(使用负值表示没有限制)
|
|
@ -0,0 +1,10 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Insert title here</title>
|
||||
</head>
|
||||
<body>
|
||||
hello controller
|
||||
</body>
|
||||
</html>
|
|
@ -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>" +"   <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){
|
||||
|
||||
});
|
||||
|
||||
|
|
@ -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>
|
Loading…
Reference in New Issue