1.在maven引入相关的依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0..RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.</version>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 支持 @ConfigurationProperties 注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.</version>
</dependency>
<!-- mysql连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.</version>
</dependency>
2.SpringBoot加载文件application.yml

mysql-connector-java6.0的url需要加载时区,driverClassName变为com.mysql.cj.jdbc.Driver。redis的密码在redis.config中修改。

com.mysql.cj.jdbc.Driver
server:
port:
session-timeout:
tomcat.max-threads:
tomcat.uri-encoding: UTF-
spring:
datasource:
type : com.alibaba.druid.pool.DruidDataSource
url : jdbc:mysql://localhost:3306/book_store?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
username : root
password : root
driverClassName : com.mysql.cj.jdbc.Driver
initialSize :
minIdle :
maxActive :
maxWait :
timeBetweenEvictionRunsMillis :
minEvictableIdleTimeMillis :
validationQuery : SELECT FROM DUAL
testWhileIdle : true
testOnBorrow : false
testOnReturn : false
poolPreparedStatements : true
maxPoolPreparedStatementPerConnectionSize :
filters : stat,wall
connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis :
useGlobalDataSourceStat : true mybatis:
type-aliases-package: com.qyj.entity
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
http:
encoding:
force : true
charset : UTF-
enables : true thymeleaf:
cache: false
mode: HTML5
prefix: classpath\:/static/
suffix: .html
encoding: UTF-
content: text/html redis:
host: 127.0.0.1
port:
password:
timeout:
3.mybatis配置类MybatisConfig
@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {
@Autowired
DataSource dataSource;
   //注入pageHelper
@Bean
public PageInterceptor pageInterceptor() throws IOException {
PageInterceptor pageInterceptor = new PageInterceptor();
Properties props = new Properties();
props.setProperty("reasonable", "true");
props.setProperty("supportMethodsArguments", "true");
props.setProperty("returnPageInfo", "check");
props.setProperty("params", "count=countSql");
pageInterceptor.setProperties(props);
return pageInterceptor;
} @Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
} @Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
4.redis配置(参考springboot2.0文档所写)
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.password}")
private String password;
@Bean
public KeyGenerator wiselyKeyGenerator(){//主键生成策咯
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
} @Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
} @Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration factory = new RedisStandaloneConfiguration();
factory.setHostName(host);
factory.setPort(port);
factory.setPassword(RedisPassword.of(password));
return new JedisConnectionFactory(factory);
} }
5.Service(PageHelp5.0用法)
@Cacheable("queryGgdmDataList")
public ResponseData queryGgdmDataList(Map<String, Object> queryParams) {
int index = Integer.parseInt(queryParams.get("page").toString());
int size = Integer.parseInt(queryParams.get("rows").toString());
PageHelper.startPage(index, size);
StringBuilder sql = new StringBuilder();
List<Map<String,Object>> commList = mybatisDao.queryListDataBySql(sql.toString());
PageInfo page = new PageInfo(commList) ;
List list = page.getList();
ResponseData pageResult = new ResponseData((int) page.getTotal(), list);
return pageResult;
}
6.SpringBoot启动
@SpringBootApplication
@ServletComponentScan
@EnableAutoConfiguration
@MapperScan("xx.xx.dao")//扫描的Dao层
public class ApplicationSpringBoot {
public static void main(String[] args) {
SpringApplication.run(ApplicationSpringBoot.class,args);
} }
7.Controller采用Restful风格
@RestController
@RequestMapping("/framework")
public class CommController {
@RequestMapping( value = "/bzfl/getComboxData", method =
RequestMethod.GET)
@ResponseBody
public List<Map<String,Object>> getComboxData(@RequestParam String ssfl){
return commService.getComboxData(ssfl);
}
}
9.数据缓存到redis(工具Redis Resktop Manager)

SpringBoot2.0+Mybatis+PageHelper+Redis实现缓存的更多相关文章

  1. springboot2.0+mybatis多数据源集成

    最近在学springboot,把学的记录下来.主要有springboot2.0+mybatis多数据源集成,logback日志集成,springboot单元测试. 一.代码结构如下 二.pom.xml ...

  2. spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!

    前言 开心一刻 小白问小明:“你前面有一个5米深的坑,里面没有水,如果你跳进去后该怎样出来了?”小明:“躺着出来呗,还能怎么出来?”小白:“为什么躺着出来?”小明:“5米深的坑,还没有水,跳下去不死就 ...

  3. (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)

    一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...

  4. Mybatis使用Redis二级缓存

    在Mybatis中允许开发者自定义自己的缓存,本文将使用Redis作为Mybatis的二级缓存.在Mybatis中定义二级缓存,需要如下配置: 1. MyBatis支持二级缓存的总开关:全局配置变量参 ...

  5. mybatis整合redis二级缓存

    mybatis默认开启了二级缓存功能,在mybatis主配置文件中,将cacheEnabled设置成false,则会关闭二级缓存功能 <settings> <!--二级缓存默认开启, ...

  6. SpringBoot2.0之五 优雅整合SpringBoot2.0+MyBatis+druid+PageHelper

    上篇文章我们介绍了SpringBoot和MyBatis的整合,可以说非常简单快捷的就搭建了一个web项目,但是在一个真正的企业级项目中,可能我们还需要更多的更加完善的框架才能开始真正的开发,比如连接池 ...

  7. SpringBoot + MySQL + MyBatis 整合 Redis 实现缓存操作

    本地安装 Redis Redis 安装:https://www.cnblogs.com/oukele/p/11373052.html 项目结构:  SpringBootRedis 工程项目结构如下: ...

  8. springboot2.0 Mybatis 整合

    原文:https://blog.csdn.net/Winter_chen001/article/details/80010967 环境/版本一览: 开发工具:Intellij IDEA 2017.1. ...

  9. SSM+redis整合(mybatis整合redis做二级缓存)

    SSM:是Spring+Struts+Mybatis ,另外还使用了PageHelper 前言: 这里主要是利用redis去做mybatis的二级缓存,mybaits映射文件中所有的select都会刷 ...

随机推荐

  1. [转] 基于Gitlab CI搭建持续集成环境

    [From] https://blog.csdn.net/wGL3k77y9fR1k61T1aS/article/details/78798577 前言 本文是在12月12号迅雷@赵兵在前端早读课第三 ...

  2. django-获取当前url和ip

    1.添加'django.template.context_processors.request', 2.在模板的html中输入 {{ request.path}}{{ request.get_host ...

  3. portmap安装

  4. mAP(mean Average Precision)应用(转)

    原文章地址来自于知乎:https://www.zhihu.com/question/41540197 1. precision 和 recall 的计算(没什么好说的,图片示例相当棒): 图1 图中上 ...

  5. thinkphp5+nginx的linux环境搭建

    安装环境&工具安装php安装nginx运行服务器安装thinkphp安装Composer安装thinkphp配置nginx.conf配置php-fpm运行thinkphp注意事项 php7已经 ...

  6. Android创建定时和周期任务

    问题:应用需要按时执行某个操作,例如定时更新UI. 解决方案:使用Handler提供的定时操作功能.通过Handler,可以在指定的时间或是指定的延时后执行操作. 下面看一个在TextView中显示当 ...

  7. 【LESS系列】简介和使用

    LESS —— 一个CSS预编译框架,它在CSS的语法基础之上,引入了变量.Mixin(混入).运算以及函数等功能,大大简化了CSS的编写,并且降低了CSS的维护成本,就像它的名称所说的那样,LESS ...

  8. C 标准库 - string.h之memchr使用

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

  9. Hibernate 一对一映射(惟一外键)

  10. CentOS7 防火墙操作

    1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...