Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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辅助计数,使用简单且无侵入性

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