文章目录
要求:在搜索框中进行模糊查询,查询到一个字段中的一个数据时把整个数据都显示出来
1. 实现
1.1 展现数据
所有字段属性显示
1.2 模糊查询
点击搜索框实现搜索时,会直接显示跟该字段相关的所有数据
1.3 实现结果
2. 实现SQL
<select id="faulSolutionServiceListPage"
resultType="cn.qt.saas.erp.infrastructure.persistence.pojo.FaultSolutionDO">
select
eft.*,
GROUP_CONCAT(d.device_type_id) as device_type
from
erp_fault_solution eft
left join (
select a.id ,GROUP_CONCAT(b.device_type_id) as device_type from erp_fault_solution a inner join
erp_fault_solution_has_device_type b on a.id = b.fault_solution_id and a.is_deleted = 0 where b.is_deleted = 0
<if test="query.deviceTypeId != null and query.deviceTypeId.size > 0">
and b.device_type_id in
<foreach collection="query.deviceTypeId" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
group by a.id
) c
on c.id = eft.id
left join erp_fault_solution_has_device_type d on eft.id = d.fault_solution_id and d.is_deleted = 0
<where>
<if test="1==1">
eft.is_deleted = 0
</if>
<if test="query.code != null and query.code != ''">
and eft.code like concat ('%',#{query.code},'%')
</if>
<if test="query.name != null and query.name != ''">
and eft.name like concat ('%',#{query.name},'%')
</if>
<if test="query.deviceTypeId != null and query.deviceTypeId.size > 0">
and c.device_type is not null
</if>
</where>
group by
eft.id
</select>
2.1 SQL解析
2.1.1 查询语句
<select id="faulSolutionServiceListPage"
resultType="cn.qt.saas.erp.infrastructure.persistence.pojo.FaultSolutionDO">
</select>
create:新增
select :查询
update:修改
delete:删除
此处使用的是查询语句
id:是作为mapper层的一个查询接口
resulType:作为DO层,可以直接连接数据库进行数据的增删改查
2.1.2 开头查询
select
eft.*,
GROUP_CONCAT(d.device_type_id) as device_type
from
erp_fault_solution eft
查询开头表示这一段sql是用来查询的
eft.*
表示查询eft中的所有字段,正常情况下是不推荐使用星号的,毕竟是引用星号占用SQL性能
GROUP_CONCAT(d.device_type_id) as device_type
按照d中device_type_id字段分组并设置一个别名为device_type
from
erp_fault_solution eft
在 erp_fault_solution 表中查询并设置一个别名为eft
2.1.3 关联查询
left join (
select a.id ,GROUP_CONCAT(b.device_type_id) as device_type from erp_fault_solution a inner join
erp_fault_solution_has_device_type b on a.id = b.fault_solution_id and a.is_deleted = 0 where b.is_deleted = 0
关联查询分为几种:左关联,右关联,内关联 笛卡尔积
常用的分别是:左关联,右关联
不常用:笛卡尔积,内关联
笛卡尔积:使用笛卡尔积可能会导致字段冗余
内关联:使用内关联查询的是两表之间所有的数据,过于庞大,所以不推荐平常使用,可以根据业务使用
select a.id ,GROUP_CONCAT(b.device_type_id) as device_type from erp_fault_solution a
查询a表中的id字段,分组b表中的 device_type_id 别名为 device_type 在 erp_fault_solution 表中查询并设置一个别名为a
inner join
erp_fault_solution_has_device_type b on a.id = b.fault_solution_id and a.is_deleted = 0 where b.is_deleted = 0
内关联
erp_fault_solution_has_device_type 表并设置一个别名为b并进行判断b表中的 fault_solution_id 字段和a表中的 is_deleted =0
查询的条件是b表中的 is_deleted =0
2.1.4 判断查询与循环查询
<if test="query.deviceTypeId != null and query.deviceTypeId.size > 0">
and b.device_type_id in
<foreach collection="query.deviceTypeId" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
if标签作为判断条件,可以在其中写入需要进行判断的条件
<if test="query.deviceTypeId != null and query.deviceTypeId.size > 0"></if>
foreach标签作为循环查询,可以在其中写入需要循环的条件
<foreach collection="query.deviceTypeId" item="item" index="index" open="(" close=")" separator=",">
2.1.5 分组查询
group by a.id
) c
on c.id = eft.id
left join erp_fault_solution_has_device_type d on eft.id = d.fault_solution_id and d.is_deleted = 0
group by为分组查询
这里为id分组查询,条件是c表中的id = eft表中的id
left join erp_fault_solution_has_device_type d on eft.id = d.fault_solution_id and d.is_deleted = 0
进行左关联查询
erp_fault_solution_has_device_type 并设置一个别名d 左关联查询条件是 d表中的 fault_solution_id 和eft表中的id相等,同时d表中的is_deleted=0
2.1.6 查询条件并分组
<where>
<if test="1==1">
eft.is_deleted = 0
</if>
<if test="query.code != null and query.code != ''">
and eft.code like concat ('%',#{query.code},'%')
</if>
<if test="query.name != null and query.name != ''">
and eft.name like concat ('%',#{query.name},'%')
</if>
<if test="query.deviceTypeId != null and query.deviceTypeId.size > 0">
and c.device_type is not null
</if>
</where>
group by
eft.id
where标签作为条件查询语句
if标签作为判断查询语句
<if test="1==1">
eft.is_deleted = 0
</if>
这个判断查询的条件是test=“1==1”
意思是:任何条件下都成立,必定进入判断
eft表中的is_deleted = 0
<if test="query.code != null and query.code != ''">
and eft.code like concat ('%',#{query.code},'%')
</if>
这个判断的条件是test=“query.code != null and query.code != ‘’”
意思是:查询到code不是null值时和不是空值时成立,进入判断
and eft.code like concat ('%',#{query.code},'%')
和eft表中的code字段进行模糊查询
concat函数用于连接两个字符串,形成一个字符串
<if test="query.name != null and query.name != ''">
and eft.name like concat ('%',#{query.name},'%')
</if>
这个判断的条件是test=“query.name != null and query.name != ‘’”
意思是:查询到name不是null值时和不是空值时成立,进入判断
and eft.name like concat ('%',#{query.name},'%')
和eft表中的name字段进行模糊查询
concat函数用于连接两个字符串,形成一个字符串
<if test="query.deviceTypeId != null and query.deviceTypeId.size > 0">
and c.device_type is not null
</if>
这个判断的条件是test=“query.deviceTypeId != null and query.deviceTypeId.size > 0”
意思是查询到的deviceTypeId字段不为空,字段的长度大于0
and c.device_type is not null
和 c表中的device_type字段不为null
group by
eft.id
查询到所有的数据时对eft表中的id字段进行分组