catalogue
preface
Follow the previous article: Engineering and routine operation of fluent mybatis project (addition, deletion and modification) (I) | practice of fluent mybatis_ A Liang's blog - CSDN blog
Warehouse address: GitHub warehouse
query
Define query request body
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: query criteria */ @Data @AllArgsConstructor @NoArgsConstructor @Builder public class TestFluentMybatisQueryReq { private String age; private String name; }
Query writing 1
Query interface method definition
/** * Query interface 1 * * @param queryReq Query request * @return list */ List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq);
Method, here we use mapper to implement the query syntax pattern given by the official.
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: Implementation of basic operation interface */ @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 layer method definition
@ApiOperation(value = "Query data 1", notes = "Query data 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); } }
Debug the interface
At first glance, there seems to be no problem, but friends of long-term back-end development should be able to see that if the age or name parameter is empty, there will be no result. Because according to the writing of the statement, the two parameters of age and name will be forcibly compared.
Let's try setting one of the parameters to an empty string.
Not surprisingly. Now I can adjust the method, judge the parameters, and then replace the select statement. For more elegant implementation, I'll go to the official documents and look for it again.
Query writing 2
Query method 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: Implementation of basic operation interface */ @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()); } } }); } }
Code description
I compared the writing of official documents and found that the listByMap method in my version of fm has one more Boolean parameter of isColumn. So I chased the source code.
It only affects error printing. The main reason is that the set map parameter must be the column name or the parameter name in the entity object. It doesn't matter.
Verify it
No problem, you can still find out.
New problems
However, according to this query method, if both values are passed into an empty string, will the full table data be found?
Verify it
Cough, cough, wrong report.
So I'd better honestly judge and optimize the code parameters first.
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: Implementation of basic operation interface */ @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()); } } }); } }
Verify it
Delete
Add interface method to delete data by ID
/** * Delete interface * * @param id id */ void deleteById(Integer id);
Implementation interface method
@Override public void deleteById(Integer id) { testFluentMybatisMapper.deleteById(id); }
Verify it
Delete succeeded
summary
These two articles are mainly to carry out engineering transformation of previous projects, and add some regular operations such as documents and interfaces. The basic functions of adding, deleting, modifying and querying database tables are realized. Other functions will be updated slowly later. fm integrates many other orm frameworks and needs to be explored slowly.
If this article is helpful to you, please point a praise to support it.