主要内容
- Spring Boot环境搭建
- 集成Thymeleaf,Result结果封装
- 集成Mybatis+Druid
- 集成Jedis+Redis安装+通用缓存Key封装
一、Spring Boot环境搭建
创建项目
导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
Thymeleaf配置
Spring Boot参考文档:链接
一般Spring Boot配置项在红框中有说明
具体配置项如下
测试一波
1.Controller
@Controller
@RequestMapping("/demo")
public class SampleController {
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model){
model.addAttribute("name","hsw");
return "hello";
}
}
2.hello.html页面
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'hello:'+${name}" ></p>
</body>
</html>
3.启动项目
二、Result结果封装
创建Result类
建一个result包写一个Result< T >类,为了方便使用封装一些快速创建结果的方法避免多次new对象
Result
public class Result<T> {
private int code;
private String msg;
private T data;
//成功的时候调用
public static <T> Result<T> success(T data){
return new Result<T>(data);
}
//构造器私有防止直接new对象
private Result(T data) {
this.code = 0;
this.msg = "success";
this.data = data;
}
//失败的时候调用
public static <T> Result<T> error(CodeMsg cm){
return new Result<T>(cm);
}
private Result(CodeMsg cm) {
if(cm==null){
return;
}
this.code = cm.getCode();
this.msg = cm.getMsg();
}
CodeMsg
public class CodeMsg {
private int code;
private String msg;
//这里可以慢慢加不同的状态
public static CodeMsg SUCCESS = new CodeMsg(0,"success");
public static CodeMsg SERVER_ERROR = new CodeMsg(500100,"服务端异常");
//登陆模块5002XX
//商品模块5003XX
//订单模块5004XX
//秒杀模块5005XX
private CodeMsg(int code, String msg) {
this.code = code;
this.msg = msg;
}
使用对比
//返回成功
new Result(0,"success",data);
Result.success(data);
//返回失败
new Result(500100,"异常")
Result.error(cm); //cm是CodeMsg对象 如CodeMsg.SERVER_ERROR
如下图所示:
封装完之后不需要new操作,直接调用相关方法即可
三、集成Mybatis
Spring Boot集成Mybatis参考文档:链接
导包
推荐一个找maven包的网站:链接
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
配置
#Mybatis
#mapper.xml位置
mybatis.mapper-locations=classpath:com/hsw/miaosha/dao/*.xml
#指定POJO扫描包来让mybatis自动扫描到自定义POJO
mybatis.type-aliases-package=com.hsw.miaosha.domain
#下滑线转驼峰
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=3000
# druid
spring.datasource.url=jdbc:mysql://192.168.174.10:3306/miaosha?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat
spring.datasource.maxActive=1000
spring.datasource.initialSize=100
spring.datasource.maxWait=60000
spring.datasource.minIdle=500
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20
测试一波
注意:使用@Repository注解时需要加上此配置@MapperScan(“com.hsw.miaosha.dao”)
四、集成Redis
Redis安装
redis安装过程基本流程
- make -j4 //用4核加快编译
- make install 安装
- 开放端口号6379
- reidis.conf文件中设置
- bind = 0.0.0.0
- daemonize = yes //允许后台执行
- requirepass 123456 //设置密码
- 使redis成为系统服务 utils/install_server.sh
- chkconfig --list //查看系统服务
导包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
//对象序列化
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
配置
application.properties
#redis
redis.host=192.168.174.10
redis.port=6379
redis.timeout=10
redis.password=123456
redis.poolMaxTotal=1000
redis.poolMaxIdle=500
redis.poolMaxWait=500
新建redis.RedisConfig接收配置中的参数
@Component
@ConfigurationProperties(prefix="redis")
public class RedisConfig {
private String host;
private int port;
private int timeout;//秒
private String password;
private int poolMaxTotal;
private int poolMaxIdle;
private int poolMaxWait;//秒
新建redis.RedisPoolFactory获取JedisPool
@Service
public class RedisPoolFactory {
@Autowired
private RedisConfig redisConfig;
@Bean
public JedisPool jedisPoolFactory(){
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000);
JedisPool jp = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),
redisConfig.getTimeout()*1000, redisConfig.getPassword(), 0);
return jp;
}
}
通用缓存key封装
使用模板模式
1.接口
public interface KeyPrefix {
public int expireSeconds();
public String getPrefix();
}
2.抽象类
public abstract class BasePrefix implements KeyPrefix{
//默认0永不过期
private int expireSeconds;
private String prefix;
public BasePrefix(int expireSeconds, String prefix) {
this.expireSeconds = expireSeconds;
this.prefix = prefix;
}
public BasePrefix(String prefix) {
this.expireSeconds = 0;
this.prefix = prefix;
}
@Override
public int expireSeconds() {
return expireSeconds;
}
@Override
public String getPrefix() {
String simpleName = getClass().getSimpleName();
return simpleName+":"+prefix;
}
}
3.实现类
public class UserKey extends BasePrefix {
public UserKey(int expireSeconds, String prefix) {
super(expireSeconds, prefix);
}
public UserKey(String prefix) {
super(prefix);
}
public static UserKey getById = new UserKey("id");
public static UserKey getByName = new UserKey("name");
}
封装RedisService
Jedis使用介绍:链接
@Service
public class RedisService {
@Autowired
private JedisPool jedisPool;
public <T> T get(KeyPrefix prefix,String key,Class<T> clazz){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String realKey = prefix.getPrefix()+key;
String value = jedis.get(realKey);
T t = stringToBean(value,clazz);
return t;
} finally {
returnToPool(jedis);
}
}
public <T> boolean set(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 {
//如果 key 已经存在, SETEX 命令将会替换旧的值。
jedis.setex(realKey, seconds, str);
}
return true;
} finally {
returnToPool(jedis);
}
}
/**
* 判断key是否存在
* */
public <T> boolean exists(KeyPrefix prefix, String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = prefix.getPrefix() + key;
return jedis.exists(realKey);
}finally {
returnToPool(jedis);
}
}
/**
* 增加值
* */
public <T> Long incr(KeyPrefix prefix, String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = prefix.getPrefix() + key;
return jedis.incr(realKey);
}finally {
returnToPool(jedis);
}
}
/**
* 减少值
* */
public <T> Long decr(KeyPrefix prefix, String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = prefix.getPrefix() + key;
return jedis.decr(realKey);
}finally {
returnToPool(jedis);
}
}
private <T> 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 {
return JSON.toJSONString(value);
}
}
private <T> T stringToBean(String str,Class<T> clazz) {
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 {
return JSON.toJavaObject(JSON.parseObject(str), clazz);
}
}
private void returnToPool(Jedis jedis) {
if(jedis!=null){
jedis.close();
}
}
}