Scenario: inventory management module of the logistics system: User Login, addition, deletion, modification, and query of the warehouse spring JdbcTemplate to implement data layer operations.
To reduce the number of I/O operations of a database, configure the AOP aspect and use Map to manually implement a cache: Clear the cache when adding, modifying, and deleting a repository; query the repository list or query the information of each repository, load data to the cache.
Implementation:
Cut plane:
Package main.com. store. aop; import java. util. collections; import java. util. hashMap; import java. util. list; import java. util. map; import main.com. store. domain. store; import org. aspectj. lang. proceedingJoinPoint;/*** AOP cache plane ** @ author lee **/@ SuppressWarnings ("all") public class CacheAspect {// use Collections for thread safety. synchronizedMap (new HashMap (); private static Map <String, Object> aopCahche = Collection S. synchronizedMap (new HashMap <String, Object> (); // private static Map <String, Object> aopCahche = new HashMap <String, Object> (); public static Map <String, Object> getAopCahche () {return aopCahche;} public static void setAopCahche (Map <String, Object> aopCahche) {CacheAspect. aopCahche = aopCahche;} public static List <Store> doCacheList (ProceedingJoinPoint point) throws Throwable {List <Store> Result = (List <Store>) point. proceed (); return result;} public static Store doCacheSingle (ProceedingJoinPoint point) throws Throwable {Store result = (Store) point. proceed (); return result;} public static void upload AchE () {aopCahche = new HashMap <String, Object> (); System. out. println ("cache cleared successfully! ");}}
Dao
Package main.com. store. dao; import java. util. list; import main.com. store. domain. store; import org. springframework. orm. hibernate3.support. hibernateDaoSupport; @ SuppressWarnings ("all") public class StoreDao extends HibernateDaoSupport {/*** query all Repository Information ** @ return */public List <Store> findAllStores () {List <Store> stores = this. getHibernateTemplate (). find ("from Store"); return stores;}/*** add Warehouse Information ** @ param store */public void add (Store store) {this. getHibernateTemplate (). save (store);}/*** delete Repository Information ** @ param id */public void delete (Store store) {this. getHibernateTemplate (). delete (store);}/*** query repository information by id ** @ param id * @ return */public Store getStoreById (String id) {Store result = null; result = this. getHibernateTemplate (). get (Store. class, id); return result;}/*** modify Repository Information ** @ param store */public void update (Store store) {this. getHibernateTemplate (). update (store );}}
VO
Package main.com. store. domain;/*** repository ** @ author lee **/public class Store {/** repository id */private String id; /** repository name */private String name;/** repository location */private String addr;/** repository administrator */private String manager; public Store () {} public String getId () {return id;} public void setId (String id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getAddr () {return addr;} public void setAddr (String addr) {this. addr = addr;} public String getManager () {return manager;} public void setManager (String manager) {this. manager = manager ;}@ Override public String toString () {return "Store [id =" + id + ", name =" + name + ", addr = "+ addr +", manager = "+ manager +"] ";}}
Service
Package main.com. store. service; import java. util. arrayList; import java. util. list; import java. util. map; import java. util. map. entry; import java. util. set; import main.com. store. aop. cacheAspect; import main.com. store. dao. storeDao; import main.com. store. domain. store; public class StoreService {private StoreDao storeDao; public void setStoreDao (StoreDao storeDao) {this. storeDao = storeDao;}/*** query all warehouse information * /Public List <Store> findAllStores () {List <Store> stores = new ArrayList <Store> (); // obtain the cache value. If there is a Map <String, object> cacheMap = CacheAspect. getAopCahche (); Set <Entry <String, Object> entrySet = cacheMap. entrySet (); if (! CacheAspect. getAopCahche (). isEmpty () {System. out. println ("query cache"); for (Entry <String, Object> entry: entrySet) {String key = entry. getKey (). toString (); System. out. println (key); Store store = (Store) entry. getValue (); stores. add (store) ;}} else {System. out. println ("query database"); stores = storeDao. findAllStores (); for (Store: stores) {cacheMap. put (store. getId (), store) ;}} return stores. isEm Pty ()? Null: stores;}/*** query repository information by id ** @ param id * @ return */public Store findStoreById (String id) {Store store = new Store (); // obtain the cache value. If Map <String, Object> cacheMap = CacheAspect exists. getAopCahche (); Set <Entry <String, Object> entrySet = cacheMap. entrySet (); if (! CacheMap. isEmpty () {System. out. println ("query cache"); for (Entry <String, Object> entry: entrySet) {String key = entry. getKey (). toString (); System. out. println (key); store = (Store) entry. getValue () ;}} else {System. out. println ("Database"); store = storeDao. getStoreById (id); cacheMap. put (store. getId (), store);} return store;}/*** add warehouse ** @ param store */public void add (Store store) {storeDao. add (store);}/*** delete Repository Information ** @ param id */public void delete (Store store) {storeDao. delete (store);}/*** modify Warehouse Information ** @ param store */public void update (Store store) {storeDao. update (store );}}
ApplicationContext. xml
<Bean id = "cacheAdvice" class = "main.com. store. aop. cacheAspect "/> <aop: config proxy-target-class =" true "> <aop: aspect ref =" cacheAdvice "> <aop: pointcut expression =" execution (* main.com. store. service. storeService. findAllStores (..)) "id =" cacheList "/> <aop: Und und method =" doCacheList "pointcut-ref =" cacheList "/> <aop: pointcut expression =" execution (* main.com. store. service. storeService. findStoreById (..)) "id = "CacheSingle"/> <aop: Und und method = "doCacheSingle" pointcut-ref = "cacheSingle"/> <aop: pointcut expression = "execution (* main.com. store. service. storeService. add (..)) "id =" clearAdd "/> <aop: pointcut expression =" execution (* main.com. store. service. storeService. delete (..)) "id =" clearDelete "/> <aop: pointcut expression =" execution (* main.com. store. service. storeService. update (..)) "id =" clearUpdate "/> <ao P: before method = "effecache" pointcut-ref = "clearAdd"/> <aop: before method = "effecache" pointcut-ref = "clearDelete"/> <aop: before method = "effecache" pointcut-ref = "clearUpdate"/> </aop: aspect> </aop: config> <! -- Repository Management --> <bean id = "storeDao" class = "main.com. store. dao. storeDao "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> <bean id =" storeService "class =" main.com. store. service. storeService "> <property name =" storeDao "ref =" storeDao "/> </bean> <bean id =" storeAction "class =" main.com. store. web. storeAction "> <property name =" storeService "ref =" storeService "/> </bean>
OK! Finished!
This article from the "Fox Spirit Legend" blog, please be sure to keep this source http://foxspark.blog.51cto.com/6273668/1344879