爱游戏全站app官网入口-爱游戏官网

spring mybatis基于注解整合redis -爱游戏全站app官网入口

2023-08-21,,

  基于这段时间折腾redis遇到了各种问题,想着整理一下。本文主要介绍基于spring mybatis以注解的形式整合redis。废话少说,进入正题。

  首先准备redis,我下的是windows版,下载后直接启动redis-server就行了,见下图:

一,先上jar包

二,创建实体类

 package com.sl.user.vo;
 import java.io.serializable;
 import com.fasterxml.jackson.databind.propertynamingstrategy;
import com.fasterxml.jackson.databind.annotation.jsonnaming;
import com.fasterxml.jackson.databind.annotation.jsonserialize; @jsonserialize
@jsonnaming(propertynamingstrategy.lowercasewithunderscoresstrategy.class)
public class uservo implements serializable{ private static final long serialversionuid = 1l; private int id;
private string username;
private string password;
private int age; public uservo(){
super();
} public uservo(int id, string username, string password, int age) {
super();
this.id = id;
this.username = username;
this.password = password;
this.age = age;
} public int getid() {
return id;
} public void setid(int id) {
this.id = id;
} public string getusername() {
return username;
} public void setusername(string username) {
this.username = username;
} public string getpassword() {
return password;
} public void setpassword(string password) {
this.password = password;
} public int getage() {
return age;
} public void setage(int age) {
this.age = age;
} @override
public string tostring() {
return "uservo [id=" id ", username=" username ", password="
password ", age=" age "]";
} }

三,dao接口

 package com.sl.user.dao;
 import com.sl.user.vo.uservo;
 public interface userdao {
     public void adduser(uservo user);
     public void deleteuser(uservo user);
     public void updateuser(uservo user);
     public uservo getuserbyid(int id);
     public uservo getuser(int id);
 }

四,usermapper

 







insert into t_user(username,password,age) values(#{username},#{password},#{age})

delete * from t_user where id = #{id}

update t_user set
username = #{username},
password = #{password},
age = #{age}
where 1=1
and id = #{id}

五,service接口

 package com.sl.user.service;
 import com.sl.user.vo.uservo;
     public interface userservice {
         public void adduser(uservo user);
         public void deleteuser(uservo user);
         public void updateuser(uservo user);
         public uservo getuserbyid(int id);
         public uservo getuser(int id);
 }

六,service实现

 package com.sl.user.service.impl;
 import org.springframework.beans.factory.annotation.autowired;
import org.springframework.cache.annotation.cacheevict;
import org.springframework.cache.annotation.cacheable;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.propagation;
import org.springframework.transaction.annotation.transactional; import com.sl.user.dao.userdao;
import com.sl.user.service.userservice;
import com.sl.user.vo.uservo; @service("userservice")
@transactional(propagation=propagation.required, rollbackfor=exception.class)
public class userserviceimpl implements userservice{ @autowired
private userdao userdao; @override
@cacheevict(value="user",key="adduser",allentries=true)
public void adduser(uservo user) {
userdao.adduser(user);
} @override
@cacheevict(value = {"getuser", "getuserbyid"}, allentries = true)
public void deleteuser(uservo user) {
userdao.deleteuser(user);
} @override
@cacheevict(value = {"getuser", "getuserbyid"}, allentries = true)
public void updateuser(uservo user) {
userdao.updateuser(user);
} @override
@cacheable(value="user",key="getuserbyid")
public uservo getuserbyid(int id) {
return userdao.getuserbyid(id);
} @override
@cacheable(value="user",key="'getuser'")
public uservo getuser(int id) {
return userdao.getuser(id);
} }

七,ctrl层

 package com.sl.user.web;
 import java.util.hashmap;
import java.util.map; import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.responsebody; import com.sl.user.service.userservice;
import com.sl.user.vo.uservo; @controller
@requestmapping("/userctrl")
public class userctrl { @autowired
private userservice userservice; @requestmapping("/adduser")
public void adduser(uservo user){
userservice.adduser(user);
} @requestmapping("/deleteuser")
public void deleteuser(uservo user){
userservice.deleteuser(user);
} @requestmapping("/updateuser")
public void updateuser(uservo user){
userservice.updateuser(user);
} @responsebody
@requestmapping("/getuserbyid")
public map getuserbyid(uservo user){
map map = new hashmap();
map.put("msg",userservice.getuserbyid(4));
return map;
} @responsebody
@requestmapping("/getuser")
public map getuser(uservo vo){
map map = new hashmap();
object user = userservice.getuser(4);
map.put("msg",user.tostring());
return map;
} }

八,redis关键类,用于crud操作

 package com.sl.user.redis;
import java.io.bytearrayinputstream;
import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.io.objectinputstream;
import java.io.objectoutputstream; import org.springframework.cache.cache;
import org.springframework.cache.support.simplevaluewrapper;
import org.springframework.dao.dataaccessexception;
import org.springframework.data.redis.connection.redisconnection;
import org.springframework.data.redis.core.rediscallback;
import org.springframework.data.redis.core.redistemplate; public class redisutil implements cache{ private redistemplate redistemplate;
private string name;
public redistemplate getredistemplate() {
return redistemplate;
} public void setredistemplate(redistemplate redistemplate) {
this.redistemplate = redistemplate;
} public void setname(string name) {
this.name = name;
} @override
public string getname() {
return this.name;
} @override
public object getnativecache() {
return this.redistemplate;
} /**
* 从缓存中获取key
*/
@override
public valuewrapper get(object key) {
system.out.println("get key");
final string keyf = key.tostring();
object object = null;
object = redistemplate.execute(new rediscallback
网站地图