Integration of the SSH framework and Redis (2), integration of the ssh framework redis
5. Implementation of the add function
Create an Action: RClasAction to add course data to Redis and synchronize it to MySQL.
Package com. school. action; import java. util. arrayList; import java. util. hashMap; import java. util. list; import org. springframework. beans. factory. annotation. autowired; import com. opensymphony. xwork2.ActionSupport; import com. school. entity. clas; import com. school. redisclient. redisService; import com. school. redisclient. redisTool; import com. school. service. clasService; @ SuppressWarnings ("serial") public class RClasAction extends ActionSupport {@ Autowired private ClasService clasService; RedisService rs = RedisTool. getRedisService (); List <Clas> claslist = new ArrayList <Clas> (); private Clas clas; public Clas getClas () {return clas;} public void setClas (Clas) {this. clas = Clas;} public String execute () {saveClas (clas); return SUCCESS;} @ SuppressWarnings ({"rawtypes", "unchecked"}) private void saveClas (Clas c) {List <String> ids = rs. getList ("clas: id"); // clas: id int num = ids. size (); int id = Integer. parseInt (ids. get (num-1) + 1; rs. rightPushList ("clas: id", String. valueOf (id); // clas: count int count = Integer. parseInt (rs. get ("clas: count"); rs. set ("clas: count", String. valueOf (count + 1),-1); // Add HashMap hashmap = new HashMap (); hashmap. put ("ID", String. valueOf (id); hashmap. put ("NAME", clas. getName (); hashmap. put ("COMMENT", clas. getComment (); rs. addHash ("clas:" + id, hashmap); // synchronize to MySQL clasService. saveClas (clas );}}
Clas: id is a List-type Key-Value. It records all the course IDs, extracts the last ID, and then + 1 as the ID of the added course. At the same time, clas: the value of count must be + 1. Add a Hash Key-Value (that is, a course) to Redis using the addHash () method ):
@SuppressWarnings({ "unchecked", "rawtypes" }) public synchronized void addHash(K key, HashMap map){ redisTemplate.opsForHash().putAll(key, map); }
At the same time, add this course to MySQL.
6. Implementation of the delete function
Create an Action: RClasDeleteAction to delete Redis course data and synchronize it to MySQL.
Package com. school. action; import org. springframework. beans. factory. annotation. autowired; import com. opensymphony. xwork2.ActionSupport; import com. school. redisclient. redisService; import com. school. redisclient. redisTool; import com. school. service. clasService; @ SuppressWarnings ("serial") public class RClasDeleteAction extends ActionSupport {@ Autowired private ClasService clasService; RedisService rs = RedisTool. getRedisService (); private int id; public int getId () {return id;} public void setId (int id) {this. id = id;} public String execute () {deleteClas (id); // synchronize to MySQL clasService. deleteClas (id); return SUCCESS;} private void deleteClas (int id) {// Delete rs. del ("clas:" + id); // clas: count int count = Integer. parseInt (rs. get ("clas: count"); rs. set ("clas: count", String. valueOf (count-1),-1); // clas: id rs. delListItem ("clas: id", String. valueOf (id ));}}
Directly Delete clas: id, and then the value of clas: count-1. The two steps are relatively simple. The complex is to delete the id of the course from clas: ID, the delListItem () method is used for implementation:
@Override public synchronized void delListItem(K key, V value){ redisTemplate.opsForList().remove(key, 1, value); }
The redisTemplate. opsForList (). remove () method is similar to the LREM command. Finally, the same course is also deleted in MySQL.
7. Implementation of the modification function
Create an Action: RClasUpdateAction to delete Redis course data and synchronize it to MySQL.
Package com. school. action; import java. util. hashMap; import org. springframework. beans. factory. annotation. autowired; import com. opensymphony. xwork2.ActionSupport; import com. school. entity. clas; import com. school. redisclient. redisService; import com. school. redisclient. redisTool; import com. school. service. clasService; @ SuppressWarnings ("serial") public class RClasUpdateAction extends ActionSupport {@ Autowired private ClasService clasService; RedisService rs = RedisTool. getRedisService (); private Clas clas; public Clas getClas () {return clas;} public void setClas (Clas clas) {this. clas = clas ;}@ SuppressWarnings ({"unchecked", "rawtypes"}) public String execute () {HashMap hashmap = new HashMap (); hashmap. put ("ID", String. valueOf (clas. getId (); hashmap. put ("NAME", clas. getName (); hashmap. put ("COMMENT", clas. getComment (); rs. putHash ("clas:" + clas. getId (), hashmap); // synchronize to MySQL clasService. updateClas (clas); return SUCCESS ;}}
The putHash () method is used to update:
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public synchronized void putHash(K key, HashMap map){ redisTemplate.boundHashOps(key).putAll(map); }
Perform the same update operation in MySQL.