搭建springboot项目
添加依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-configuration-processor
true
mysql
mysql-connector-java
com.alibaba
druid
1.0.5
目录结构
集成Mybatis
添加mybatis-spring-boot-starter依赖
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
添加配置
#mybatis
mybatis.type-aliases-package=com.imooc.miaosha.domain
mybatis.configuration.map-underscore-to-camel-case=truemybatis.configuration.default-fetch-size=100mybatis.configuration.default-statement-timeout=3000mybatis.mapperLocations=classpath:com/imooc/miaosha/dao/*.xml
添加druid配置
#druid
spring.datasource.url=jdbc:mysql://localhost:3306/miaosha?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat
spring.datasource.tomcat.max-active=2spring.datasource.dbcp2.initial-size=1spring.datasource.tomcat.max-wait=60000spring.datasource.tomcat.min-idle=1spring.datasource.tomcat.time-between-eviction-runs-millis=60000spring.datasource.tomcat.min-evictable-idle-time-millis=300000spring.datasource.tomcat.validation-query=select 'x'spring.datasource.tomcat.test-on-borrow=truespring.datasource.dbcp2.test-on-borrow=falsespring.datasource.dbcp2.test-on-return=falsespring.datasource.dbcp2.pool-prepared-statements=truespring.datasource.dbcp2.max-open-prepared-statements=20
添加mysql、druid依赖
mysql
mysql-connector-java
com.alibaba
druid
1.0.5
集成redis
首先安装Redis
修改配置文件redis.conf
将bind 127.0.0.1注释掉
修改daemonize为yes
设置密码
redies连接失败:Could not get a resource from the pool] with root cause
解决方法:关闭linux中的防火墙
命令:systemctl stop firewalld
添加Jedis依赖、添加Fastison依赖
redis.clients
jedis
com.alibaba
fastjson
1.2.38
添加redis配置
#redis
spring.redis.host=47.97.197.62spring.redis.port=6379spring.redis.timeout=3spring.redis.password=123456spring.redis.pool-max-total=10spring.redis.pool.max-idle=10spring.redis.pool.max-wait=3spring.redis.pool.max-active=8
RedisConfig
packagecom.imooc.miaosha.redis;importorg.apache.ibatis.annotations.Param;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="spring.redis")public classRedisConfig {privateString host;private intport;private inttimeout;privateString password;private intpoolMaxTotal;private intpoolMaxIdle;private intpoolMaxWait;publicString getHost() {returnhost;
}public voidsetHost(String host) {this.host =host;
}public intgetPort() {returnport;
}public void setPort(intport) {this.port =port;
}public intgetTimeout() {returntimeout;
}public void setTimeout(inttimeout) {this.timeout =timeout;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}public intgetPoolMaxTotal() {returnpoolMaxTotal;
}public void setPoolMaxTotal(intpoolMaxTotal) {this.poolMaxTotal =poolMaxTotal;
}public intgetPoolMaxIdle() {returnpoolMaxIdle;
}public void setPoolMaxIdle(intpoolMaxIdle) {this.poolMaxIdle =poolMaxIdle;
}public intgetPoolMaxWait() {returnpoolMaxWait;
}public void setPoolMaxWait(intpoolMaxWait) {this.poolMaxWait =poolMaxWait;
}
}
RedisService
packagecom.imooc.miaosha.redis;importcom.alibaba.fastjson.JSON;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Bean;importorg.springframework.stereotype.Service;importredis.clients.jedis.Jedis;importredis.clients.jedis.JedisPool;importredis.clients.jedis.JedisPoolConfig;
@Servicepublic classRedisService {
@Autowired
JedisPool jedisPool;/*** 获取单个对象*/
public T get(KeyPrefix prefix,String key, Classclazz) {
Jedis jedis=null;try{
jedis=jedisPool.getResource();//生成真正的key
String realKey=prefix.getPrefix()+key;
String str=jedis.get(realKey);
T t=stringToBean(str,clazz);returnt;
}finally{
returnToPool(jedis);
}
}/*** 设置对象*/
public booleanset(KeyPrefix prefix, String key, T value) {
Jedis jedis=null;try{
jedis=jedisPool.getResource();
String str=beanToString(value);if(str==null||str.length()<=0){return false;
}//生成真正的key
String realKey=prefix.getPrefix()+key;int seconds=prefix.expireSeconds();if(seconds<=0){
jedis.set(realKey,str);
}else{
jedis.setex(realKey,seconds,str);
};return true;
}finally{
returnToPool(jedis);
}
}/*** 判断key是否存在*/
public booleanexists(KeyPrefix prefix, String key) {
Jedis jedis=null;try{
jedis=jedisPool.getResource();//生成真正的key
String realKey=prefix.getPrefix()+key;returnjedis.exists(realKey);
}finally{
returnToPool(jedis);
}
}/*** 增加值*/
public Long incr(KeyPrefix prefix, String key) {
Jedis jedis=null;try{
jedis=jedisPool.getResource();//生成真正的key
String realKey=prefix.getPrefix()+key;returnjedis.incr(realKey);
}finally{
returnToPool(jedis);
}
}/*** 减少值*/
public Long decr(KeyPrefix prefix, String key) {
Jedis jedis=null;try{
jedis=jedisPool.getResource();//生成真正的key
String realKey=prefix.getPrefix()+key;returnjedis.decr(realKey);
}finally{
returnToPool(jedis);
}
}private String beanToString(T value){if(value==null){return null;
}
Class> clazz=value.getClass();if(clazz==int.class||clazz==Integer.class){return ""+value;
}else if(clazz==String.class){return(String)value;
}else if(clazz==long.class||clazz==Long.class){return ""+value;
}else{returnJSON.toJSONString(value);
}
}
@SuppressWarnings("unchecked")private T stringToBean(String str,Classclazz){if(str==null||str.length()<=0||clazz==null){return null;
}if(clazz==int.class||clazz==Integer.class){return(T)Integer.valueOf(str);
}else if(clazz==String.class){return(T) str;
}else if(clazz==long.class||clazz==Long.class){return(T)Long.valueOf(str);
}else{returnJSON.toJavaObject(JSON.parseObject(str), clazz);
}
}private voidreturnToPool(Jedis jedis){if(jedis!=null){
jedis.close();
}
}
}
运行结果