项目框架搭建
Spring Boot环境搭建
集成Thymeleaf Result结果搭建
集成Mybatis+Druid
集成Jedis+Redis安装+通用缓存Key封装
加入依赖pom.xml
Spring Boot依赖、thymeleaf依赖、 Mybatis、 mysql、 druid、 Jedis依赖
<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>2.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<scope>1.0.5</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
配置文件application.properties
thymeleaf
spring.thymeleaf.prefix=classpath:/templates
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
mybatis
mybatis.type-aliases-package=com.example.domain.model
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=3000
mybatis.mapper-locations=classpath:edu/cn/dao/*.xml
druid数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/miaosha?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#spring.datasource.type=com.alibaba.druid.poll.DruidDataSource
spring.datasource.filters=stat
spring.datasource.maxActive=2
spring.datasource.initialSize=1
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
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
Redis
redis.host=127.0.0.1
redis.port=6379
redis.timeout=3
redis.password=123456
redis.poolMaxTotal=10
redis.poolMaxIdle=10
redis.poolMaxWait=3
集成Thymeleaf
Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。既可以静态显示页面,也可以使页面动态显示。
1、加入依赖,生成配置文件
2、在resources目录下新建templates,新建hello.html页面
3、在Controller里面进行测试
集成mybatis+druid
1、在pom.xml文件中添加mybatis、jdbc和druid的依赖
2、在application.properties添加mybatis配置项,配置数据源druid
3、在controller里写一个方法
@RequestMapping("/db/get")
@ResponseBody
public Result<User> dbGet(){
return Result.success(userService.getByID(1));
}
4、Service代码,UserService 里面注入userDao
@Autowired
UserDao userDao;
public User getByID(int id){
return userDao.getByID(id);
}
5、Dao层、pojo层代码实现
集成Jedis+Redis
Jedis就是集成了redis的一些命令操作,封装了redis的java客户端,提供了连接池管理。一般不直接使用jedis,而是在其上在封装一层,作为业务的使用。
1、添加Jedis、Fastjson依赖
2、添加redis的配置项
3、新建一个包redis,在里面新建一个RedisConfig类
@Component
@ConfigurationProperties(prefix="redis")// 指定配置文件里面前缀为"redis"的配置项,与配置项里面的属性对应起来。
4、在redis包中创建RedisService类来提供所有关于redis的服务方法。
//封装了常用的Redis 方法
public T get(KeyPrefix prefix,String key,Class<T> clazz) 根据key取得缓存中值
public boolean set(KeyPrefix prefix,String key,T value) 根据key设置缓存中值
public boolean exitsKey(KeyPrefix prefix,String key) 是否存在key
public Long incr(KeyPrefix prefix,String key) 自增
public Long decr(KeyPrefix prefix,String key) 自减
private <T> String beanToString(T value) 将Bean对象转换成字符串
private <T> T stringToBean(String str,Class<T> clazz) 将字符串转换为Bean对象
5、在redis包中创建RedisPool类,(将原本写在RedisService类中的JedisPool方法单独分离出一个类),通过配置文件,生成Jedis连接池
6、缓存key
接口
public interface KeyPrefix {
public int expireSeconds();
public String getPrefix();
}
抽象类
public abstract class BasePrefix implements KeyPrefix {
private int expireSeconds;
private String prefix;
public BasePrefix(String prefix) {
this(0, prefix);
}
public BasePrefix(int expireSeconds, String prefix) {
this.expireSeconds = expireSeconds;
this.prefix = prefix;
}
@Override
public int expireSeconds() {
return expireSeconds;
}
@Override
public String getPrefix() {
String className = getClass().getSimpleName();
return className + ":" + prefix;
}
具体实现类
user的key的过期时间为不会过期
public class UserKey extends BasePrefix {
public UserKey(String prefix) {
super(prefix);
}
public static UserKey getById = new UserKey("id");
public static UserKey getByName = new UserKey("name");
}
设置有效期时间为2天
public class MiaoshaUserKey extends BasePrefix {
public static final int TOKEN_EXPIRE = 3600*24*2;
public MiaoshaUserKey(int expireSeconds, String prefix) {
super(expireSeconds, prefix);
}
public static MiaoshaUserKey token = new MiaoshaUserKey(TOKEN_EXPIRE, "tk");
}