软件编程
位置:首页>> 软件编程>> java编程>> Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下

Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下

作者:剑客阿良_ALiang  发布时间:2021-07-19 11:33:11 

标签:Java,Fluent,Mybatis,常规操作

前言

接着上一篇:Java Fluent Mybatis 项目工程化与常规操作详解流程篇 上

仓库地址:GitHub仓库

查询

定义查询请求体


package com.hy.fmp.dto.req;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/** @Author huyi @Date 2021/10/20 19:37 @Description: 查询条件 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TestFluentMybatisQueryReq {
 private String age;
 private String name;
}

查询写法1

查询接口方法定义


 /**
  * 查询接口1
  *
  * @param queryReq 查询请求
  * @return 列表
  */
 List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq);

方法实现,这里我们改用了mapper来实现一下官方给出的查询语法模式。


package com.hy.fmp.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.hy.fmp.dto.req.TestFluentMybatisQueryReq;
import com.hy.fmp.fluent.dao.intf.TestFluentMybatisDao;
import com.hy.fmp.fluent.entity.TestFluentMybatisEntity;
import com.hy.fmp.fluent.helper.TestFluentMybatisMapping;
import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper;
import com.hy.fmp.fluent.wrapper.TestFluentMybatisQuery;
import com.hy.fmp.service.IBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/** @Author huyi @Date 2021/10/20 17:10 @Description: 基础操作接口实现 */
@Slf4j
@Service
public class BaseServiceImpl implements IBaseService {

@Autowired private TestFluentMybatisDao testFluentMybatisDao;
 @Autowired private TestFluentMybatisMapper testFluentMybatisMapper;

@Override
 public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) {
   testFluentMybatisDao.saveOrUpdate(param);
   return param;
 }

@Override
 public List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq) {
   return testFluentMybatisMapper.listEntity(
       new TestFluentMybatisQuery()
           .selectAll()
           .where
           .age()
           .eq(queryReq.getAge())
           .and
           .name()
           .eq(queryReq.getName())
           .end());
 }

}

control层方法定义


 @ApiOperation(value = "查询数据1", notes = "查询数据1")
 @RequestMapping(value = "/query1", method = RequestMethod.POST)
 @ResponseBody
 public Result<List<TestFluentMybatisEntity>> query1(
     @RequestBody TestFluentMybatisQueryReq queryReq) {
   try {
     return Result.ok(baseService.query1(queryReq));
   } catch (Exception exception) {
     return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
   }
 }

调试一下接口

Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下

一眼望去貌似没问题,但是长期后端开发的朋友应该能看出来,这个实现方式如果一旦age或者name参数为空的话,那么肯定查不出结果。因为按照语句的写法,会强制比较age和name两个参数。

我们将其中一个参数设置为空字符串试试看。

Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下

不出意料。现在我可以就该方法做调整,参数判断然后替换select语句,为了更优雅的实现,我去官方文档再找找。

查询写法2

查询方法2


package com.hy.fmp.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.hy.fmp.dto.req.TestFluentMybatisQueryReq;
import com.hy.fmp.fluent.dao.intf.TestFluentMybatisDao;
import com.hy.fmp.fluent.entity.TestFluentMybatisEntity;
import com.hy.fmp.fluent.helper.TestFluentMybatisMapping;
import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper;
import com.hy.fmp.fluent.wrapper.TestFluentMybatisQuery;
import com.hy.fmp.service.IBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/** @Author huyi @Date 2021/10/20 17:10 @Description: 基础操作接口实现 */
@Slf4j
@Service
public class BaseServiceImpl implements IBaseService {

@Autowired private TestFluentMybatisDao testFluentMybatisDao;
 @Autowired private TestFluentMybatisMapper testFluentMybatisMapper;

@Override
 public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) {
   testFluentMybatisDao.saveOrUpdate(param);
   return param;
 }

@Override
 public List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq) {
   return testFluentMybatisMapper.listEntity(
       new TestFluentMybatisQuery()
           .selectAll()
           .where
           .age()
           .eq(queryReq.getAge())
           .and
           .name()
           .eq(queryReq.getName())
           .end());
 }

@Override
 public List<TestFluentMybatisEntity> query2(TestFluentMybatisQueryReq queryReq) {
   return testFluentMybatisMapper.listByMap(
       true,
       new HashMap<String, Object>() {
         {
           if (!StrUtil.hasEmpty(queryReq.getAge())) {
             this.put(TestFluentMybatisMapping.age.column, queryReq.getAge());
           }
           if (!StrUtil.hasEmpty(queryReq.getName())) {
             this.put(TestFluentMybatisMapping.name.column, queryReq.getName());
           }
         }
       });
 }
}

代码说明

我对比了一下官方文档的写法,发现我这个版本的fm该listByMap方法多一个isColumn的布尔型参数。所以我追了一下源码。

Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下

只影响错误打印,主要就是设置的map参数必须要是列名或者实体对象内的参数名。就不管了。

验证一下

Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下

没什么问题,还是可以查出来。

新问题

但是按照这个查询方法,如果两个值都传空字符串会查出全表数据吗?

验证一下

Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下

咳咳,报错了。

所以我还是老老实实先把代码参数判空优化一下。


package com.hy.fmp.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.hy.fmp.dto.req.TestFluentMybatisQueryReq;
import com.hy.fmp.fluent.dao.intf.TestFluentMybatisDao;
import com.hy.fmp.fluent.entity.TestFluentMybatisEntity;
import com.hy.fmp.fluent.helper.TestFluentMybatisMapping;
import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper;
import com.hy.fmp.fluent.wrapper.TestFluentMybatisQuery;
import com.hy.fmp.service.IBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/** @Author huyi @Date 2021/10/20 17:10 @Description: 基础操作接口实现 */
@Slf4j
@Service
public class BaseServiceImpl implements IBaseService {

@Autowired private TestFluentMybatisDao testFluentMybatisDao;
 @Autowired private TestFluentMybatisMapper testFluentMybatisMapper;

@Override
 public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) {
   testFluentMybatisDao.saveOrUpdate(param);
   return param;
 }

@Override
 public List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq) {
   return testFluentMybatisMapper.listEntity(
       new TestFluentMybatisQuery()
           .selectAll()
           .where
           .age()
           .eq(queryReq.getAge())
           .and
           .name()
           .eq(queryReq.getName())
           .end());
 }

@Override
 public List<TestFluentMybatisEntity> query2(TestFluentMybatisQueryReq queryReq) {
   if (StrUtil.hasEmpty(queryReq.getAge()) && StrUtil.hasEmpty(queryReq.getName())) {
     return testFluentMybatisMapper.listEntity(new TestFluentMybatisQuery().selectAll());
   }
   return testFluentMybatisMapper.listByMap(
       true,
       new HashMap<String, Object>() {
         {
           if (!StrUtil.hasEmpty(queryReq.getAge())) {
             this.put(TestFluentMybatisMapping.age.column, queryReq.getAge());
           }
           if (!StrUtil.hasEmpty(queryReq.getName())) {
             this.put(TestFluentMybatisMapping.name.column, queryReq.getName());
           }
         }
       });
 }
}

验证一下

Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下

添加通过ID删除数据的接口方法


 /**
  * 删除接口
  *
  * @param id id
  */
 void deleteById(Integer id);

实现接口方法


 @Override
 public void deleteById(Integer id) {
   testFluentMybatisMapper.deleteById(id);
 }

验证一下

Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下

Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下

删除成功

来源:https://huyi-aliang.blog.csdn.net/article/details/120875177

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com