Skip to content

Commit e1476c6

Browse files
committed
springCache is ok
1 parent ddca076 commit e1476c6

9 files changed

Lines changed: 372 additions & 0 deletions

File tree

springboot-Cache/pom.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.us</groupId>
8+
<artifactId>springboot-Cache</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.3.0.RELEASE</version>
14+
</parent>
15+
16+
<properties>
17+
<start-class>com.us.Application</start-class>
18+
19+
<maven.compiler.target>1.8</maven.compiler.target>
20+
<maven.compiler.source>1.8</maven.compiler.source>
21+
</properties>
22+
23+
<!-- Add typical dependencies for a web application -->
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-cache</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>net.sf.ehcache</groupId>
40+
<artifactId>ehcache</artifactId>
41+
</dependency>
42+
43+
<!--切换缓存-->
44+
<!--<dependency>-->
45+
<!--<groupId>com.google.guava</groupId>-->
46+
<!--<artifactId>guava</artifactId>-->
47+
<!--<version>18.0</version>-->
48+
<!--</dependency>-->
49+
50+
<!--<dependency>-->
51+
<!--<groupId>org.springframework.boot</groupId>-->
52+
<!--<artifactId>spring-boot-starter-redis</artifactId>-->
53+
<!--</dependency>-->
54+
55+
<!--db-->
56+
57+
<dependency>
58+
<groupId>mysql</groupId>
59+
<artifactId>mysql-connector-java</artifactId>
60+
<version>6.0.5</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>com.mchange</groupId>
64+
<artifactId>c3p0</artifactId>
65+
<version>0.9.5.2</version>
66+
<exclusions>
67+
<exclusion>
68+
<groupId>commons-logging</groupId>
69+
<artifactId>commons-logging</artifactId>
70+
</exclusion>
71+
</exclusions>
72+
</dependency>
73+
74+
</dependencies>
75+
76+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.us.example;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.cache.annotation.EnableCaching;
5+
import org.springframework.context.ConfigurableApplicationContext;
6+
import org.springframework.context.annotation.ComponentScan;
7+
8+
import static org.springframework.boot.SpringApplication.*;
9+
10+
/**
11+
* Created by yangyibo on 17/1/13.
12+
*/
13+
14+
@ComponentScan(basePackages ="com.us.example")
15+
@SpringBootApplication
16+
@EnableCaching
17+
public class Application {
18+
public static void main(String[] args) {
19+
ConfigurableApplicationContext run = run(Application.class, args);
20+
}
21+
22+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.us.example.bean;
2+
import javax.persistence.Entity;
3+
import javax.persistence.GeneratedValue;
4+
import javax.persistence.Id;
5+
import javax.persistence.Table;
6+
7+
/**
8+
* Created by yangyibo on 17/1/13.
9+
*/
10+
@Entity
11+
@Table(name = "Person")
12+
public class Person {
13+
@Id
14+
@GeneratedValue
15+
private Long id;
16+
17+
private String name;
18+
19+
private Integer age;
20+
21+
private String address;
22+
23+
public Person() {
24+
super();
25+
}
26+
public Person(Long id, String name, Integer age, String address) {
27+
super();
28+
this.id = id;
29+
this.name = name;
30+
this.age = age;
31+
this.address = address;
32+
}
33+
public Long getId() {
34+
return id;
35+
}
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
public String getName() {
40+
return name;
41+
}
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
public Integer getAge() {
46+
return age;
47+
}
48+
public void setAge(Integer age) {
49+
this.age = age;
50+
}
51+
public String getAddress() {
52+
return address;
53+
}
54+
public void setAddress(String address) {
55+
this.address = address;
56+
}
57+
58+
59+
}
60+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.us.example.config;
2+
3+
/**
4+
* Created by yangyibo on 17/1/13.
5+
*/
6+
import java.beans.PropertyVetoException;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.core.env.Environment;
12+
import com.mchange.v2.c3p0.ComboPooledDataSource;
13+
14+
@Configuration
15+
public class DBConfig {
16+
@Autowired
17+
private Environment env;
18+
19+
@Bean(name="dataSource")
20+
public ComboPooledDataSource dataSource() throws PropertyVetoException {
21+
ComboPooledDataSource dataSource = new ComboPooledDataSource();
22+
dataSource.setDriverClass(env.getProperty("ms.db.driverClassName"));
23+
dataSource.setJdbcUrl(env.getProperty("ms.db.url"));
24+
dataSource.setUser(env.getProperty("ms.db.username"));
25+
dataSource.setPassword(env.getProperty("ms.db.password"));
26+
dataSource.setMaxPoolSize(20);
27+
dataSource.setMinPoolSize(5);
28+
dataSource.setInitialPoolSize(10);
29+
dataSource.setMaxIdleTime(300);
30+
dataSource.setAcquireIncrement(5);
31+
dataSource.setIdleConnectionTestPeriod(60);
32+
33+
return dataSource;
34+
}
35+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.us.example.config;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import javax.persistence.EntityManagerFactory;
7+
import javax.sql.DataSource;
8+
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.ComponentScan;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
14+
import org.springframework.orm.jpa.JpaTransactionManager;
15+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
16+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
17+
import org.springframework.transaction.PlatformTransactionManager;
18+
import org.springframework.transaction.annotation.EnableTransactionManagement;
19+
20+
/**
21+
* Created by yangyibo on 17/1/13.
22+
*/
23+
24+
@Configuration
25+
@EnableJpaRepositories("com.us.example.dao")
26+
@EnableTransactionManagement
27+
@ComponentScan
28+
public class JpaConfig {
29+
@Autowired
30+
private DataSource dataSource;
31+
32+
@Bean
33+
public EntityManagerFactory entityManagerFactory() {
34+
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
35+
//vendorAdapter.setShowSql(true);
36+
//vendorAdapter.setGenerateDdl(true);
37+
38+
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
39+
factory.setJpaVendorAdapter(vendorAdapter);
40+
factory.setPackagesToScan("com.us.example.bean");
41+
factory.setDataSource(dataSource);
42+
43+
44+
Map<String, Object> jpaProperties = new HashMap<>();
45+
jpaProperties.put("hibernate.ejb.naming_strategy","org.hibernate.cfg.ImprovedNamingStrategy");
46+
jpaProperties.put("hibernate.jdbc.batch_size",50);
47+
//jpaProperties.put("hibernate.show_sql",true);
48+
49+
factory.setJpaPropertyMap(jpaProperties);
50+
factory.afterPropertiesSet();
51+
return factory.getObject();
52+
}
53+
54+
@Bean
55+
public PlatformTransactionManager transactionManager() {
56+
57+
JpaTransactionManager txManager = new JpaTransactionManager();
58+
txManager.setEntityManagerFactory(entityManagerFactory());
59+
return txManager;
60+
}
61+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.us.example.controller;
2+
import com.us.example.bean.Person;
3+
import com.us.example.service.DemoService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.ResponseBody;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
/**
11+
* Created by yangyibo on 17/1/13.
12+
*/
13+
@RestController
14+
public class CacheController {
15+
16+
@Autowired
17+
private DemoService demoService;
18+
19+
//http://localhost:8080/put?name=abel&age=23&address=shanghai
20+
@RequestMapping("/put")
21+
public Person put(Person person){
22+
return demoService.save(person);
23+
24+
}
25+
26+
//http://localhost:8080/able?id=1
27+
@RequestMapping("/able")
28+
@ResponseBody
29+
public Person cacheable(Person person){
30+
31+
32+
return demoService.findOne(person);
33+
34+
}
35+
36+
//http://localhost:8080/evit?id=1
37+
@RequestMapping("/evit")
38+
public String evit(Long id){
39+
demoService.remove(id);
40+
return "ok";
41+
42+
}
43+
44+
45+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.us.example.dao;
2+
3+
import com.us.example.bean.Person;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
/**
7+
* Created by yangyibo on 17/1/13.
8+
*/
9+
public interface PersonRepository extends JpaRepository<Person, Long> {
10+
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.us.example.service;
2+
3+
import com.us.example.bean.Person;
4+
5+
/**
6+
* Created by yangyibo on 17/1/13.
7+
*/
8+
public interface DemoService {
9+
public Person save(Person person);
10+
11+
public void remove(Long id);
12+
13+
public Person findOne(Person person);
14+
15+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.us.example.service.Impl;
2+
3+
import com.us.example.bean.Person;
4+
import com.us.example.dao.PersonRepository;
5+
import com.us.example.service.DemoService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.cache.annotation.CacheEvict;
8+
import org.springframework.cache.annotation.CachePut;
9+
import org.springframework.cache.annotation.Cacheable;
10+
import org.springframework.stereotype.Service;
11+
12+
/**
13+
* Created by yangyibo on 17/1/13.
14+
*/
15+
@Service
16+
public class DemoServiceImpl implements DemoService {
17+
18+
@Autowired
19+
private PersonRepository personRepository;
20+
21+
@Override
22+
//@CachePut缓存新增的或更新的数据到缓存,其中缓存名字是 people 。数据的key是person的id
23+
@CachePut(value = "people", key = "#person.id")
24+
public Person save(Person person) {
25+
Person p = personRepository.save(person);
26+
System.out.println("为id、key为:"+p.getId()+"数据做了缓存");
27+
return p;
28+
}
29+
30+
@Override
31+
//@CacheEvict 从缓存people中删除key为id 的数据
32+
@CacheEvict(value = "people")
33+
public void remove(Long id) {
34+
System.out.println("删除了id、key为"+id+"的数据缓存");
35+
//这里不做实际删除操作
36+
}
37+
38+
@Override
39+
//@Cacheable缓存key为person 的id 数据到缓存people 中,如果没有指定key则方法参数作为key保存到缓存中。
40+
@Cacheable(value = "people", key = "#person.id")
41+
public Person findOne(Person person) {
42+
Person p = personRepository.findOne(person.getId());
43+
System.out.println("为id、key为:"+p.getId()+"数据做了缓存");
44+
return p;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)