Mybatis-Plus多表联查与条件查询

首先需要引入mybatis-puls的依赖,和配置数据库连接的信息:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.21</version>
    </dependency>

数据库连接

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    #url: jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8
    url: jdbc:mysql://localhost:3306/XXX(数据库名)?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
    username: 账户
    password: 密码
    type: com.alibaba.druid.pool.DruidDataSource

第二步就是编写创建service层与contrller层了(省略);
第三步就是编写sql语句了:

<sql id="tables">
    select bs.student_id as studentId,
    bsh.shift_id as shiftId,
    bs.student_name as name,
    bs.sfz_number as number,
    bst.state as state,
    bs.student_phone as phone,
    bas.assigns_name as assigns,
    bs.registration_time as assignsDate,
    concat(bcf.carform_name ,'-',bsst.shift_system) as department,
    concat( bcfes.carform_name,'-',bsstes.shift_system) as carShift,
    bsh.money as subjects,
    br.references_name as reference
    FROM b_shift bsh
    LEFT JOIN b_student bs on bsh.student_id =bs.student_id
    LEFT JOIN b_shift_system bsst on bsh.shift_system_id =bsst.shift_system_id
    LEFT JOIN b_shift_system bsstes on bsh.old_shift_system_id =bsstes.shift_system_id
    LEFT JOIN b_state bst on bsh.audit_state = bst.state_id
    LEFT JOIN b_assigns bas on bs.assigns_id = bas.assigns_id
    LEFT JOIN b_carform bcf on bsst.carform_id =bcf.carform_id
    LEFT JOIN b_carform bcfes on bsstes.carform_id =bcfes.carform_id
    LEFT JOIN b_references br on bsh.references_id = br.references_id
</sql>
通过left join把多个表连接起来,再通过select 把想要的字段一个个查出来 第四步就是条件查询了:
<select id="QueryWrapperTable" resultType="com.gx.busi.entity.Vo.student.archivesVo">
    <include refid="tables"></include>where
    <if test="siginAddress !='' and siginAddress!=null">
        bas.assigns_id = #{siginAddress} and
    </if>
    <if test="printState !='' and printState!=null">
        bst.state like concat(#{printState}) and
    </if>
    <if test="exchangeReference !='' and exchangeReference!=null">
        br.references_name like concat('%',#{exchangeReference},'%') and
    </if>
    <if test="startDate!='' and startDate!=null or endDate!=''and endDate!=null">
        <if test="startDate!='' and startDate!=null and endDate=='' or endDate==null">
            DATE(bs.registration_time) &gt;= DATE(#{startDate}) and
        </if>
        <if test="startDate=='' or startDate==null and endDate!='' and endDate!=null">
            DATE(bs.registration_time) &lt;= DATE(#{endDate}) and
        </if>
        <if test="startDate!='' and startDate!=null and endDate!='' and endDate!=null">
            DATE(bs.registration_time) BETWEEN DATE(#{startDate}) and DATE(#{endDate}) and
        </if>
    </if>  1=1
</select>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值