From 3328cf12a9cebb7c693f1b82ae5a1a019e72d4a2 Mon Sep 17 00:00:00 2001 From: timebusker Date: Tue, 6 Jun 2017 20:55:09 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0Scheduled=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-13-MyBatis/README.md | 67 ++++--------------------------- spring-boot-4-Scheduled/README.md | 12 +++++- 2 files changed, 18 insertions(+), 61 deletions(-) diff --git a/spring-boot-13-MyBatis/README.md b/spring-boot-13-MyBatis/README.md index 77bbf6d..41c1832 100644 --- a/spring-boot-13-MyBatis/README.md +++ b/spring-boot-13-MyBatis/README.md @@ -5,73 +5,20 @@ ![image](https://github.com/timebusker/spring-boot/raw/master/static/spring-boot-3-logs/spring-boot-3-logs-Logback/Logback.png?raw=true) + #### SLF4J+Logback配置说明 - * [logback日志分开纪录](http://www.cnblogs.com/DeepLearing/p/5664941.html)
+ * [logback日志分开纪录](http://www.cnblogs.com/DeepLearing/p/5664941.html) * [logback节点配置详解](http://www.cnblogs.com/DeepLearing/p/5663178.html) * [logback 中文手册.pdf](https://github.com/timebusker/spring-boot/raw/master/static/spring-boot-3-logs/spring-boot-3-logs-Logback/logback_cn.pdf?raw=true) + #### 配置多环境不同日志级别 ***logback.xml*配置讲解** ```xml - - - - - - - - - - - - - %date [%thread] %-5level %logger{80} || %msg%n - - - - - - - +``` - - - - - - ${TEST_FILE_PATH} - - - ${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log - - 100 - - - ${PATTERN} - - - - - - +### 相关文章 + +[MyBatis Generator 详解](http://blog.csdn.net/isea533/article/details/42102297) +[logback节点配置详解](http://www.cnblogs.com/DeepLearing/p/5663178.html) +[logback节点配置详解](http://www.cnblogs.com/DeepLearing/p/5663178.html) - - - - ${PRO_FILE_PATH} - - ${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log - 100 - - - ${PATTERN} - - - - - - - -``` - ---- \ No newline at end of file diff --git a/spring-boot-4-Scheduled/README.md b/spring-boot-4-Scheduled/README.md index 786a699..3ccdc88 100644 --- a/spring-boot-4-Scheduled/README.md +++ b/spring-boot-4-Scheduled/README.md @@ -55,6 +55,12 @@ - #### Spring Task 简单使用、动态修改cron表达式、动态添加修改删除定时任务 ++ 关于 spring task的注解使用--@Scheduled详解 +@Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行 +@Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行 +@Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次 +@Scheduled(cron="*/5 * * * * *") :通过cron表达式定义规则 + + 使用spring task不需要引用额外的jar包,spring-core中已经完成封装 ![image](https://github.com/timebusker/spring-boot/raw/master/static/spring-boot-4-Scheduled/spring-core-tast.png?raw=true) @@ -93,4 +99,8 @@ public class SpringTaskSample { ![image](https://github.com/timebusker/spring-boot/raw/master/static/spring-boot-4-Scheduled/spring-core-tast-res.png?raw=true) + Spring的Scheduled Task实现集群思路 - 实现任务互斥,通过声明***全局锁***作为互斥变量,获得锁的变量才有权执行任务。 \ No newline at end of file +实现任务互斥,通过声明 ***全局锁*** 作为互斥变量,获得锁的变量才有权执行任务。 + + + + \ No newline at end of file From 7a722b41ec0725f149a2c6c2d3db5108858c53ee Mon Sep 17 00:00:00 2001 From: timebusker Date: Tue, 6 Jun 2017 20:56:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0Scheduled=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-4-Scheduled/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-boot-4-Scheduled/README.md b/spring-boot-4-Scheduled/README.md index 3ccdc88..7a356b7 100644 --- a/spring-boot-4-Scheduled/README.md +++ b/spring-boot-4-Scheduled/README.md @@ -56,10 +56,10 @@ - #### Spring Task 简单使用、动态修改cron表达式、动态添加修改删除定时任务 + 关于 spring task的注解使用--@Scheduled详解 -@Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行 -@Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行 -@Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次 -@Scheduled(cron="*/5 * * * * *") :通过cron表达式定义规则 + - @Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行 + - @Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行 + - @Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次 + - @Scheduled(cron="*/5 * * * * *") :通过cron表达式定义规则 + 使用spring task不需要引用额外的jar包,spring-core中已经完成封装 ![image](https://github.com/timebusker/spring-boot/raw/master/static/spring-boot-4-Scheduled/spring-core-tast.png?raw=true) @@ -99,7 +99,7 @@ public class SpringTaskSample { ![image](https://github.com/timebusker/spring-boot/raw/master/static/spring-boot-4-Scheduled/spring-core-tast-res.png?raw=true) + Spring的Scheduled Task实现集群思路 -实现任务互斥,通过声明 ***全局锁*** 作为互斥变量,获得锁的变量才有权执行任务。 +实现任务互斥,通过声明 **全局锁** 作为互斥变量,获得锁的变量才有权执行任务。 From ec5f3a08f06184a9409fd9f28deab1fa0e87269a Mon Sep 17 00:00:00 2001 From: timebusker Date: Tue, 6 Jun 2017 22:37:35 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=8C=E6=88=90MyBatis=E5=9F=BA=E4=BA=8E?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-13-MyBatis/README.md | 6 +- spring-boot-13-MyBatis/pom.xml | 25 ++++- .../src/main/java/cn/timebusker/App.java | 3 +- .../cn/timebusker/dao/UserInfoMapper.java | 38 +++++++ .../java/cn/timebusker/entity/UserInfo.java | 101 ++++++++++++++++++ .../main/resources/application-dev.properties | 11 +- .../java/test/dao/UserInfoMapperTest.java | 70 ++++++++++++ 7 files changed, 244 insertions(+), 10 deletions(-) create mode 100644 spring-boot-13-MyBatis/src/main/java/cn/timebusker/dao/UserInfoMapper.java create mode 100644 spring-boot-13-MyBatis/src/main/java/cn/timebusker/entity/UserInfo.java create mode 100644 spring-boot-13-MyBatis/src/test/java/test/dao/UserInfoMapperTest.java diff --git a/spring-boot-13-MyBatis/README.md b/spring-boot-13-MyBatis/README.md index 41c1832..1901959 100644 --- a/spring-boot-13-MyBatis/README.md +++ b/spring-boot-13-MyBatis/README.md @@ -1,7 +1,7 @@ ---- -## [spring-boot-3-logs-Logback 集成Logback日志框架](https://github.com/timebusker/spring-boot/tree/master/spring-boot-3-logs/spring-boot-3-logs-Logback/) +## [集成MyBatis持久层框架](https://github.com/timebusker/spring-boot/tree/master/spring-boot-13-MyBatis/) -### 项目阐述 +### 关于MyBatis ![image](https://github.com/timebusker/spring-boot/raw/master/static/spring-boot-3-logs/spring-boot-3-logs-Logback/Logback.png?raw=true) + #### SLF4J+Logback配置说明 @@ -18,7 +18,5 @@ ### 相关文章 [MyBatis Generator 详解](http://blog.csdn.net/isea533/article/details/42102297) -[logback节点配置详解](http://www.cnblogs.com/DeepLearing/p/5663178.html) -[logback节点配置详解](http://www.cnblogs.com/DeepLearing/p/5663178.html) ---- \ No newline at end of file diff --git a/spring-boot-13-MyBatis/pom.xml b/spring-boot-13-MyBatis/pom.xml index 553d460..42231de 100644 --- a/spring-boot-13-MyBatis/pom.xml +++ b/spring-boot-13-MyBatis/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 cn.timebusker @@ -13,7 +14,7 @@ UTF-8 - + org.springframework.boot @@ -56,8 +57,21 @@ true + + + mysql + mysql-connector-java + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.0 + + + - + org.slf4j slf4j-api @@ -88,5 +102,10 @@ logback-core + + + com.alibaba + fastjson + diff --git a/spring-boot-13-MyBatis/src/main/java/cn/timebusker/App.java b/spring-boot-13-MyBatis/src/main/java/cn/timebusker/App.java index 8b1f6c1..b2e2720 100644 --- a/spring-boot-13-MyBatis/src/main/java/cn/timebusker/App.java +++ b/spring-boot-13-MyBatis/src/main/java/cn/timebusker/App.java @@ -31,6 +31,7 @@ public class App { * @ComponentScan 告诉Spring寻找其他组件,配置,以及业务层类,最前面才能加载到所有的类。 */ + @SuppressWarnings("unused") public static void main(String[] args) { // devtools:是spring boot的一个热部署工具 //设置 spring.devtools.restart.enabled 属性为false,可以关闭该特性. @@ -42,7 +43,7 @@ public static void main(String[] args) { String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { - System.out.println(beanName); + // System.out.println(beanName); } } } diff --git a/spring-boot-13-MyBatis/src/main/java/cn/timebusker/dao/UserInfoMapper.java b/spring-boot-13-MyBatis/src/main/java/cn/timebusker/dao/UserInfoMapper.java new file mode 100644 index 0000000..0222fd0 --- /dev/null +++ b/spring-boot-13-MyBatis/src/main/java/cn/timebusker/dao/UserInfoMapper.java @@ -0,0 +1,38 @@ +package cn.timebusker.dao; + +import java.util.List; + +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; + +import cn.timebusker.entity.UserInfo; + +/** + * 基于注解实现持久化操作 + * + */ +@Mapper +public interface UserInfoMapper { + + @Select("SELECT * FROM user_info WHERE username = #{name}") + List findByName(@Param("name") String name); + + @Select("SELECT * FROM user_info WHERE id = #{id}") + List findById(@Param("id") int id); + + @Insert("INSERT INTO user_info(Id,username,password,usertype,enabled,realname,email,tel) VALUES(#{id}, #{username},#{password}, #{usertype},#{enabled}, #{realname},#{email}, #{tel})") + int insert(UserInfo ui); + + @Select("SELECT * FROM user_info WHERE 1=1 ") + List findAll(); + + @Update("UPDATE user_info SET password=#{password} WHERE username=#{username}") + void update(UserInfo ui); + + @Delete("DELETE FROM user_info WHERE id =#{id}") + void delete(int id); +} \ No newline at end of file diff --git a/spring-boot-13-MyBatis/src/main/java/cn/timebusker/entity/UserInfo.java b/spring-boot-13-MyBatis/src/main/java/cn/timebusker/entity/UserInfo.java new file mode 100644 index 0000000..0883686 --- /dev/null +++ b/spring-boot-13-MyBatis/src/main/java/cn/timebusker/entity/UserInfo.java @@ -0,0 +1,101 @@ +package cn.timebusker.entity; + +import java.io.Serializable; + +/** + * 用户信息 + */ +public class UserInfo implements Serializable { + + private static final long serialVersionUID = -3054513298829319801L; + + private Integer id; + + private String username; + + private String password; + + private String usertype; + + private Integer enabled; + + private String realname; + + private String qq; + + private String email; + + private String tel; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getUsertype() { + return usertype; + } + + public void setUsertype(String usertype) { + this.usertype = usertype; + } + + public Integer getEnabled() { + return enabled; + } + + public void setEnabled(Integer enabled) { + this.enabled = enabled; + } + + public String getRealname() { + return realname; + } + + public void setRealname(String realname) { + this.realname = realname; + } + + public String getQq() { + return qq; + } + + public void setQq(String qq) { + this.qq = qq; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getTel() { + return tel; + } + + public void setTel(String tel) { + this.tel = tel; + } +} diff --git a/spring-boot-13-MyBatis/src/main/resources/application-dev.properties b/spring-boot-13-MyBatis/src/main/resources/application-dev.properties index d4eecd1..e5b70e3 100644 --- a/spring-boot-13-MyBatis/src/main/resources/application-dev.properties +++ b/spring-boot-13-MyBatis/src/main/resources/application-dev.properties @@ -1,5 +1,5 @@ # 项目contextPath,一般在正式发布版本中,我们不配置 -server.context-path=/logback +server.context-path=/mybatis # 错误页,指定发生错误时,跳转的URL。请查看BasicErrorController源码便知 server.error.path=/error # 服务端口 @@ -7,4 +7,11 @@ server.port=80 # session最大超时时间(分钟),默认为30 server.session-timeout=60 # 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置 -# server.address=192.168.16.11 \ No newline at end of file +# server.address=192.168.16.11 + + +# 数据连接配置 +spring.datasource.url=jdbc:mysql://localhost:3306/timebusker-spring +spring.datasource.username=root +spring.datasource.password=root +spring.datasource.driver-class-name=com.mysql.jdbc.Driver \ No newline at end of file diff --git a/spring-boot-13-MyBatis/src/test/java/test/dao/UserInfoMapperTest.java b/spring-boot-13-MyBatis/src/test/java/test/dao/UserInfoMapperTest.java new file mode 100644 index 0000000..2d5fe3e --- /dev/null +++ b/spring-boot-13-MyBatis/src/test/java/test/dao/UserInfoMapperTest.java @@ -0,0 +1,70 @@ +package test.dao; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringRunner; + +import cn.timebusker.App; +import cn.timebusker.dao.UserInfoMapper; +import cn.timebusker.entity.UserInfo; + +import com.alibaba.fastjson.JSON; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT) +public class UserInfoMapperTest { + + @Autowired + UserInfoMapper dao; + + @Test + public void findAll() throws Exception { + List lui = dao.findAll(); + System.out.println("\n\n\n\n\n"); + System.out.println("输出结果:" + JSON.toJSONString(lui.get(0))); + System.out.println("输出结果:" + JSON.toJSONString(lui)); + System.out.println("\n\n\n\n\n"); + } + + @Test + public void findByName() throws Exception { + List lui = dao.findByName("test2"); + System.out.println("\n\n\n\n\n"); + System.out.println("输出结果:" + lui.get(0).getPassword()); + System.out.println("\n\n\n\n\n"); + } + + @Test + public void insert() throws Exception { + UserInfo ui = new UserInfo(); + ui.setId(1); + ui.setUsername("余姣姣"); + System.out.println("\n\n\n\n\n"); + System.out.println("输出结果:" + dao.insert(ui)); + System.out.println("\n\n\n\n\n"); + } + + @Test + public void update() throws Exception { + List lui = dao.findById(2); + lui.get(0).setPassword("wewrewz"); + dao.update(lui.get(0)); + lui = dao.findById(2); + System.out.println("\n\n\n\n\n"); + System.out.println("输出结果:" + JSON.toJSONString(lui)); + System.out.println("\n\n\n\n\n"); + } + + @Test + public void delete() throws Exception { + dao.delete(1); + System.out.println("\n\n\n\n\n"); + System.out.println("输出结果:" + JSON.toJSONString(dao.findById(1))); + System.out.println("\n\n\n\n\n"); + } +} \ No newline at end of file