Spring 整合jedis

來源:互聯網
上載者:User

Spring 整合jedis

使用的是maven,所以直接上依賴包了

pom檔案 

Web pom檔案

 

4.0.0com.tgbdui1dui.parent0.0.1-SNAPSHOT../dui1dui.parent/pom.xmldui1dui.user.webwarjunitjunit  org.springframeworkspring-jdbcorg.springframeworkspring-webmvcorg.springframeworkspring-context-supportorg.springframeworkspring-test org.mybatismybatis org.mybatismybatis-spring javaxjavaee-apiprovided mysqlmysql-connector-java commons-dbcpcommons-dbcp jstljstl  log4jlog4j com.alibabafastjsonorg.slf4jslf4j-apiorg.slf4jslf4j-log4j12  org.codehaus.jacksonjackson-mapper-asl commons-fileuploadcommons-fileuploadcommons-iocommons-io   commons-codeccommons-codec org.springframework.dataspring-data-redis1.3.6.RELEASE redis.clientsjedis2.5.0${project.artifactId}

 

 

Spring 配置

有了依賴包接下來就要進行整合配置了

Spring-context配置

 

classpath:redis.propertiesclasspath:jdbc.properties  

 

redis.properties檔案

 

# Redis settings  # server IPredis.host=localhost# server portredis.port=6379   # use dbIndexredis.database=0redis.maxIdle=300  redis.maxWait=3000  redis.testOnBorrow=true //#指明是否在從池中取出串連前進行檢驗,如果檢驗失敗,則從池中去除串連並嘗試取出另一個

 

緩衝使用

如何使用緩衝呢,這裡我們根據上篇Spring緩衝註解進行使用,同時也要對SPEL運算式有一點的瞭解

Service操作

 

/** * 商家操作類 */package dui1dui.merchant.core;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 dui1dui.merchant.dao.UserInfoMapper;import dui1dui.merchant.entity.User;/** *  * 商家業務操作類 * @author hanyk * @version 1.0.0 , 下午2:28:15 */@Servicepublic class D1dMerchantServiceImpl implements D1dMerchantService {@Resourceprivate UserInfoMapper userinfoMapper;@Override@Cacheable(value="username")public User selectByPrimaryKey(int userId) {System.out.println("qingqiu");returnuserinfoMapper.selectByPrimaryKey(userId);}@Override@Cacheable("selectall")public List selectAll() {List users = this.userinfoMapper.selectAll();System.out.println("查詢所有");return users;}//清空username 緩衝@CacheEvict(value="username",allEntries=true)public void diselect(){System.out.println("清空username");}//對名稱是hanhan 的進行緩衝@Cacheable(value="name",condition="#name=='hanhan'")public User selectByName(String name) {User user =userinfoMapper.selectByName(name);System.out.println(user);return user;}}

 


controller

@Controllerpublic classScoreManagerController {     @Autowired    private D1dMerchantServiced1dMerchantService;     JacksonJsonUtil jsonutill = newJacksonJsonUtil();     /**     *     根據主鍵查詢     * @return     * @throws NamingException     */    @RequestMapping("/querylist")    public String querylist2(HttpServletRequestrequest,            HttpServletResponse response,Modelmodel) throws NamingException {                   Useruser = d1dMerchantService.selectByPrimaryKey(3);                   model.addAttribute("user",user);        return "/showInfo";     }   //查詢所有內容     @RequestMapping("/selectall")    public String selectall(HttpServletRequestrequest,            HttpServletResponse response)throws NamingException {                   Listusers = d1dMerchantService.selectAll();                   System.out.println(users);                           return "/showInfo";     }   //清空緩衝    @RequestMapping("/diselect")    public String disname() {    d1dMerchantService.diselect();    System.out.println("sdf");;    return"/showInfo";}  //根據使用者名稱稱查詢使用者    @RequestMapping("/getname")    public String getname(){           User user =d1dMerchantService.selectByPrimaryKey(3);           d1dMerchantService.selectByName(user.getUsername());            return"/showInfo";    } }
小結:

該jedis 與spring 整合是通過設定檔以及用戶端實作類別進行緩衝,減少了多餘的實現代碼,同時也省去了緩衝的商務邏輯,基本實現了Aop的效果。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.