diff --git a/SpringBootRedis/src/main/java/com/zz/App.java b/SpringBootRedis/src/main/java/com/zz/App.java new file mode 100644 index 0000000..8142f63 --- /dev/null +++ b/SpringBootRedis/src/main/java/com/zz/App.java @@ -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); + } + +} diff --git a/SpringBootRedis/src/main/java/com/zz/controller/IndexController.java b/SpringBootRedis/src/main/java/com/zz/controller/IndexController.java new file mode 100644 index 0000000..041ad41 --- /dev/null +++ b/SpringBootRedis/src/main/java/com/zz/controller/IndexController.java @@ -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"; + } + +} diff --git a/SpringBootRedis/src/main/java/com/zz/controller/TestController.java b/SpringBootRedis/src/main/java/com/zz/controller/TestController.java new file mode 100644 index 0000000..9c7f3bc --- /dev/null +++ b/SpringBootRedis/src/main/java/com/zz/controller/TestController.java @@ -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; + } +} diff --git a/SpringBootRedis/src/main/java/com/zz/controller/UserController.java b/SpringBootRedis/src/main/java/com/zz/controller/UserController.java new file mode 100644 index 0000000..48c242c --- /dev/null +++ b/SpringBootRedis/src/main/java/com/zz/controller/UserController.java @@ -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 getAll(){ + UserService us=new UserService(); + List 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; + } + +} diff --git a/SpringBootRedis/src/main/java/com/zz/controller/UserController2.java b/SpringBootRedis/src/main/java/com/zz/controller/UserController2.java new file mode 100644 index 0000000..8e689f9 --- /dev/null +++ b/SpringBootRedis/src/main/java/com/zz/controller/UserController2.java @@ -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 getAll(){ + List 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; + } + +} diff --git a/SpringBootRedis/src/main/java/com/zz/entity/User.java b/SpringBootRedis/src/main/java/com/zz/entity/User.java new file mode 100644 index 0000000..a18b016 --- /dev/null +++ b/SpringBootRedis/src/main/java/com/zz/entity/User.java @@ -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; + } + + + +} diff --git a/SpringBootRedis/src/main/java/com/zz/service/UserService.java b/SpringBootRedis/src/main/java/com/zz/service/UserService.java new file mode 100644 index 0000000..ee296c4 --- /dev/null +++ b/SpringBootRedis/src/main/java/com/zz/service/UserService.java @@ -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 selectAll(){ + List 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; + + } + +} diff --git a/SpringBootRedis/src/main/java/com/zz/util/JDBCComon.java b/SpringBootRedis/src/main/java/com/zz/util/JDBCComon.java new file mode 100644 index 0000000..ee1c80a --- /dev/null +++ b/SpringBootRedis/src/main/java/com/zz/util/JDBCComon.java @@ -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; + + } + +} diff --git a/SpringBootRedis/src/main/java/com/zz/util/TimerDemo1.java b/SpringBootRedis/src/main/java/com/zz/util/TimerDemo1.java new file mode 100644 index 0000000..c0b85a9 --- /dev/null +++ b/SpringBootRedis/src/main/java/com/zz/util/TimerDemo1.java @@ -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()); + } +} diff --git a/SpringBootRedis/src/main/resources/application.properties b/SpringBootRedis/src/main/resources/application.properties new file mode 100644 index 0000000..99dd32b --- /dev/null +++ b/SpringBootRedis/src/main/resources/application.properties @@ -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 +## 连接池最大阻塞等待时间(使用负值表示没有限制) \ No newline at end of file diff --git a/SpringBootRedis/src/main/resources/static/index.html b/SpringBootRedis/src/main/resources/static/index.html new file mode 100644 index 0000000..62ad5cc --- /dev/null +++ b/SpringBootRedis/src/main/resources/static/index.html @@ -0,0 +1,10 @@ + + + + +Insert title here + + +hello controller + + \ No newline at end of file diff --git a/SpringBootRedis/src/main/resources/static/js/table.js b/SpringBootRedis/src/main/resources/static/js/table.js new file mode 100644 index 0000000..b00c0d5 --- /dev/null +++ b/SpringBootRedis/src/main/resources/static/js/table.js @@ -0,0 +1,84 @@ + $(document).ready(function(){ + // 在这里写你的代码... + $.getJSON("all", function(json){ + console.log(json); + $("#tbodymainbtn").empty(); + for(var i=0;i" + +""+ json[i].id + +"" + +""+ json[i].name + +"" + +""+ json[i].age + +"" + +"" +"   " + +"" + ); + $("#tbodymainbtn").append( + "
" + +"" + +"" + +"" + +"" + +"
" + ); + } + + + //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){ + + }); + + \ No newline at end of file diff --git a/SpringBootRedis/src/main/resources/static/table.html b/SpringBootRedis/src/main/resources/static/table.html new file mode 100644 index 0000000..cf6f0e5 --- /dev/null +++ b/SpringBootRedis/src/main/resources/static/table.html @@ -0,0 +1,33 @@ + + + + 用户管理 + + + + + + + + + +
+

用户管理

+
+ + + + + + + + + + + +
学号姓名年龄操作
+
+
+ + + \ No newline at end of file