当前位置: 首页 > news >正文

依宝诺手表官方网站哈尔滨建站流程

依宝诺手表官方网站,哈尔滨建站流程,wordpress侧边栏怎么加php代码,西数网站管理助手点击蓝字 关注我们 作者#xff1a;xfkhttps://www.cnblogs.com/xfk1999/p/11347793.html一直想在springboot上集成带缓存的redis#xff0c;终于成功了。网上有1000种写法#xff0c;想找到一篇合适的还真不容易?。走下流程#xff0c;加深下印象。环境:springboot版本xfkhttps://www.cnblogs.com/xfk1999/p/11347793.html一直想在springboot上集成带缓存的redis终于成功了。网上有1000种写法想找到一篇合适的还真不容易?。走下流程加深下印象。 环境:springboot版本2.1.7orm框架mybatis实现?:在serviceImpl层方法上加注解Cacheable和CachEvict。Cacheable把数据放进redis下一次需要数据直接在缓存中取CacheEvict使redis中的缓存失效。关于注解的更多详细可以参考https://www.cnblogs.com/fashflying/p/6908028.html写得很详细。准备工作pom.xml?org.springframework.bootspring-boot-starter-parent2.1.7.RELEASEorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestjavax.servletjavax.servlet-apijavax.servletjstlorg.apache.tomcat.embedtomcat-embed-jasperorg.springframework.bootspring-boot-devtoolstruemysqlmysql-connector-java5.1.21org.mybatis.spring.bootmybatis-spring-boot-starter1.1.1org.mybatis.generatormybatis-generator-core1.3.7com.fasterxml.jackson.datatypejackson-datatype-jsr310org.springframework.bootspring-boot-starter-data-redisapplication.properties文件?#mvcspring.mvc.view.prefix/WEB-INF/jsp/spring.mvc.view.suffix.jsp#mysqlspring.datasource.urljdbc:mysql://localhost:3306/common?characterEncodingutf-8spring.datasource.usernamexfkspring.datasource.password123456spring.datasource.driver-class-namecom.mysql.jdbc.Driver#mybatismybatis.mapper-locationsclasspath:/mapper/*.xmlmybatis.type-aliases-packagecom.xfk.sb.pojo#redisspring.redis.database0spring.redis.host127.0.0.1spring.redis.port6379spring.redis.passwordspring.redis.timeout5000书写一允许使用缓存?在springboot的主启动类上添加注解EnableCachingSbApplication.javapackage com.xfk.sb;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;SpringBootApplicationEnableCachingpublic class SbApplication {public static void main(String[] args) { SpringApplication.run(SbApplication.class, args); }} 二redis配置类?这里一个重要的点就是Serializer。RedisCache默认使用的是JdkSerializationRedisSerializer我们要实现json格式的数据在redis上的存储。利用Jackson2JsonRedisSerializer或GenericJackson2JsonRedisSerializer其优点是存储的长度小。在这里我们用GenericJackson2JsonRedisSerializer。成功之后可以换Jackson2JsonRedisSerializer试试看一看存储的数据有什么不同。?cacheManager方法是用作注解Cacheable和CacheEvict执行service实现层方法缓存数据的另外就是定义一个redisTemplate哪个controller需要就在哪个controller中注入灵活使用。RedisConfig.javapackage com.xfk.sb.config;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheConfiguration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.*;import java.time.Duration;ConfigurationEnableCachingpublic class RedisConfig extends CachingConfigurerSupport {// 过期时间private Duration timeToLive Duration.ofHours(12);Beanpublic RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) { RedisCacheConfiguration config RedisCacheConfiguration.defaultCacheConfig() .entryTtl(this.timeToLive) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer())) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer())) .disableCachingNullValues();return RedisCacheManager.builder(connectionFactory) .cacheDefaults(config) .transactionAware() .build(); } Bean(name redisTemplate)public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate redisTemplate new RedisTemplate(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(keySerializer()); redisTemplate.setHashKeySerializer(keySerializer()); redisTemplate.setValueSerializer(valueSerializer()); redisTemplate.setHashValueSerializer(valueSerializer());return redisTemplate; }private RedisSerializer keySerializer() {return new StringRedisSerializer(); }private RedisSerializer valueSerializer() {return new GenericJackson2JsonRedisSerializer(); }}三增删改查Demo简单的pojo类Student.javapackage com.xfk.sb.pojo;public class Student {private int id;private String name;private int age;public Student() { }public int getId() {return id; }public void setId(int id) {this.id id; }public String getName() {return name; }public void setName(String name) {this.name name; }public int getAge() {return age; }public void setAge(int age) {this.age age; }Overridepublic String toString() {return Student{ id id , name name \ , age age }; }}?我使用的是mybatis的注解方式简单的几个增删改查方法。xml文件studentMapper.xml?xml version1.0 encodingUTF-8?/span PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd select * from student delete from student where id #{id} insert into student(id, name, age) values(null, #{student.name}, #{student.age}) update student set name#{student.name}, age#{student.age} where id #{student.id} select * from student where id #{id} limit 1mapper接口StudentMapper.javapackage com.xfk.sb.mapper;import com.xfk.sb.pojo.Student;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.springframework.stereotype.Component;import java.util.List;MapperComponentpublic interface StudentMapper {List selectStudent();int deleteStudent(Param(id)int id);int createStudent(Param(student)Student student);int updateStudent(Param(student)Student student);Student selectStudentByPrimaryKey(Param(id)int id);}service层接口StudentService.javapackage com.xfk.sb.service;import com.xfk.sb.pojo.Student;import java.util.List;public interface StudentService {List selectStudent();int deleteStudent(int id);int createStudent(Student student);int updateStudent(Student student);Student selectStudentByPrimaryKey(int id);}service实现层StudentServiceImpl.javapackage com.xfk.sb.service.implement;import com.xfk.sb.mapper.StudentMapper;import com.xfk.sb.pojo.Student;import com.xfk.sb.service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.cache.annotation.Caching;import org.springframework.stereotype.Service;import java.util.List;ServiceCacheConfig(cacheNamesstudents)public class StudentServiceImpl implements StudentService {private final StudentMapper studentMapper;Autowiredpublic StudentServiceImpl(StudentMapper studentMapper) {this.studentMapper studentMapper; }Cacheable(keystudents)Overridepublic List selectStudent() { System.out.println(从数据库中取selectStudent);return studentMapper.selectStudent(); }OverrideCaching(evict{ CacheEvict(keysingleStudent#id),CacheEvict(keystudents), })public int deleteStudent(int id) { System.out.println(从数据库中删除deleteStudent);return studentMapper.deleteStudent(id); }OverrideCaching(evict{ CacheEvict(keysingleStudent#student.id),CacheEvict(keystudents), })public int createStudent(Student student) { System.out.println(从数据库中创建createStudent);return studentMapper.createStudent(student); }Caching(evict{ CacheEvict(keysingleStudent#student.id),CacheEvict(keystudents), })Overridepublic int updateStudent(Student student) { System.out.println(从数据库中更新updateStudent);return studentMapper.updateStudent(student); }Cacheable(keysingleStudent#p0)Overridepublic Student selectStudentByPrimaryKey(int id) { System.out.println(从数据库中取一个selectStudentByPrimaryKey);return studentMapper.selectStudentByPrimaryKey(id); }}?使用CacheConfig注解相当于在redis数据库下建一个文件夹以cacheNames作为文件夹的名字统一管理这个实现层缓存的数据。正如在Redis Desktop Manager下看到的目录结构db0下有一个students文件夹。?使用Cacheable注解使缓存生效以实现层的selectStudentByPrimaryKey()方法为例从数据库中根据id查询一个Student对象。使用Cacheable(keysingleStudent#p0)#p0就是形参parameter0多个参数就是#p1#p2也可以写成#id注意singleStudent字符串一定要用单引号扩上然后使用字符串的拼接模式这个变量的规则是spring的EL表达式一定要用加上#符号如果是一个对象则可以直接用.引用属性参考createStudent()方法中的#student.id 。属性key相当于在students文件夹下的文件夹创建一条以singleStudent#p0为名字的一条缓存数据在Redis Desktop Manager可以看到由于students文件夹下的文件夹没有名字所以成功缓存数据的命名是students::singleStudent1两个引号之间为空。这就相当以这个命名空间下的唯一key可以根据唯一key准确的失效缓存数据。?CacheEvict注解使缓存失效根据需求要保证数据库与缓存的一致性所以操作数据库之后要同步缓存。在更新删除和增加后要使缓存失效不能返回过时的信息。在这里使用Caching的目的是使多条缓存失效它集合了CacheableCacheEvictCachePut可以很直观的管理生效与失效。还可以直接使用CacheEvict(allEntriestrue)使这个命名空间下的所有缓存失效。到这里核心工作完成得差不多了就还差controller返回视图层了。controller层StudentController.javapackage com.xfk.sb.web;import com.xfk.sb.pojo.Student;import com.xfk.sb.service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.*;import java.util.List;Controllerpublic class StudentController {private final StudentService studentService;private final StringRedisTemplate redis;Autowiredpublic StudentController(StudentService studentService, StringRedisTemplate redis) {this.studentService studentService;this.redis redis; }GetMapping(/students)public String listStudent(Model model){ List students studentService.selectStudent(); model.addAttribute(students, students);return listStudent; }DeleteMapping(/students/{id})public String deleteStudent(PathVariable(id)int id) throws Exception{ studentService.deleteStudent(id);return redirect:/students; }PutMapping(/students)public String updateStudent(Student student){ studentService.updateStudent(student);return redirect:/students; }PostMapping(/students)public String createStudent(Student student){ studentService.createStudent(student);return redirect:/students; }GetMapping(/students/{id})public String editStudent(PathVariable(id)int id, Model model){ Student s studentService.selectStudentByPrimaryKey(id); model.addAttribute(student, s);return editStudent; }RequestMapping(/test)public String test(Model model){ List students studentService.selectStudent(); model.addAttribute(students, students);return test; }RequestMapping(/getOne)public String getOne(Model model){       // 获取id为1的Student对象到test.jsp Student student studentService.selectStudentByPrimaryKey(1); model.addAttribute(student, student);return test; }}?使用的restful风格返回的jsp页面/test和/getOne用来验证缓存是否生效。小贴一下jsplistStudent.jspstudents 增加: name: idnameage编辑删除${each.id}${each.name}${each.age}修改删除验证你的jquer是否生效 $(function(){ $(.deleteStudent).click(function(){var href $(this).attr(value); alert(href); $(#deleteType).attr(action, href).submit(); }) $(.hhh).click(function(){ alert(你的jquer已生效); }) })editStudent.jspeditStudent name: age :返回主页test.jsptest${each.id}, ${each.name}, ${each.age}得到一个Student${student.id}, ${student.name}, ${student.age} 四验证测试?步骤1/getOne由于StudentServiceImpl.java中的System.out.println(从数据库中取一个selectStudentByPrimaryKey); 查看后台控制台可以知道这条数据是从数据库中获取的。/getOne获取的Student的id为1所以会在Redis Desktop Manager中看到一条singleStudent1的缓存记录。http://localhost:8080/getOne2更改Redis Desktop Manager中的记录?比如redis库中原来的数据是?更改数据因为这里改的数据是redis的mysql里的数据还是没变这样就知道了是从缓存中读取的数据?点击save之后刷新http://localhost:8080/getOne 成功????? 然后CacheEvict是一样的逻辑指定失效的key就好了在看不好意思那就点个赞吧
http://www.zqtcl.cn/news/945807/

相关文章:

  • 社区网站建设申请报告WordPress评论通知邮箱
  • 佛山网站建设技术托管建设网站容易吗
  • 网站开发的层级结构iis6.0如何做网站301
  • 做旅游那些网站好个人博客怎么做
  • 中国最好网站建设公司网站前台做好之后再怎么做
  • 焦作整站优化app开发报价单及方案
  • 网站开发合同验收怎样建立网站 优帮云
  • 池州哪家做网站wordpress方小程序主题
  • 免费建设网站入驻七牛云存储wordpress
  • 上海专业的网站吕梁做网站公司
  • 网站视频链接国际物流网站模板
  • 用asp.net和access做的关于校园二手网站的论文网站环境搭建好后怎么做网站
  • 如何查网站的外链哈尔滨微信网站开发
  • 洛阳设计网站公司建设银行网站 购买外汇
  • 做视频网站的备案要求吗给工厂做代加工
  • 网站建设技术外包西安推荐企业网站制作平台
  • 建立一个做笔记的网站石家庄网站优化
  • 服务器创建多个网站吗中铁雄安建设有限公司网站
  • 建湖建网站的公司网站建设人工费
  • 沈阳公司网站设计公司怎么投放广告
  • 上海哪家做网站关键词排名如何做简洁网站设计
  • 网站维护的内容seo网站关键词优化哪家好
  • 东阳市网站建设西安做网站选哪家公司
  • 宁津网站开发万能应用商店下载
  • 专业制作标书网站地图优化
  • 广州建网站兴田德润团队什么是网络营销详细点
  • win7建网站教程wordpress chrome插件开发
  • 免费行情软件网站下载视频公司介绍ppt制作模板
  • wordpress快速建站wordpress短代码可视化
  • 餐饮型网站开发比较好看的网页设计