博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java框架-15spring4_mybatis04-EHcahce-logback
阅读量:5242 次
发布时间:2019-06-14

本文共 7643 字,大约阅读时间需要 25 分钟。

1.代码区

  

package cn.sxt.dao;import java.util.List;import cn.sxt.vo.User;public interface UserDao {     List
selectUser(); int insert(User user); }
UserDao
insert into user(name,pwd) values(#{name},#{pwd})
deletes from user where id=#{id}
user.mapper.xml
package cn.sxt.service;import java.util.List;import org.springframework.stereotype.Service;import cn.sxt.vo.User;public interface UserService {    List
selectUser(); int insert(User user); }
UserService
package cn.sxt.service.impl;import java.util.List;import javax.annotation.Resource;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import cn.sxt.dao.UserDao;import cn.sxt.service.UserService;import cn.sxt.vo.User;@Servicepublic class UserServiceImpl implements UserService {    @Resource    private UserDao userDao;        //key不是必须的    @Cacheable(value = "ehcache_test",key="'user'+#key")      public List
selectUser(){ return userDao.selectUser(); } //清空该value的缓存 @CacheEvict(value="ehcache_test",key="0",beforeInvocation=true) public int insert(User user) { return userDao.insert(user); }}
UserServiceImpl
package cn.sxt.test;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.sxt.dao.UserDao;import cn.sxt.service.UserService;import cn.sxt.vo.User;public class Test {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");        UserService userDao = (UserService)context.getBean(UserService.class);        System.out.println(userDao.insert(new User("min", 1, "ad")));        System.out.println(userDao.selectUser().size());        //test();    }        public static void test() {        //ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");            /*ApplicationContext context = new ClassPathXmlApplicationContext("springMVV-servlet.xml");        UserDao userDao = (UserDao)context.getBean(UserDao.class);        System.out.println(userDao.insert(new User("zhangsna", "1234")));        System.out.println(userDao.selectUser().size());*/          // 1. 创建缓存管理器       // CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache.xml");         CacheManager cacheManager = CacheManager.create("src/ehcache.xml");        // 2. 获取缓存对象        Cache cache = cacheManager.getCache("ehcache_test");      /*  // 3. 创建元素        Element element = new Element("key", "value1");      //  Element element1 = new Element("key1", "value1");        Element element3 = new Element("key3", "value1");        Element element4 = new Element("key4", "value1");        Element element5 = new Element("key5", "value1");        Element element6 = new Element("key6", "value1");        Element element7 = new Element("key7", "value1");        Element element8 = new Element("key8", "value1");        Element element9= new Element("key9","value1");        Element element0 = new Element("key0", "value1");        // 4. 将元素添加到缓存        cache.put(element);        cache.put(element1);         cache.put(element3);        cache.put(element4);        cache.put(element5);        cache.put(element6);        cache.put(element7);        cache.put(element8);        cache.put(element9);        cache.put(element0);        // 5. 获取缓存        Element value = cache.get("key");        //[ key = key1, value=value1, version=1, hitCount=1, CreationTime = 1517537797608, LastAccessTime = 1517537797611 ]        System.out.println("key1+++++++++++"+value);          //value1        System.out.println("value.getObjectValue()+++++++++++++++++"+value.getObjectValue());        // 6. 删除元素        cache.remove("key");*/        User dog  = new User("taidi",1000,"taile");        User dog1 = new User("taidi",1000,"taile");        User dog2 = new User("taidi",1000,"taile");        User dog3 = new User("taidi",1000,"taile");        User dog4 = new User("taidi",1000,"taile");        User dog5 = new User("taidi",1000,"taile");        User dog6 = new User("taidi",1000,"taile");        Element element0 = new Element("taidi", dog);        Element element1 = new Element("taidi1", dog1);        Element element2 = new Element("taidi2", dog2);        Element element3 = new Element("taidi3", dog3);        Element element4 = new Element("taidi4", dog4);        Element element5 = new Element("taidi5", dog5);        Element element6 = new Element("taidi6", dog6);        Element element7 = new Element("taidi7", dog6);        Element element8 = new Element("taidi8", dog6);        cache.put(element0);        cache.put(element1);        cache.put(element2);        cache.put(element3);        cache.put(element4);        cache.put(element5);        cache.put(element6);        cache.put(element7);        cache.put(element8);        Element value2 = cache.get("taidi1");        User dog12 = (User) value2.getObjectValue();        //User [id=1000, name=taidi, pwd=taile]        System.out.println(dog12);        //1        System.out.println(cache.getSize());        for (String cacheName : cacheManager.getCacheNames()) {            System.out.println(cacheName);        }        // 获取所有的缓存对象        for (Object key : cache.getKeys()) {            System.out.println(key);        }                   // 得到缓存对象占用内存的大小      System.out.println(cache.getMemoryStoreSize());             // 7. 刷新缓存        cache.flush();        // 8. 关闭缓存管理器       cacheManager.shutdown();            }}
Test
package cn.sxt.vo;import java.io.Serializable;public class User implements Serializable{    /**     *      */    private static final long serialVersionUID = 1L;    /**     *      */    private int id;    private String name;    private String pwd;            public User() {        super();    }    public User(String name,int id,  String pwd) {        super();        this.id = id;        this.name = name;        this.pwd = pwd;    }    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 String getPwd() {        return pwd;    }    public void setPwd(String pwd) {        this.pwd = pwd;    }}
User
bean.xml
ehcache.xml
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/orcljdbc.username=rootjdbc.password=123456
jdbc.properties

 

转载于:https://www.cnblogs.com/ou-pc/p/8543774.html

你可能感兴趣的文章
Item 9: Avoid Conversion Operators in Your APIs(Effective C#)
查看>>
学习Spring Boot:(二十八)Spring Security 权限认证
查看>>
IT学习神器——慕课网App获App Store、Android应用市场重磅推荐
查看>>
Linux网络状态工具ss命令使用详解
查看>>
深入浅出JavaScript(2)—ECMAScript
查看>>
编程珠玑第十一章----排序
查看>>
Face The Right Way POJ - 3276 (开关问题)
查看>>
STEP2——《数据分析:企业的贤内助》重点摘要笔记(六)——数据描述
查看>>
变量的命名规范
查看>>
手机端自动跳转
查看>>
react中进入某个详情页URL路劲参数Id获取问题
查看>>
首届.NET Core开源峰会
查看>>
ViewPager的onPageChangeListener里面的一些方法参数:
查看>>
Jenkins关闭、重启,Jenkins服务的启动、停止方法。
查看>>
CF E2 - Array and Segments (Hard version) (线段树)
查看>>
Linux SPI总线和设备驱动架构之四:SPI数据传输的队列化
查看>>
SIGPIPE并产生一个信号处理
查看>>
CentOS
查看>>
Linux pipe函数
查看>>
java equals 小记
查看>>