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);
}
}
调试一下接口
一眼望去貌似没问题,但是长期后端开发的朋友应该能看出来,这个实现方式如果一旦age或者name参数为空的话,那么肯定查不出结果。因为按照语句的写法,会强制比较age和name两个参数。
我们将其中一个参数设置为空字符串试试看。
不出意料。现在我可以就该方法做调整,参数判断然后替换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的布尔型参数。所以我追了一下源码。
只影响错误打印,主要就是设置的map参数必须要是列名或者实体对象内的参数名。就不管了。
验证一下
没什么问题,还是可以查出来。
新问题
但是按照这个查询方法,如果两个值都传空字符串会查出全表数据吗?
验证一下
咳咳,报错了。
所以我还是老老实实先把代码参数判空优化一下。
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());
}
}
});
}
}
验证一下
删
添加通过ID删除数据的接口方法
/**
* 删除接口
*
* @param id id
*/
void deleteById(Integer id);
实现接口方法
@Override
public void deleteById(Integer id) {
testFluentMybatisMapper.deleteById(id);
}
验证一下
删除成功
来源:https://huyi-aliang.blog.csdn.net/article/details/120875177


猜你喜欢
- JAVA基础八股文Switch能支持哪些类型?jdk5之前,switch能够作用在byte,short,char,int(实际上都是提升为i
- 前言当我们写了一个方法,那么这个方法是如何被执行的呢?public int add(){ int a = 10;
- java语言是一种面向对象的程序设计语言吗java语言是面向对象的程序设计语言支持部分或绝大部分面向对象特性(类和实例、封装性、继承、多态)
- 前言:mongodb是一个基于分布式文件存储的开源数据库系统。mongodb与我们平常使用的mysql的区别: 1.
- 前言:Guarded Suspension意为保护暂停,其核心思想是仅当服务进程准备好时,才提供服务。设想一种场景,服务器可能会在很短时间内
- 什么是过滤器过滤器 Filter 基于 Servlet 实现,过滤器的主要应用场景是对字符编码、跨域等问题进行过滤。Servlet 的工作原
- 首先说一下,教科书上的扫描线算法确实是用c++很好实现,而且网上有很多源码,而java实现的基本没有(可能是我没看到),所以肖先生还是打算自
- 发现坑最近在配置项目主题的时候报了如下错误:This Activity already has an action bar supplied
- 本文实例展示了WinForm项目开发中NPOI用法,对于C#初学者有一定的借鉴价值。具体实例如下:private void ExportMe
- 如下所示:public static boolean isSdcardExists(Context context) {StorageMan
- Flayway是一款数据库版本控制管理工具,,支持数据库版本自动升级,Migrations可以写成sql脚本,也可以写在java代码里;不仅
- 本文实例讲述了Android编程实现下载时主界面与详细界面一致更新的方法。分享给大家供大家参考,具体如下:1、创建监听管理类public c
- 本文实例讲述了Android判断设备网络连接状态及判断连接方式的方法。分享给大家供大家参考,具体如下:在Android开发过程中,对于一个需
- 一、什么是Websocket?1.WebSocket是HTML5下一种新的协议(websocket协议本质上是一个基于tcp的协议)2.它实
- 通常在C#的实际开发过程中,会发现设置其属性ScriptErrorsSuppressed无法达到屏蔽脚本错误效果,但是可以通过下面两种方式实
- 本文实例讲述了Android编程之交互对话框。分享给大家供大家参考,具体如下:1. 在Android SDK中,虽然有许多的窗口,有些类似M
- Mybatis 入参方式单个基本类型或 String 参数在 mapper 文件中随便写<select id=""
- 步骤:1、创建一个项目,该项目主要用来设计用户控件。2、创建一个用户控件窗体,用来设计用户控件。3、向用户控件窗体中添加一个按钮(butto
- 本文实例讲述了Android桌面组件App Widget用法。分享给大家供大家参考。具体如下:Android开发应用除了程序应用,还有App
- 本文实例为大家分享了java实现通讯录管理系统的具体代码,供大家参考,具体内容如下完成项目的流程:1.根据需求,确定大体方向 2.功能模块分