...">
美文网首页
多条件搜索功能的自定义SQL--mybatis-plus

多条件搜索功能的自定义SQL--mybatis-plus

作者: 私人云笔记_骁勇波波 | 来源:发表于2023-02-13 18:07 被阅读0次

    1.  DeviceMapper.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    <mapper namespace="com.bmc.cloud.mapper.DeviceMapper">

        <resultMap id="BaseResultMap" type="com.bmc.cloud.model.ExtDevice">

            <id column="id" property="id" jdbcType="INTEGER" />

            <result column="pairing_status" property="pairingStatus" jdbcType="VARCHAR"/>

            <result column="create_id" property="createId" jdbcType="INTEGER"/>

            <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>

            <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>

        </resultMap>

        <select id="getDeviceListByCondition" resultMap="BaseResultMap">

    select c.*, d.patient_hcp_id

            from (

                SELECT a.*, b.patient_id

                from sys_device as a inner join sys_patient_device as b

                on a.sn=b.sn

                where a.create_id=#{createId, jdbcType=INTEGER}

                <if test="condition.model != null and condition.model != ''">

                    and a.model = #{condition.model, jdbcType=VARCHAR}

                </if>

                <if test="condition.type != null and condition.type != ''">

                    and a.type = #{condition.type, jdbcType=VARCHAR}

                </if>

                <if test="condition.status != null and condition.status != ''">

                    and a.status = #{condition.status, jdbcType=VARCHAR}

                </if>

                <if test="condition.pairingStatus != null and condition.pairingStatus != ''">

                    and a.pairing_status = #{condition.pairingStatus, jdbcType=VARCHAR}

                </if>

                <if test="condition.startTime != null and condition.startTime != ''">

                    and a.create_time >= STR_TO_DATE(#{condition.startTime, jdbcType=VARCHAR},'%Y-%m-%d %H:%i:%s')

                </if>

                <if test="condition.endTime != null and condition.endTime != ''">

                    and a.create_time &lt;= STR_TO_DATE(#{condition.endTime, jdbcType=VARCHAR},'%Y-%m-%d %H:%i:%s')

                </if>

            ) as c

            left JOIN sys_patient as d

            ON c.patient_id=d.id

        </select>

    </mapper>

    2. DeviceMapper.java

    public interface DeviceMapper extends BaseMapper<Device> {

    // 多表符合查询列表

        @Select("select c.*, d.patient_hcp_id from (SELECT a.*, b.patient_id from sys_device as a LEFT JOIN sys_patient_device as b on a.create_id=#{createId} AND a.sn=b.sn) as c left JOIN sys_patient as d ON c.patient_id=d.id")

    Page<ExtDevice> getPageDevicePatientVo(Integer createId, Page<ExtDevice> iPage);

    // 多条件搜索功能

        Page<ExtDevice> getDeviceListByCondition(Integer createId, Page<ExtDevice> iPage,@Param("condition") SearchDeviceRequest request);

    }

    3. DeviceServiceImpl

    public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

        @Resource

        DeviceMapper deviceMapper;

        @Override

        public Page<ExtDevice> searchDeviceList(Account account, SearchDeviceRequest request) {

            //获取指定分页的设备列表

            Long page = Long.valueOf(request.getPage());

            Long size = Long.valueOf(request.getSize());

            Page<ExtDevice> ipage = new Page<ExtDevice>(page, size);

            Page<ExtDevice> result = deviceMapper.getDeviceListByCondition(account.getId(), ipage, request);

            return result;

        }

    }

    4. DeviceService

    public interface IDeviceService extends IService<Device> {

      public Page<ExtDevice> getDeviceList(Account account, Page<ExtDevice> ipage);

      public Page<ExtDevice> searchDeviceList(Account account, SearchDeviceRequest request);

    }

    5. DeviceController

    @ApiOperation(value = "按条件获取设备列表", notes = "post请求")

        @ApiImplicitParams({

                @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "java.lang.String"),

                @ApiImplicitParam(name = "size", value = "单页容量", required = true, dataType = "java.lang.String"),

                @ApiImplicitParam(name = "startTime", value = "设备添加开始时间", required = true, dataType =             "java.lang.String"),

                @ApiImplicitParam(name = "endTime", value = "设备添加结束时间", required = true, dataType =                 "java.lang.String")

        })

        @PostMapping("/search")

        BaseResponse searchDeviceList(@RequestBody SearchDeviceRequest requestBody) {

            Page<ExtDevice> deviceList = deviceService.searchDeviceList(account, requestBody);

            return success(deviceList);

        }

    6. 日期过滤简化:入参 before_date, after_date为String

    and v1.record_date BETWEEN #{before_date, jdbcType=DATE} AND #{after_date, jdbcType=DATE}

    order by v1.record_date

    相关文章

      网友评论

          本文标题:多条件搜索功能的自定义SQL--mybatis-plus

          本文链接:https://www.haomeiwen.com/subject/hlhskdtx.html