-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ZhangYinHua
committed
Aug 29, 2018
1 parent
5c68ef7
commit 6b4bb5b
Showing
9 changed files
with
877 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/build/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>springboot-redis</artifactId> | ||
|
||
<parent> | ||
<groupId>com.lance.learn</groupId> | ||
<artifactId>springbootlearning</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
|
||
<name>springboot-redis</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- Spring Boot Redis依赖 --> | ||
<!-- 注意:1.5版本的依赖和2.0的依赖不一样,注意看哦 1.5我记得名字里面应该没有“data”, 2.0必须是“spring-boot-starter-data-redis” 这个才行--> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-redis</artifactId> | ||
<!-- 1.5的版本默认采用的连接池技术是jedis 2.0以上版本默认连接池是lettuce, 在这里采用jedis,所以需要排除lettuce的jar --> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>io.lettuce</groupId> | ||
<artifactId>lettuce-core</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<!-- 添加jedis客户端 --> | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
</dependency> | ||
|
||
<!--spring2.0集成redis所需common-pool2--> | ||
<!-- 必须加上,jedis依赖此 --> | ||
<!-- spring boot 2.0 的操作手册有标注 大家可以去看看 地址是:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/--> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-pool2</artifactId> | ||
<version>2.5.0</version> | ||
</dependency> | ||
|
||
<!-- 将作为Redis对象序列化器 --> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<version>1.2.47</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>1.3.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid-spring-boot-starter</artifactId> | ||
<version>1.1.9</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
14 changes: 14 additions & 0 deletions
14
...gBoot-Redis/src/main/java/com/lance/learn/springbootredis/SpringbootRedisApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.lance.learn.springbootredis; | ||
|
||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@MapperScan("com.lance.learn.springbootredis.mapper") | ||
@SpringBootApplication | ||
public class SpringbootRedisApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringbootRedisApplication.class, args); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...Redis/src/main/java/com/lance/learn/springbootredis/configuration/DruidConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.lance.learn.springbootredis.configuration; | ||
|
||
import com.alibaba.druid.support.http.StatViewServlet; | ||
import com.alibaba.druid.support.http.WebStatFilter; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.boot.web.servlet.ServletRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Druid配置类 | ||
* | ||
* @author lemon | ||
*/ | ||
@Configuration | ||
public class DruidConfiguration { | ||
|
||
/** | ||
* 配置监控服务器 | ||
* | ||
* @return 返回监控注册的servlet对象 | ||
*/ | ||
@Bean | ||
public ServletRegistrationBean statViewServlet() { | ||
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); | ||
// 添加IP白名单 | ||
servletRegistrationBean.addInitParameter("allow", "192.168.25.125,127.0.0.1"); | ||
// 添加IP黑名单,当白名单和黑名单重复时,黑名单优先级更高 | ||
servletRegistrationBean.addInitParameter("deny", "192.168.25.123"); | ||
// 添加控制台管理用户 | ||
servletRegistrationBean.addInitParameter("loginUsername", "druid"); | ||
servletRegistrationBean.addInitParameter("loginPassword", "123456"); | ||
// 是否能够重置数据 | ||
servletRegistrationBean.addInitParameter("resetEnable", "false"); | ||
return servletRegistrationBean; | ||
} | ||
|
||
/** | ||
* 配置服务过滤器 | ||
* | ||
* @return 返回过滤器配置对象 | ||
*/ | ||
@Bean | ||
public FilterRegistrationBean statFilter() { | ||
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); | ||
// 添加过滤规则 | ||
filterRegistrationBean.addUrlPatterns("/*"); | ||
// 忽略过滤格式 | ||
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,"); | ||
return filterRegistrationBean; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...Redis/src/main/java/com/lance/learn/springbootredis/configuration/RedisConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.lance.learn.springbootredis.configuration; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
|
||
/** | ||
* @Auther: Lance(ZYH) | ||
* @Date: 2018/8/29 15:18 | ||
* @Description: redis配置 | ||
*/ | ||
@Configuration | ||
public class RedisConfiguration { | ||
/** | ||
* 定义 StringRedisTemplate ,指定序列化和反序列化的处理类 | ||
* @param factory | ||
* @return | ||
*/ | ||
@Bean | ||
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { | ||
StringRedisTemplate template = new StringRedisTemplate(factory); | ||
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>( | ||
Object.class); | ||
ObjectMapper om = new ObjectMapper(); | ||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | ||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | ||
jackson2JsonRedisSerializer.setObjectMapper(om); | ||
//序列化 值时使用此序列化方法 | ||
template.setValueSerializer(jackson2JsonRedisSerializer); | ||
template.afterPropertiesSet(); | ||
return template; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...t-Redis/src/main/java/com/lance/learn/springbootredis/controller/RedisTestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.lance.learn.springbootredis.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @Auther: Lance(ZYH) | ||
* @Date: 2018/8/29 15:32 | ||
* @Description: 测试redis | ||
*/ | ||
@RestController | ||
@RequestMapping("/redis") | ||
public class RedisTestController { | ||
@Autowired | ||
StringRedisTemplate redisTemplate; | ||
/** | ||
* 测试key value | ||
* @param key | ||
* @param value | ||
* @return | ||
*/ | ||
@RequestMapping(value = "set/{key}/{value}",method = RequestMethod.GET) | ||
public String testSet(@PathVariable String key,@PathVariable String value){ | ||
redisTemplate.opsForValue().set(key, value); | ||
return key+":"+value; | ||
} | ||
|
||
@RequestMapping(value = "get/{key}",method = RequestMethod.GET) | ||
public String testGet(@PathVariable String key){ | ||
String value = redisTemplate.opsForValue().get(key); | ||
return "根据key获取的value是"+value; | ||
} | ||
} |
Oops, something went wrong.