一个好的好的sql能省很多逻辑代码,最开始的做手中的的代码时,老是习惯的本着面向过程书写代码,一行一行代码的敲,判断来判断去,如果这个成功就执行这个,反之在进行一个新得逻辑,但java这门语言主打的就是面向对象,好多东西封装好了我们用就行了,简单的东西千万别复杂化了,java可以说用到老学到老,你不学,也不用,你就会被时代抛弃,数数大家都会,但是java就好比你要学会加减,在后面你要学乘除,在后面你要学方程等等,你需要知道1+1=2吗?,你不需要!你只需要会套公式用就行了,如果你只停留在数数的层次早晚会被淘汰,让你套个公式(框架)都不会用,那只能给这行说再见了。
下面这段sql,三表联查,非空判断条件查询,resultMap的映射
<resultMap id="PackScannerResult" type="PackScanner" >
<id property="scannerId" column="scanner_id" />
<result property="scannerCode" column="scanner_code" />
<result property="status" column="status" />
<result property="createTime" column="create_time" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
<association property="packPosition" javaType="PackPosition">
<id property="positionId" column="position_id"/>
<result property="positionName" column="position_name" />
</association>
</resultMap>
<sql id="selectPackScanner">
select s.scanner_id, s.scanner_code, s.update_time, s.create_time,s.del_flag, s.status, s.remark,pc.position_name
from pack_scanner s
</sql>
<select id="selectScannerList" parameterType="PackScanner" resultMap="PackScannerResult">
<include refid="selectPackScanner"/>
LEFT JOIN pack_position_scanner pa ON s.`scanner_id`=pa.`scanner_id`
LEFT JOIN pack_position pc ON pa.`position_id`= pc.`position_id`
where s.del_flag = '0'
<if test="scannerCode != null and scannerCode != ''">
AND s.scanner_code like concat('%', #{scannerCode}, '%')
</if>
<if test="status != null and status != ''">
AND s.status = #{status}
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
and date_format(s.create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
and date_format(s.create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
</if>
<if test="packPosition.positionName != null and packPosition.positionName != ''">
and position_name LIKE concat('%', #{packPosition.positionName}, '%')
</if>
</select>
(1)左外连接,LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL,ON 为条件查询条件。
SELECT s.StudentId,s.StudentName,sc.ClassName
FROM student s
LEFT JOIN student_class_code scc
ON s.StudentId = scc.StudentID
LEFT JOIN student_class sc
ON scc.ClassId= sc.ClassId
通过上图得知我们通过两次左外连接拿到了C表的全部数据,你在有一张表仍然可以在拼接条件进行查询,注意如果左边关联的右表对应仪表不存在右边置空,右外连接和隐式内连接大差不差
(2)配置resultMap标签,映射不同的字段和属性名,如果你查询的结果对象中有一个非基本类型(集合或者对象集合),这时你就需要用到resultMap标签
<!-- 查询的所有数据 -->
<!-- resultMap最终还是要将结果对象上,type映射指定到这个对象上 -->
<resultMap id="PackScannerResult" type="PackScanner" >
<!-- 定义普通属性 -->
<id property="scannerId" column="scanner_id" />
<result property="scannerCode" column="scanner_code" />
<result property="status" column="status" />
<result property="createTime" column="create_time" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
<!-- association :对象属性标签,他里面包括普通属性 -->
<!-- property:PackScanner对象里面的PackPosition对象属性名 -->
<!-- javaType:属性类型 -->
<association property="packPosition" javaType="PackPosition">
<!-- property:主键在pojo中的属性名 -->
<!-- column:主键在数据库中的列名 --两者一一对应映射 -->
<id property="positionId" column="position_id"/>
<result property="positionName" column="position_name" />
</association>
</resultMap>
<sql id="selectPackScanner">
select s.scanner_id, s.scanner_code, s.update_time, s.create_time,s.del_flag, s.status, s.remark,pc.position_name
from pack_scanner s
</sql>
<!-- resultMap=必须对应resultMap标签的id的值 -->
<select id="selectScannerList" parameterType="PackScanner" resultMap="PackScannerResult">
<include refid="selectPackScanner"/>
LEFT JOIN pack_position_scanner pa ON s.`scanner_id`=pa.`scanner_id`
LEFT JOIN pack_position pc ON pa.`position_id`= pc.`position_id`
where s.del_flag = '0'
</select>