【Mybatis+springBoot】实现模糊查询
???????今天写项目,设计到了模糊查询,模糊查询部分学习时间长,都又忘记了,今天回顾了一下,于是这里写个总结。
pojo
package com.yczxyy.paperfree.dto.jhemr;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class JobEmrMonitor {
@ApiModelProperty(value = "监控代码编号")
private String MONITOR_ITEM_CODE;
@ApiModelProperty(value = "监控代码名称")
private String MONITOR_ITEM_NAME;
}
数据库
Mapper接口类
List<JobEmrMonitor> getMonitor(String MONITOR_ITEM_NAME);
Service
List<JobEmrMonitor> getMonitor(String MONITOR_ITEM_NAME);
ServiceImpl
@Override
public List<JobEmrMonitor> getMonitor(String MONITOR_ITEM_NAME) {
return dmsZhiKongRuleDao.getMonitor(MONITOR_ITEM_NAME);
}
Mapper.xml配置文件
<select id="getMonitor" resultType="com.yczxyy.paperfree.dto.jhemr.JobEmrMonitor" parameterType="String">
select MONITOR_ITEM_CODE MONITOR_ITEM_CODE,
MONITOR_ITEM_NAME MONITOR_ITEM_NAME
from dms_monitor
where 1=1
<if test="_parameter!=null">
and MONITOR_ITEM_NAME like concat('%',#{MONITOR_ITEM_NAME},'%')
</if>
在此 SQL 语句中, where 1=1 是多条件拼接时的小技巧, 后面的条件查询就可以都用 and 了。
Controller
@ApiOperation("监控代码/名称 字典")
@PostMapping("getMonitorNameAndCode")
public CommonResult getMonitorNameAndCode(@RequestParam(value="MONITOR_ITEM_NAME", required = false) String MONITOR_ITEM_NAME){
ArrayList<JobEmrMonitor> list = new ArrayList<>();
list=(ArrayList<JobEmrMonitor>) dmsZhiKongRuleService.getMonitor(MONITOR_ITEM_NAME);
return CommonResult.success(list);
}
测试
参考资料
1、https://blog.csdn.net/Rice_kil/article/details/83622160 2、https://blog.csdn.net/m0_48814545/article/details/116569516 3、https://blog.csdn.net/fanminghao/article/details/107544056 4、https://blog.csdn.net/qq_43700885/article/details/104148421
|