Spring Boot+Spring Data JPA+Spring Cache

本文使用spring boot 快速构建项目工程,spring data jpa 控制数据访问服务层,spring cache 缓存查询数据

1.pom.xml 配置信息及application.properties



	4.0.0

	org.springframework
	com-my-data-cache
	0.1.0

	
		org.springframework.boot
		spring-boot-starter-parent
		1.3.1.RELEASE
	

	
		1.8
	

	
	
		
			org.springframework.boot
			spring-boot-starter
		
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
			
			org.springframework.boot
			spring-boot-starter-aop
		
		  
            org.springframework.boot  
            spring-boot-starter-thymeleaf  
          
		
			
			org.springframework.boot
			spring-boot-starter-jetty
		
		
			org.springframework.boot
			spring-boot-starter-tomcat
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework
			spring-core
		
		
		
			org.springframework.boot
			spring-boot-starter-cache
		
		
	
		
			org.springframework.data
			spring-data-commons
		
		
			
			org.springframework.boot
			spring-boot-starter-log4j
		
		
		
		
			org.springframework.boot
			spring-boot-starter-jdbc
		
			
			mysql
			mysql-connector-java
		
			
				org.hibernate
				hibernate-entitymanager
			
			
	javax.servlet
	javax.servlet-api


	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


# primary datasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost/mydat
spring.datasource.username=root
spring.datasource.password= wb12345



2.新建实体类对象 BOOK

package com.my.data.cache.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="book")
public class Book implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6283522837937163003L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = true)
	private Integer id;
    private String isbn;
    private String title;

    public Book(String isbn, String title) {
        this.isbn = isbn;
        this.title = title;
    }
    public Book() {
	}
	public Book(int id, String isbn, String title) {
		super();
		this.id = id;
		this.isbn = isbn;
		this.title = title;
	}


	public int getId() {
		return id;
	}




	public void setId(int id) {
		this.id = id;
	}




	public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "Book{" + "isbn='" + isbn + '\'' + ", title='" + title + '\'' + '}';
    }
}
3.创建DAO接口,该接口采用Spring Data JPA 持久层技术,可以采用命名查询,注解查询,命名参数查询等查询方式,
package com.my.data.cache.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.my.data.cache.domain.Book;
/**
 * 接口
 * @author wbw
 *
 */
public interface BookRepository extends JpaRepository {

  @Query("select b from Book b  where 1=1")
  public   List findBookAll();
  /**
   * 根据isbn查询
   * @param name
   * @return
   */
  @Query("select  b from Book b where b.id =?1")
  public  Book findById(Integer bid);
   /**
    * 统计size  
    * @return
    */
  @Query("select count(*) from Book where 1=1 ")
  public int countBook();
  /**
   * 根据命名规范查询 findBy+属性
   * @param isbn
   * @return
   */
  public Book findByIsbn(String isbn);

}

4.创建service业务逻辑处理层接口及实现接口

package com.my.data.cache.service;

import java.util.List;

import com.my.data.cache.domain.Book;

public interface BookService {

	public Book findById(Integer bid);

	public List findBookAll();

	public void insertBook(Book book);

	public Book findByTitle(String title);
	
	public int countBook();
	
	public void modifyBook(Book book);
	
	 public Book findByIsbn(String isbn);
	
	
}

package com.my.data.cache.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Transactional;

import com.my.data.cache.annotation.LogAnnotation;
import com.my.data.cache.domain.Book;
import com.my.data.cache.exception.MyException;
import com.my.data.cache.repository.BookRepository;
import com.my.data.cache.service.BookService;

@Service
@Transactional
public class BookServiceImpl  implements BookService{
	private static final Logger log = LoggerFactory.getLogger(BookServiceImpl.class);
	@Autowired
	private BookRepository bookRepository;
	
	/*@PersistenceContext
	private EntityManager em;*/
	
	//将缓存保存进andCache,并使用参数中的bid加上一个字符串(这里使用方法名称)作为缓存的key
	@Cacheable(value="andCache",key="#bid+'findById'")
	//@LogAnnotation(value="通过Id查询Book")
	public Book findById(Integer bid) {
		this.simulateSlowService();
		return bookRepository.findById(bid);
	}
	
	@Override
	public List findBookAll() {
		return bookRepository.findBookAll();
	}
    
	//将缓存保存进andCache,并当参数title的长度小于32时才保存进缓存,默认使用参数值及类型作为缓存的key 
	@Cacheable(value="andCache",condition="#title.length >5") 
	public Book findByTitle(String title){
		return null;
		
	}
	/**
	 * 新增
	 * @param book
	 * @return
	 */
	public void insertBook(Book book){
		bookRepository.save(book);
	}
	
	@Override
	public int countBook() {
		return bookRepository.countBook();
	}  
	//清除掉指定key中的缓存
	@CacheEvict(value="andCache",key="#book.id + 'findById'")
	public void modifyBook(Book book) {
		log.info("清除指定缓存"+book.getId()+"findById");
		bookRepository.save(book);
	}
	
	//清除掉全部缓存  
	@CacheEvict(value="andCache",allEntries=true,beforeInvocation=true)  
	public void ReservedBook() {  
		log.info("清除全部的缓存");  
	}

	// Don't do this at home
    private void simulateSlowService() {
        try {
            long time = 5000L;
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new MyException("程序出错", e);
        }
    }

	@Override
	public Book findByIsbn(String isbn) {
		return bookRepository.findByIsbn(isbn);
	}
}

5.创建controller控制器

package com.my.data.cache.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.my.data.cache.domain.Book;
import com.my.data.cache.service.BookService;

@RestController 
@RequestMapping("/user")  
public class BookController {
	private static final Logger log = LoggerFactory.getLogger(BookController.class);
    
	@Autowired
    private BookService bookService;
	
    
    @RequestMapping("/{id}")
	public @ResponseBody Book index(@PathVariable("id") Integer id){
		Book b = bookService.findById(id);
	    log.info(b.getIsbn()+"------>"+b.getTitle());
  		return b;
	}
    
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public  @ResponseBody List list(){
    	List b = bookService.findBookAll();
		return  b;
    	
    }
    @RequestMapping(value = "/add")
    public String  insertBook(){
    	Book b = new Book();
    	b.setId(4);
    	b.setIsbn("1111");
    	b.setTitle("相信自己");
    	bookService.insertBook(b);
    	return "success";
    	
    }    
    /**
     * 更新
     * @return
     */
    @RequestMapping(value = "/update")
    public String update(){
    	Book b = new Book();
    	b.setId(1);
    	b.setIsbn("1");
    	b.setTitle("爱的力量");
    	bookService.modifyBook(b);
    	return "success";
    }
}

6.测试

package com.my.data.cache;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

import com.my.data.cache.domain.Book;
import com.my.data.cache.service.BookService;


/**
 * 
 * 启动器
 *
 */
@SpringBootApplication
@EnableCaching//扫描cahce注解
public class Application1  implements CommandLineRunner{
	
	@Autowired
    private BookService bookService;
	@Override
	public void run(String... args) throws Exception {
		Book b1 = bookService.findByIsbn("1");
		Book b2 = bookService.findByIsbn("2");
		Book b3 = bookService.findById(3);
	    System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
		
	}
	public static void main(String[] args) {
		 SpringApplication.run(Application1.class,args);
	}

}

Spring Cache

3.1开始,Spring引入了对Cache的支持。其使用方法和原理都类似于Spring对事务管理的支持。Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存放在缓存中,等到下次利用同样的参数来调用该方法时将不再执行该方法,而是直接从缓存中获取结果进行返回。所以在使用Spring Cache的时候我们要保证我们缓存的方法对于相同的方法参数要有相同的返回结果。

 使用Spring Cache需要我们做两方面的事:

n  声明某些方法使用缓存

n  配置Spring对Cache的支持

和Spring对事务管理的支持一样,Spring对Cache的支持也有基于注解和基于XML配置两种方式

想了解更多相关的信息链接该地址:
https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/



你可能感兴趣的:(Spring,Boot)