├── README.md
├── SpringBootDemo
├── .gitignore
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── xiaour
│ │ └── spring
│ │ └── boot
│ │ ├── Application.java
│ │ ├── config
│ │ └── MyBatisConfig.java
│ │ ├── controller
│ │ └── TestCtrl.java
│ │ ├── entity
│ │ └── UserInfo.java
│ │ ├── exception
│ │ ├── OApiException.java
│ │ └── OApiResultException.java
│ │ ├── filter
│ │ ├── DruidStatFilter.java
│ │ └── MyFilter.java
│ │ ├── mapper
│ │ └── UserInfoMapper.java
│ │ ├── service
│ │ ├── RedisService.java
│ │ └── impl
│ │ │ └── RedisServiceImpl.java
│ │ ├── task
│ │ └── Task.java
│ │ └── utils
│ │ └── JsonUtil.java
│ └── resources
│ ├── application.yml
│ ├── mapper
│ └── UserInfoMapper.xml
│ └── mybatis-config.xml
├── SpringBootDemoV2
├── .gitignore
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── xiaour
│ │ │ ├── Application.java
│ │ │ ├── config
│ │ │ └── DuckPorperties.java
│ │ │ ├── constants
│ │ │ └── Api.java
│ │ │ ├── controller
│ │ │ └── DemoController.java
│ │ │ ├── exception
│ │ │ ├── AesException.java
│ │ │ └── OApiException.java
│ │ │ ├── service
│ │ │ ├── DubboService.java
│ │ │ └── impl
│ │ │ │ └── DubboServiceImpl.java
│ │ │ └── utils
│ │ │ ├── ByteGroup.java
│ │ │ ├── HttpHelper.java
│ │ │ ├── IDGenerate.java
│ │ │ ├── JsonUtil.java
│ │ │ ├── PKCS7Encoder.java
│ │ │ ├── SHA1.java
│ │ │ ├── Test.java
│ │ │ ├── WXBizMsgCrypt.java
│ │ │ ├── WXBizMsgCryptTest.java
│ │ │ ├── XMLParse.java
│ │ │ └── Xml2JsonUtil.java
│ └── resources
│ │ ├── application.yml
│ │ └── static
│ │ └── banner.png
│ └── test
│ └── java
│ └── com
│ └── github
│ └── xiaour
│ └── SpringBootDemoV2ApplicationTests.java
├── SpringBootDemoV3
├── .gitignore
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── xiaour
│ │ │ └── spring
│ │ │ └── boot
│ │ │ ├── SpringBootDemoV3Application.java
│ │ │ ├── controller
│ │ │ └── CatController.java
│ │ │ ├── entity
│ │ │ ├── Cat.java
│ │ │ ├── PageIndex.java
│ │ │ └── PageInfo.java
│ │ │ └── service
│ │ │ └── CatService.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── xiaour
│ └── spring
│ └── boot
│ └── SpringBootDemoV3ApplicationTests.java
├── SpringBootKafkaDemo
├── SpringBootKafkaDemo.iml
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── xiaour
│ │ │ │ └── spring
│ │ │ │ └── boot
│ │ │ │ └── kafka
│ │ │ │ ├── KafkaApplication.java
│ │ │ │ ├── consumer
│ │ │ │ └── Consumer.java
│ │ │ │ └── producer
│ │ │ │ ├── Message.java
│ │ │ │ ├── Producer.java
│ │ │ │ └── SendController.java
│ │ └── resources
│ │ │ └── application.yml
│ └── test
│ │ └── java
│ │ └── com
│ │ └── xiaour
│ │ └── spring
│ │ └── boot
│ │ └── kafka
│ │ └── KafkaApplicationTests.java
└── target
│ └── classes
│ └── com
│ └── xiaour
│ └── spring
│ └── boot
│ └── kafka
│ └── producer
│ └── Message.class
├── SpringBootRocketMqDemo
├── .gitignore
├── .mvn
│ └── wrapper
│ │ ├── maven-wrapper.jar
│ │ └── maven-wrapper.properties
├── SpringbootRocketMQ.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── xiaour
│ │ │ └── spring
│ │ │ └── boot
│ │ │ └── rocketmq
│ │ │ ├── SpringBootRocketmqDemoApplication.java
│ │ │ ├── consumer
│ │ │ └── Consumer.java
│ │ │ ├── controller
│ │ │ └── TestController.java
│ │ │ └── producer
│ │ │ └── Producer.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── xiaour
│ └── spring
│ └── boot
│ └── rocketmq
│ └── SpringBootRocketmqDemoApplicationTests.java
├── SpringWebFluxDemo
├── .gitignore
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── xiaour
│ │ │ └── flux
│ │ │ ├── SpringWebFluxDemoApplication.java
│ │ │ ├── controller
│ │ │ ├── HelloWorlController.java
│ │ │ ├── SSEController.java
│ │ │ └── UserController.java
│ │ │ ├── entity
│ │ │ └── User.java
│ │ │ └── service
│ │ │ └── UserService.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── github
│ └── xiaour
│ └── flux
│ └── SpringWebFluxDemoApplicationTests.java
└── pom.xml
/README.md:
--------------------------------------------------------------------------------
1 | ## 相关文章
2 |
3 | 1.[SpringMVC配置太多?试试SpringBoot](https://xiaour.github.io/2018/05/02/The_SpringMVC_configuration_is_too_much_Try_SpringBoot/)
4 |
5 | 2.[Springboot集成Kafka](https://xiaour.github.io/2018/05/23/Springboot_integrated_Kafka/)
6 |
7 | 3.[Springboot集成RocketMQ](https://xiaour.github.io/2018/08/16/SpringbootRocketMQ/)
8 |
9 | 4.Webflux Spring异步编程,你将发现新大陆(代码已经部分上传,blog完善中...)
10 |
11 | 5.[利用EasyExcel,两行代码开发一个和API结果一致的生成Excel功能](https://github.com/xiaour/EasyExport#readme)
12 |
13 | ## 代码部分
14 |
15 | ⭐️ SpringBootDemo
16 | 本代码集成了SpringBoot+MyBatis+Redis+MySql。
17 | 最新的部分经网友指正已经把冗余的代码去掉了,代码部分和配置文件部分都有相关的注释;
18 | git clone 到本地后就可以运行了,项目中使用到的数据库表如下
19 |
20 | ```sql
21 | -- ----------------------------
22 | -- Table structure for `user_info`
23 | -- ----------------------------
24 | DROP TABLE IF EXISTS `user_info`;
25 | CREATE TABLE `user_info` (
26 | `id` int(8) NOT NULL AUTO_INCREMENT,
27 | `name` varchar(20) NOT NULL,
28 | `age` int(2) DEFAULT NULL,
29 | PRIMARY KEY (`id`)
30 | ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
31 |
32 | -- ----------------------------
33 | -- Records of user_info
34 | -- ----------------------------
35 | INSERT INTO `user_info` VALUES ('1', 'xiaour', '18');
36 | ```
37 |
38 | ⭐️ SpringBootDemoV2
39 | 代码主要和SpringBootDemo的区别是使用了Springboot2.0
40 |
41 | 2018.08.22 添加了Twitter ID生成算法的工具,每秒支持12万ID生成。
42 |
43 | ⭐️ SpringBootKafkaDemo
44 | Springboot2.0继承了Kafka消息中间件
45 |
46 | ⭐️ SpringBootRocketMqDemo
47 | Springboot2.0继承了RocketMQ4.3消息中间件
48 |
49 | ⭐️ SpringWebfluxDemo
50 | Webflux Spring异步编程。
51 |
52 |
53 | ---------------------------------
54 | 有兴趣的朋友可以关注一下最新开源的
55 | [spring.boot.sapi.starter](https://github.com/xiaour/spring.boot.sapi.starter)
56 |
57 |
--------------------------------------------------------------------------------
/SpringBootDemo/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 |
--------------------------------------------------------------------------------
/SpringBootDemo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.github.xiaour
8 | springbootv1
9 | 0.0.1-SNAPSHOT
10 | SpringBootDemo
11 | Demo project for Spring Boot
12 | jar
13 |
14 |
15 |
16 |
17 | org.springframework.boot
18 | spring-boot-starter-parent
19 | 1.5.2.RELEASE
20 |
21 |
22 |
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-thymeleaf
28 |
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-devtools
33 | true
34 |
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-configuration-processor
39 | true
40 |
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-starter-redis
45 | 1.3.8.RELEASE
46 |
47 |
48 |
49 | org.mybatis.spring.boot
50 | mybatis-spring-boot-starter
51 | 1.3.0
52 |
53 |
54 |
55 | com.alibaba
56 | druid
57 | 1.0.19
58 |
59 |
60 |
61 | mysql
62 | mysql-connector-java
63 |
64 |
65 |
66 | com.github.pagehelper
67 | pagehelper
68 | 3.7.5
69 |
70 |
71 |
72 |
73 | javax.inject
74 | javax.inject
75 | 1
76 |
77 |
78 | javax
79 | javaee-api
80 | 7.0
81 | provided
82 |
83 |
84 |
85 | javax.servlet
86 | javax.servlet-api
87 |
88 |
89 |
90 | com.google.code.gson
91 | gson
92 |
93 |
94 |
95 | commons-codec
96 | commons-codec
97 |
98 |
99 |
100 | com.alibaba
101 | fastjson
102 | 1.2.83
103 |
104 |
105 |
106 | commons-io
107 | commons-io
108 | 2.7
109 |
110 |
111 |
112 | org.apache.httpcomponents
113 | httpclient
114 |
115 |
116 |
117 | org.apache.httpcomponents
118 | httpmime
119 |
120 |
121 |
122 |
123 | org.apache.poi
124 | poi
125 | 4.1.1
126 |
127 |
128 | org.apache.poi
129 | poi-ooxml
130 | 3.13
131 |
132 |
133 | org.apache.poi
134 | ooxml-schemas
135 | 1.1
136 |
137 |
138 |
139 |
140 |
141 |
142 | 1.8
143 |
144 |
145 |
146 |
147 |
148 | org.springframework.boot
149 | spring-boot-maven-plugin
150 |
151 |
152 |
153 |
154 |
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/Application.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
5 | import org.springframework.beans.factory.annotation.Value;
6 | import org.springframework.boot.SpringApplication;
7 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 | import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
10 | import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
11 | import org.springframework.boot.web.servlet.ServletComponentScan;
12 | import org.springframework.boot.web.support.SpringBootServletInitializer;
13 |
14 | /**
15 | *
16 | * @ClassName Application
17 | * @author Zhang.Tao
18 | * @Date 2017年4月27日 下午5:30:34
19 | * @version V2.0.0
20 | */
21 |
22 | @SpringBootApplication(exclude = MybatisAutoConfiguration.class)
23 | @ServletComponentScan
24 | @MapperScan("com.xiaour.spring.boot.mapper")
25 | public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {
26 |
27 | @Value("${server.port}")
28 | private int port;//应用的端口
29 | /**
30 | * 启动入口
31 | * @param args
32 | */
33 | public static void main(String ... args){
34 | SpringApplication.run(Application.class, args);
35 | }
36 |
37 | /**
38 | * 自定义端口
39 | */
40 | @Override
41 | public void customize(ConfigurableEmbeddedServletContainer container) {
42 | container.setPort(port);
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/config/MyBatisConfig.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot.config;
2 |
3 | import com.alibaba.druid.pool.DruidDataSource;
4 | import org.apache.ibatis.session.SqlSessionFactory;
5 | import org.mybatis.spring.SqlSessionFactoryBean;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
11 |
12 |
13 | /**
14 | * @Date: 2018-12-26 12:05
15 | * @version: v1.0
16 | * @Description: mybatis stater从某个版本后去掉了自动配置,需要用config配置
17 | */
18 | @Configuration
19 | public class MyBatisConfig {
20 |
21 | @Autowired
22 | private DataSourceProperties dataSourceProperties;
23 |
24 |
25 | @Bean(name = "dataSource")
26 | public DruidDataSource dataSource() {
27 | DruidDataSource dataSource = new DruidDataSource();
28 | dataSource.setUrl(dataSourceProperties.getUrl());
29 |
30 | dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
31 | dataSource.setUsername(dataSourceProperties.getUsername());
32 | dataSource.setPassword(dataSourceProperties.getPassword());
33 |
34 | return dataSource;
35 |
36 | }
37 |
38 | @Bean
39 | public SqlSessionFactory sqlSessionFactory() throws Exception {
40 | SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
41 | sqlSessionFactoryBean.setDataSource(dataSource());
42 | sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
43 | .getResources(("classpath*:mapper/*.xml")));
44 | return sqlSessionFactoryBean.getObject();
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/controller/TestCtrl.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot.controller;
2 |
3 |
4 | import com.xiaour.spring.boot.service.RedisService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
7 | import org.springframework.core.ParameterNameDiscoverer;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RequestMapping;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | import com.xiaour.spring.boot.entity.UserInfo;
13 | import com.xiaour.spring.boot.mapper.UserInfoMapper;
14 | import com.xiaour.spring.boot.utils.JsonUtil;
15 |
16 | import java.util.Arrays;
17 | import java.util.HashMap;
18 | import java.util.List;
19 | import java.util.Map;
20 |
21 |
22 | /**
23 | * Created by xiaour on 2017/4/19.
24 | */
25 | @RestController
26 | @RequestMapping(value="/test")
27 | public class TestCtrl {
28 |
29 | @Autowired
30 | private RedisService redisService;
31 |
32 | @Autowired
33 | private UserInfoMapper userInfoMapper;
34 |
35 | @RequestMapping(value="/index")
36 | public String index(){
37 | return "hello world";
38 | }
39 |
40 | /**
41 | * 向redis存储值
42 | * @param key
43 | * @param value
44 | * @return
45 | * @throws Exception
46 | */
47 | @RequestMapping("/set")
48 | public String set(String key, String value) throws Exception{
49 |
50 | redisService.set(key, value);
51 | return "success";
52 | }
53 |
54 | /**
55 | * 获取redis中的值
56 | * @param key
57 | * @return
58 | */
59 | @RequestMapping("/get")
60 | public String get(String key){
61 | try {
62 | return redisService.get(key);
63 | } catch (Exception e) {
64 | e.printStackTrace();
65 | }
66 | return "";
67 | }
68 |
69 | /**
70 | * 获取数据库中的用户
71 | * @param id
72 | * @return
73 | */
74 | @RequestMapping("/getUser/{id}")
75 | public String get(@PathVariable("id")int id){
76 | try {
77 | UserInfo user= userInfoMapper.selectByPrimaryKey(id);
78 | return JsonUtil.getJsonString(user);
79 | } catch (Exception e) {
80 | e.printStackTrace();
81 | }
82 | return "";
83 | }
84 |
85 |
86 | public static void main(String[] args) {
87 | Map keyMap= new HashMap<>();
88 | keyMap.put("id","编号");
89 | keyMap.put("name","名称");
90 |
91 | String [] cnCloumn={"编号","名称"};
92 |
93 | System.out.println(Arrays.asList(convertMap(keyMap, cnCloumn)));
94 |
95 | }
96 |
97 | public static String[] convertMap(Map keyMap,String [] dataList){
98 |
99 | for(int i=0;i m:keyMap.entrySet()){
102 | if(m.getValue().equals(dataList[i])){
103 | dataList[i]=m.getKey();
104 | }
105 | }
106 | }
107 |
108 | return dataList;
109 | }
110 |
111 |
112 | public static String getName(String name,String add){
113 | return null;
114 | }
115 |
116 | public static void testGetClassName() {
117 | // 方法1:通过SecurityManager的保护方法getClassContext()
118 | String clazzName = new SecurityManager() {
119 | public String getClassName() {
120 | return getClassContext()[1].getName();
121 | }
122 | }.getClassName();
123 | System.out.println(clazzName);
124 | // 方法2:通过Throwable的方法getStackTrace()
125 | String clazzName2 = new Throwable().getStackTrace()[1].getClassName();
126 | System.out.println(clazzName2);
127 | // 方法3:通过分析匿名类名称()
128 | String clazzName3 = new Object() {
129 | public String getClassName() {
130 | String clazzName = this.getClass().getName();
131 | return clazzName.substring(0, clazzName.lastIndexOf('$'));
132 | }
133 | }.getClassName();
134 | System.out.println(clazzName3);
135 | //方法4:通过Thread的方法getStackTrace()
136 | String clazzName4 = Thread.currentThread().getStackTrace()[2].getClassName();
137 | System.out.println(clazzName4);
138 | }
139 |
140 |
141 |
142 | }
143 |
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/entity/UserInfo.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot.entity;
2 |
3 | import java.util.Date;
4 |
5 | public class UserInfo {
6 | private Integer id;
7 |
8 | private String name;
9 |
10 | private String age;
11 |
12 | public Integer getId() {
13 | return id;
14 | }
15 |
16 | public void setId(Integer id) {
17 | this.id = id;
18 | }
19 |
20 | public String getName() {
21 | return name;
22 | }
23 |
24 | public void setName(String name) {
25 | this.name = name;
26 | }
27 |
28 | public String getAge() {
29 | return age;
30 | }
31 |
32 | public void setAge(String age) {
33 | this.age = age;
34 | }
35 |
36 | }
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/exception/OApiException.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot.exception;
2 |
3 | public class OApiException extends Exception {
4 |
5 | public OApiException(int errCode, String errMsg) {
6 | super("error code: " + errCode + ", error message: " + errMsg);
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/exception/OApiResultException.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot.exception;
2 |
3 | public class OApiResultException extends OApiException {
4 |
5 | public static final int ERR_RESULT_RESOLUTION = -2;
6 |
7 | public OApiResultException(String field) {
8 | super(ERR_RESULT_RESOLUTION, "Cannot resolve field " + field + " from oapi resonpse");
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/filter/DruidStatFilter.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot.filter;
2 |
3 | import javax.servlet.annotation.WebFilter;
4 | import javax.servlet.annotation.WebInitParam;
5 |
6 | import com.alibaba.druid.support.http.WebStatFilter;
7 |
8 | @WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
9 | initParams={
10 | @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
11 | })
12 | public class DruidStatFilter extends WebStatFilter{
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/filter/MyFilter.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot.filter;
2 |
3 | import java.io.IOException;
4 |
5 | import javax.servlet.Filter;
6 | import javax.servlet.FilterChain;
7 | import javax.servlet.FilterConfig;
8 | import javax.servlet.ServletException;
9 | import javax.servlet.ServletRequest;
10 | import javax.servlet.ServletResponse;
11 | import javax.servlet.annotation.WebFilter;
12 | import javax.servlet.annotation.WebInitParam;
13 | import javax.servlet.annotation.WebServlet;
14 | import javax.servlet.http.HttpServletRequest;
15 | import javax.servlet.http.HttpServletResponse;
16 |
17 | import org.springframework.boot.context.properties.ConfigurationProperties;
18 |
19 | import com.alibaba.druid.support.http.StatViewServlet;
20 |
21 | /**
22 | * Created by xiaour on 2017/4/19.
23 | */
24 | @WebServlet(urlPatterns = "/druid/*",
25 | initParams={
26 | @WebInitParam(name="allow",value=""),// IP白名单 (没有配置或者为空,则允许所有访问)
27 | @WebInitParam(name="deny",value="192.168.0.1"),// IP黑名单 (存在共同时,deny优先于allow)
28 | @WebInitParam(name="loginUsername",value="root"),// 用户名
29 | @WebInitParam(name="loginPassword",value="123456"),// 密码
30 | @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
31 | })
32 | @ConfigurationProperties("cacheServer")
33 | @WebFilter(filterName="myFilter",urlPatterns="/*")
34 | public class MyFilter extends StatViewServlet implements Filter{
35 |
36 |
37 | /**
38 | */
39 | private static final long serialVersionUID = 1L;
40 |
41 | @Override
42 | public void init(FilterConfig filterConfig) throws ServletException {
43 |
44 | }
45 |
46 | @Override
47 | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
48 | HttpServletRequest request = (HttpServletRequest) servletRequest;
49 | System.out.println(request.getRequestURI());
50 | HttpServletResponse response = (HttpServletResponse) servletResponse;
51 | filterChain.doFilter(request, response);
52 | }
53 |
54 | @Override
55 | public void destroy() {
56 |
57 | }
58 |
59 |
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/mapper/UserInfoMapper.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot.mapper;
2 |
3 | import com.xiaour.spring.boot.entity.UserInfo;
4 | import org.apache.ibatis.annotations.Mapper;
5 | import org.springframework.stereotype.Component;
6 |
7 | @Component
8 | @Mapper
9 | public interface UserInfoMapper {
10 |
11 | UserInfo selectByPrimaryKey(Integer id);
12 |
13 | }
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/service/RedisService.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot.service;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * Created by zhangtao on 2017/11/8.
7 | */
8 |
9 | public interface RedisService {
10 |
11 | boolean set(String key, String value) throws Exception;
12 |
13 | String get(String key) throws Exception;
14 |
15 | boolean expire(String key, long expire) throws Exception;
16 |
17 | boolean setList(String key, List list) throws Exception;
18 |
19 | List getList(String key, Class clz) throws Exception;
20 |
21 | long lpush(String key, Object obj) throws Exception;
22 |
23 | long rpush(String key, Object obj) throws Exception;
24 |
25 | void hmset(String key, Object obj) throws Exception;
26 |
27 | T hget(String key, Class clz) throws Exception;
28 |
29 |
30 | void del(String key) throws Exception;
31 |
32 | List hmGetAll(String key, Class clz) throws Exception;
33 |
34 | String lpop(String key) throws Exception;
35 | }
36 |
--------------------------------------------------------------------------------
/SpringBootDemo/src/main/java/com/xiaour/spring/boot/service/impl/RedisServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.xiaour.spring.boot.service.impl;
2 |
3 |
4 | import com.xiaour.spring.boot.service.RedisService;
5 | import com.xiaour.spring.boot.utils.JsonUtil;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.dao.DataAccessException;
8 | import org.springframework.data.redis.connection.RedisConnection;
9 | import org.springframework.data.redis.core.RedisCallback;
10 | import org.springframework.data.redis.core.RedisTemplate;
11 | import org.springframework.data.redis.serializer.RedisSerializer;
12 | import org.springframework.stereotype.Service;
13 | import org.springframework.transaction.annotation.Transactional;
14 | import org.springframework.util.Assert;
15 |
16 | import java.util.*;
17 | import java.util.concurrent.TimeUnit;
18 |
19 | /**
20 | * Created by xiaour.github.com on 2017/11/8.
21 | */
22 | @Service("redisService")
23 | @Transactional(rollbackFor = Exception.class)
24 | public class RedisServiceImpl implements RedisService {
25 |
26 | private static int seconds=3600*24;
27 |
28 | @Autowired
29 | private RedisTemplate redisTemplate;
30 |
31 | @Override
32 | public boolean set(final String key, final String value) throws Exception {
33 | Assert.hasText(key,"Key is not empty.");
34 | boolean result = redisTemplate.execute(new RedisCallback() {
35 | @Override
36 | public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
37 | RedisSerializer serializer = redisTemplate.getStringSerializer();
38 | connection.set(serializer.serialize(key), serializer.serialize(value));
39 | return true;
40 | }
41 | });
42 | return result;
43 | }
44 |
45 | public String get(final String key) throws Exception {
46 | Assert.hasText(key,"Key is not empty.");
47 | String result = redisTemplate.execute(new RedisCallback() {
48 | @Override
49 | public String doInRedis(RedisConnection connection) throws DataAccessException {
50 | RedisSerializer serializer = redisTemplate.getStringSerializer();
51 | byte[] value = connection.get(serializer.serialize(key));
52 | return serializer.deserialize(value);
53 | }
54 | });
55 | return result;
56 | }
57 |
58 | public void del(final String key) throws Exception {
59 | Assert.hasText(key,"Key is not empty.");
60 |
61 | redisTemplate.execute(new RedisCallback() {
62 | @Override
63 | public Long doInRedis(RedisConnection conn) throws DataAccessException {
64 | RedisSerializer serializer = redisTemplate.getStringSerializer();
65 | return conn.del(serializer.serialize(key));
66 | }
67 | });
68 | }
69 |
70 |
71 |
72 | @Override
73 | public boolean expire(final String key, long expire) {
74 | return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
75 | }
76 |
77 | @Override
78 | public boolean setList(String key, List list) throws Exception {
79 | Assert.hasText(key,"Key is not empty.");
80 |
81 | String value = JsonUtil.getJsonString(list);
82 | return set(key,value);
83 | }
84 |
85 | @Override
86 | public List getList(String key,Class clz) throws Exception{
87 |
88 | Assert.hasText(key,"Key is not empty.");
89 |
90 | String json = get(key);
91 | if(json!=null){
92 | List list = JsonUtil.readJson2Array(json,clz);
93 | return list;
94 | }
95 | return null;
96 | }
97 |
98 | @Override
99 | public long lpush(final String key, Object obj)throws Exception {
100 | Assert.hasText(key,"Key is not empty.");
101 |
102 | final String value = JsonUtil.getJsonString(obj);
103 | long result = redisTemplate.execute(new RedisCallback() {
104 | @Override
105 | public Long doInRedis(RedisConnection connection) throws DataAccessException {
106 | RedisSerializer serializer = redisTemplate.getStringSerializer();
107 | long count = connection.lPush(serializer.serialize(key), serializer.serialize(value));
108 | return count;
109 | }
110 | });
111 | return result;
112 | }
113 |
114 | @Override
115 | public long rpush(final String key, Object obj) throws Exception{
116 | Assert.hasText(key,"Key is not empty.");
117 |
118 | final String value = JsonUtil.getJsonString(obj);
119 | long result = redisTemplate.execute(new RedisCallback() {
120 | @Override
121 | public Long doInRedis(RedisConnection connection) throws DataAccessException {
122 | RedisSerializer serializer = redisTemplate.getStringSerializer();
123 | long count = connection.rPush(serializer.serialize(key), serializer.serialize(value));
124 | return count;
125 | }
126 | });
127 | return result;
128 | }
129 |
130 | @Override
131 | public void hmset(String key, Object obj) throws Exception{
132 | Assert.hasText(key,"Key is not empty.");
133 |
134 | Map data=JsonUtil.readJsonByteMap(JsonUtil.getJsonString(obj));
135 | redisTemplate.execute(new RedisCallback() {
136 | @Override
137 | public String doInRedis(RedisConnection connection) throws DataAccessException {
138 | RedisSerializer serializer = redisTemplate.getStringSerializer();
139 | connection.hMSet(serializer.serialize(key),data);
140 | return "";
141 | }
142 | });
143 | }
144 |
145 | @Override
146 | public T hget(String key, Class clz) throws Exception{
147 | Assert.hasText(key,"Key is not empty.");
148 |
149 | return redisTemplate.execute(new RedisCallback() {
150 |
151 | @Override
152 | public T doInRedis(RedisConnection connection) throws DataAccessException {
153 | RedisSerializer serializer = redisTemplate.getStringSerializer();
154 |
155 | Map result;
156 |
157 | Map data=connection.hGetAll(serializer.serialize(key));
158 | result= new HashMap<>();
159 | for (Map.Entry entry: data.entrySet()) {
160 | result.put(serializer.deserialize(entry.getKey()),serializer.deserialize(entry.getValue()));
161 | }
162 |
163 | return JsonUtil.json2Obj(JsonUtil.getJsonString(result),clz);
164 | }
165 | });
166 | }
167 |
168 | @Override
169 | public List hmGetAll(String key,Class clz) throws Exception{
170 | Assert.hasText(key,"Key is not empty.");
171 |
172 | List