When developers need to perform association queries, they often need to write a lot of redundant codes to deal with the relationship between data. This not only wastes time and effort, but also affects the maintainability and scalability of the project.
EasyRelation is a simple and efficient auto-correlation data framework, which can automatically correlate query and fill in the required data with one line of code, which has minimal impact on performance and omits a lot of redundant code.
The framework adapts toThe fields in the current object need to be associated with the query and assigned to the fields of the current object. The data source can be any source such as enumeration, database, RPC interface, etc..
features
- There is no limit to the associated query method, and the data that needs to be associated can be from any source
- Two-level cache support, free to choose the cache to use
- High execution efficiency with minimal impact on performance
- Support multi-condition association and constant condition association
quick start
The following demonstrates how to use EasyRelation to automatically associate data
Suppose there is an order class (Order
) and user classes (User
), the user name is saved in the order, and the user nickname needs to be associated with the query.
@Data
public class Order {
private String orderId;
private String username;
private String nickName;
}
@Data
public class User {
private String username;
private String nickName;
}
add dependencies
<properties>
<easy-relation.version>最新版本</easy-relation.version>
</properties>
<dependencies>
<dependency>
<groupId>cn.easii</groupId>
<artifactId>easy-relation-spring-boot-starter</artifactId>
<version>${easy-relation.version}</version>
</dependency>
</dependencies>
Define user data data provider
Here you need to define a class that implements DataProvideService
Interface, which defines the interface for obtaining user information, and adds @DataProvider
annotation.
@Component
public class UserInfoDataProvider implements DataProvideService {
@DataProvider(RelationIdentifiers.getUserByUsername)
public User getUserByUsername(UserQueryReq req) {
if ("admin".equals(req.getUsername())) {
final User user = new User();
user.setUsername("admin");
user.setNickName("管理员");
return user;
}
return null;
}
}
here UserQueryReq
Enter parameters for user information query, defined as follows:
@Data
@AutoMapMapper
public class UserQueryReq {
private String username;
private Long userId;
private Boolean isDeleted;
}
test
@SpringBootTest
class InjectRelationTest {
@Autowired
private InjectRelation injectRelation;
@Test
void quickStart() {
Order order = getOrder("2f453910375641648ab3a2fc6e3328ef");
injectRelation.injectRelation(order);
System.out.println(order); // Order(orderId=2f453910375641648ab3a2fc6e3328ef, username=admin, nickName=管理员)
Assert.equals(order.getNickName(), "管理员");
}
private Order getOrder(String orderId) {
Order order = new Order();
order.setOrderId(orderId);
order.setUsername("admin");
return order;
}
}
cache support
EasyRelation has a two-level cache design, referring to the design in Mybatis. When performing data association, it will go through 一级缓存 --> 二级缓存 ---> 数据提供源
so as to improve the efficiency of data acquisition.
Here is a brief look at the first-level cache and the second-level cache:
- L1 cache: The cache within a single data association operation. The cached data is only valid during this association process. The first-level cache will be automatically enabled according to certain rules.
A single data association operation here refers to calling once
injectRelation
The flow of execution within a method.
- L2 cache: Global cache, such as using Redis as a cache, the second-level cache needs to be manually enabled.
For details, please refer to caching | EasyRelation (easyi.cn)
performance loss
Test run environment:
- CPU: Intel i5 10400
- Memory: 32GB
- JDK: 17
Execute one million times. In many cases, the consumption is between 185 and 620 milliseconds. For details, please check the performance | EasyRelation (easii.cn)
project address
official document
Home| EasyRelation (easii.cn)
#EasyRelation #released #simple #powerful #data #association #framework #News Fast Delivery