Skip to content

Commit 6338a8f

Browse files
committed
add cache for mybatis
1 parent cde9d3a commit 6338a8f

File tree

10 files changed

+277
-0
lines changed

10 files changed

+277
-0
lines changed

08cache/cache/pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.0.9.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>io.kimmking.08cache</groupId>
12+
<artifactId>cache</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>cache</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-jdbc</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.mybatis.spring.boot</groupId>
36+
<artifactId>mybatis-spring-boot-starter</artifactId>
37+
<version>2.1.4</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>mysql</groupId>
41+
<artifactId>mysql-connector-java</artifactId>
42+
<version>5.1.47</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.projectlombok</groupId>
46+
<artifactId>lombok</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-cache</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>net.sf.ehcache</groupId>
54+
<artifactId>ehcache</artifactId>
55+
<version>2.8.3</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.mybatis</groupId>
59+
<artifactId>mybatis-ehcache</artifactId>
60+
<version>1.0.0</version>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-test</artifactId>
66+
<scope>test</scope>
67+
<exclusions>
68+
<exclusion>
69+
<groupId>org.junit.vintage</groupId>
70+
<artifactId>junit-vintage-engine</artifactId>
71+
</exclusion>
72+
</exclusions>
73+
</dependency>
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-maven-plugin</artifactId>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.kimmking.cache;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication(scanBasePackages = "io.kimmking.cache")
8+
@MapperScan("io.kimmking.cache.mapper")
9+
public class CacheApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(CacheApplication.class, args);
13+
}
14+
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.kimmking.cache.controller;
2+
3+
import io.kimmking.cache.entity.User;
4+
import io.kimmking.cache.service.UserService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import java.util.List;
11+
12+
@RestController
13+
@EnableAutoConfiguration
14+
public class UserController {
15+
16+
@Autowired
17+
UserService userService;
18+
19+
@RequestMapping("/user/find")
20+
User find(Integer id) {
21+
return userService.find(id);
22+
//return new User(1,"KK", 28);
23+
}
24+
25+
@RequestMapping("/user/list")
26+
List<User> list() {
27+
return userService.list();
28+
// return Arrays.asList(new User(1,"KK", 28),
29+
// new User(2,"CC", 18));
30+
}
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.kimmking.cache.entity;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class User {
11+
private Integer id;
12+
private String name;
13+
private Integer age;
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.kimmking.cache.mapper;
2+
3+
import io.kimmking.cache.entity.User;
4+
import org.apache.ibatis.annotations.Mapper;
5+
6+
import java.util.List;
7+
8+
@Mapper
9+
public interface UserMapper {
10+
11+
User find(int id);
12+
13+
List<User> list();
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.kimmking.cache.service;
2+
3+
import io.kimmking.cache.entity.User;
4+
import org.springframework.cache.annotation.CacheConfig;
5+
6+
import java.util.List;
7+
8+
@CacheConfig(cacheNames = "users")
9+
public interface UserService {
10+
11+
User find(int id);
12+
13+
List<User> list();
14+
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.kimmking.cache.service;
2+
3+
import io.kimmking.cache.entity.User;
4+
import io.kimmking.cache.mapper.UserMapper;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cache.annotation.Cacheable;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.List;
10+
11+
@Service
12+
public class UserServiceImpl implements UserService {
13+
14+
@Autowired
15+
UserMapper userMapper;
16+
17+
@Cacheable(key="#id",value="userCache")
18+
public User find(int id) {
19+
return userMapper.find(id);
20+
}
21+
22+
@Cacheable(key="LIST",value="userCache")
23+
public List<User> list(){
24+
return userMapper.list();
25+
}
26+
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
server:
2+
port: 8080
3+
4+
spring:
5+
datasource:
6+
username: root
7+
password:
8+
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
9+
driver-class-name: com.mysql.jdbc.Driver
10+
11+
mybatis:
12+
mapper-locations: classpath:mapper/*Mapper.xml
13+
type-aliases-package: io.kimmking.cache.entity
14+
15+
logging:
16+
level:
17+
io:
18+
kimmking:
19+
cache : info
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
4+
<diskStore path="java.io.tmpdir" />
5+
<defaultCache eternal="false" maxElementsInMemory="1000"
6+
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
7+
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
8+
<cache
9+
name="userCache"
10+
maxElementsInMemory="1000"
11+
eternal="false"
12+
timeToIdleSeconds="300"
13+
timeToLiveSeconds="300"
14+
overflowToDisk="false"
15+
memoryStoreEvictionPolicy="LRU">
16+
<!-- 配置缓存事件监听器 replicateAsynchronously 操作是否异步,默认值为true. replicatePuts 添加操作是否同步到集群内的其他缓存,默认为true.
17+
replicateUpdates 更新操作是否同步到集群内的其他缓存,默认为true. replicateUpdatesViaCopy 更新之后的对象是否复制到集群中的其他缓存(true);
18+
replicateRemovals 删除操作是否同步到集群内的其他缓存,默认为true. -->
19+
<cacheEventListenerFactory
20+
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
21+
properties="
22+
replicateAsynchronously=true,
23+
replicatePuts=true,
24+
replicateUpdates=true,
25+
replicateUpdatesViaCopy=true,
26+
replicateRemovals=true " />
27+
28+
<!-- 初始化缓存,以及自动设置 -->
29+
<bootstrapCacheLoaderFactory
30+
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
31+
properties="bootstrapAsynchronously=true" />
32+
</cache>
33+
34+
</ehcache>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3+
<mapper namespace="io.kimmking.cache.mapper.UserMapper">
4+
5+
<cache type="org.mybatis.caches.ehcache.LoggingEhcache">
6+
<property name="memoryStoreEvictionPolicy" value="LRU"/>
7+
</cache>
8+
9+
<resultMap id="BaseResultMap" type="io.kimmking.cache.entity.User">
10+
<result column="id" jdbcType="INTEGER" property="id" />
11+
<result column="name" jdbcType="VARCHAR" property="name" />
12+
<result column="age" jdbcType="INTEGER" property="age" />
13+
</resultMap>
14+
15+
<select id="find" resultType="io.kimmking.cache.entity.User">
16+
select * from user where id = #{id}
17+
</select>
18+
19+
<select id="list" resultType="io.kimmking.cache.entity.User">
20+
select * from user
21+
</select>
22+
23+
</mapper>

0 commit comments

Comments
 (0)