新问题
但是按照这个查询方法,如果两个值都传空字符串会查出全表数据吗?
验证一下
咳咳,报错了。
所以我还是老老实实先把代码参数判空优化一下。
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); }
验证一下
删除成功
总结
这两篇文章主要是将之前的项目进行工程化改造,增加了文档、接口等一些列常规化操作。实现了数据库表的基本增删改查功能。其他的功能会在之后慢慢更新,fm融合了很多其他orm框架的东西,需要慢慢摸索摸索。
如果本文对你有帮助,请点个赞支持一下吧。