Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/scienjus/mybatis-redis-counter
use redis to counting in mybatis, non invasive and easy to use / 在MyBatis项目中使用Redis辅助计数,使用简单且无侵入性
https://github.com/scienjus/mybatis-redis-counter
Last synced: 24 days ago
JSON representation
use redis to counting in mybatis, non invasive and easy to use / 在MyBatis项目中使用Redis辅助计数,使用简单且无侵入性
- Host: GitHub
- URL: https://github.com/scienjus/mybatis-redis-counter
- Owner: ScienJus
- Created: 2016-02-06T15:50:15.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-15T08:43:02.000Z (almost 9 years ago)
- Last Synced: 2023-02-28T19:56:38.909Z (almost 2 years ago)
- Language: Java
- Homepage:
- Size: 37.1 KB
- Stars: 3
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MyBatis Redis Counter
use redis to counting in mybatis, non invasive and easy to use
[中文文档][1]
### Quick Start
Add the Maven dependency:
repository:
```
scienjus-mvn-repo
https://raw.github.com/ScienJus/maven/snapshot/
true
always
```
dependency:
```
com.scienjus
mybatis-redis-counter
1.0-SNAPSHOT```
Add `QueryCounterInterceptor` to mybatis plugins:
```
JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);QueryCounterInterceptor queryCounterInterceptor = new QueryCounterInterceptor();
queryCounterInterceptor.setJedisPool(jedisPool);
//... env config
Configuration configuration = new Configuration(environment);
configuration.addInterceptor(queryCounterInterceptor);
//... other config
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(configuration);
```Or use `spring-mybatis`:
```
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource());
sqlSessionFactory.setPlugins(new Interceptor[]{queryCounterInterceptor()});
//... other config
return sqlSessionFactory.getObject();
}@Bean
public QueryCounterInterceptor queryCounterInterceptor() {
QueryCounterInterceptor queryCounterInterceptor = new QueryCounterInterceptor();
queryCounterInterceptor.setJedisPool(jedisPool());
return queryCounterInterceptor;
}@Bean
public JedisPool jedisPool() {
return new JedisPool("127.0.0.1", 6379);
}
```Then just need to add some annotations and methods in your model:
```
@Counter
public class User {//primary key
@Id
private Integer id;private String name;
// means this is a counter field
@Field
private int followerCount;
public int setFollowerCount(int followerCount) {
this.followerCount = followerCount;
return this.followerCount;
}
public int getFollowerCount() {
return this.followerCount
}//increment
public int incrFollowerCount() {
return 0;
}//decrement
public int decrFollowerCount() {
return 0;
}}
```It should be working like this
```
User user = userMapper.get(USER_ID);
user.setFollowerCount(10);
int retVal = user.incrFollowerCount();user = userMapper.get(USER_ID); //another user instance with same id
Assert.assertEquals(retVal, 11);
Assert.assertEquals(user.getFollowerCount(), 11);
```[1]: ./zh.md