QueryWrapper<VendorInfo> queryWrapper = new QueryWrapper<>(); if(storCode != null && !"".equals(storCode)){ queryWrapper.eq("MAT_CODE", matCode).eq("VENDOR_DESC", vendorDesc).eq("STORE_CODE",storCode); }else { queryWrapper.eq("MAT_CODE", matCode).eq("VENDOR_DESC", vendorDesc); }这个代码优化一下
时间: 2024-04-21 08:22:48 浏览: 17
可以使用链式调用来优化这段代码,可以将if-else语句简化为一行代码。以下是优化后的代码示例:
```java
QueryWrapper<VendorInfo> queryWrapper = new QueryWrapper<>()
.eq("MAT_CODE", matCode)
.eq("VENDOR_DESC", vendorDesc);
if (storCode != null && !"".equals(storCode)) {
queryWrapper.eq("STORE_CODE", storCode);
}
```
通过使用链式调用,我们可以在创建QueryWrapper实例后,直接连续调用eq方法来设置查询条件。这样可以避免重复的代码,并使代码更加简洁和易读。在if-else语句中,我们只需要在storCode不为空且不等于空字符串时,再调用eq方法来添加额外的查询条件。
相关问题
public QueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate)怎么使用
QueryWrapper是MyBatis-Plus中的一个类,用于构建查询条件。select方法用于指定查询的字段,参数entityClass是实体类的Class对象,predicate是一个函数式接口,用于判断哪些字段需要查询,返回值是一个布尔值。
使用示例:
```java
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select(User.class, tableFieldInfo ->
!"password".equals(tableFieldInfo.getColumn()));
List<User> userList = userMapper.selectList(queryWrapper);
```
以上代码中,创建了一个QueryWrapper对象,并指定了要查询的实体类为User。predicate参数使用Lambda表达式实现,判断如果字段名不是"password"则需要查询。最后调用selectList方法执行查询。
优化代码 if (user.getTenantId().equals(project.getTenantId())) { List<DepositRecord> recordList = depositRecordService.list(new QueryWrapper<DepositRecord>() .eq("assets_id", assetsId) .eq("user_id", userId)); // 判断是一次性付款还是分期付款 recordList.forEach(e -> { if (recordList.size() == 1) { if (project.getTheTone() != null && e.getAmount() == project.getTheTone()) { context.put("tradeMethod", "一次性付款"); } if (project.getTheTone() == null && e.getAmount() == dealRecord.getDealPrice()) { context.put("tradeMethod", "一次性付款"); } context.put("tradeMethod", "分期付款"); } else { context.put("tradeMethod", "分期付款"); } }); } else if (user.getTenantId().equals(project.getAgencyTenantId())) { Enroll enrollServiceOne = enrollService.getOne(new QueryWrapper<Enroll>().eq("id", dealRecord.getEnrollId())); List<DepositRecord> recordList = depositRecordService.list(new QueryWrapper<DepositRecord>() .eq("assets_id", assetsId) .eq("user_id", enrollServiceOne.getUserId())); // 判断是一次性付款还是分期付款 recordList.forEach(e -> { if (recordList.size() == 1) { if (project.getTheTone() != null && e.getAmount() == project.getTheTone()) { context.put("tradeMethod", "一次性付款"); } if (project.getTheTone() == null && e.getAmount() == dealRecord.getDealPrice()) { context.put("tradeMethod", "一次性付款"); } context.put("tradeMethod", "分期付款");
可以对代码进行如下优化:
1. 提取出重复的逻辑,避免代码冗余。将获取 DepositRecord 列表以及判断付款方式的逻辑抽取出来,放到一个方法中。这个方法可以接收一个 DepositRecord 列表和 project 对象作为参数,返回一个字符串类型的付款方式。
2. 简化 if/else 的嵌套结构。可以使用 return 语句避免过深的嵌套,提高代码可读性。
优化后的代码如下:
```
String tradeMethod = getTradeMethod(user, project, dealRecord, assetsId);
context.put("tradeMethod", tradeMethod);
// ...
private String getTradeMethod(User user, Project project, DealRecord dealRecord, Long assetsId) {
List<DepositRecord> recordList = depositRecordService.list(new QueryWrapper<DepositRecord>()
.eq("assets_id", assetsId)
.eq("user_id", user.getTenantId().equals(project.getTenantId()) ? userId : enrollService.getOne(new QueryWrapper<Enroll>().eq("id", dealRecord.getEnrollId())).getUserId()));
if (recordList.size() == 1) {
if (project.getTheTone() != null && recordList.get(0).getAmount() == project.getTheTone()) {
return "一次性付款";
}
if (project.getTheTone() == null && recordList.get(0).getAmount() == dealRecord.getDealPrice()) {
return "一次性付款";
}
}
return "分期付款";
}
```
这样,代码就更加清晰简洁了。