首先需要引入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) >= DATE(#{startDate}) and
</if>
<if test="startDate=='' or startDate==null and endDate!='' and endDate!=null">
DATE(bs.registration_time) <= 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>