一、SQL语句常用小场景
1.查询某个表信息,表中某些字段为数据字典需要进行转义
sql
SELECT
t.ID,
CASE
WHEN t.DINING_TYPE = 1 THEN
'早餐'
WHEN t.DINING_TYPE = 2 THEN
'午餐'
WHEN t.DINING_TYPE = 3 THEN
'晚餐'
END AS diningTypeStr
from student t
2.联表查询语法
sql
select si.* from student_info si
left join classed_info ci on si.classed_id = ci.id
3.每个xml中可以自定义一个查询语句头
sql
<sql id="query">
select ID, ORDER_NO, WX_ORDER_NO, SCHOOL_ID, GRADE_ID, CLASSES_ID, STUDENT_ID, STUDENT_PHONE,
DINING_TYPE, FOOD_TYPE, PAY_TYPE, "STATUS", CREATE_ORDER_DATE_TIME, PAY_DATE_TIME,
IS_DELETE, CREATOR, CREATE_TIME, MODIFIER, MODIFY_TIME from student_info
</sql>
4.自定义查询语句
sql
<sql id="query">
<if test="gradeId != null and gradeId != ''">
and t.grade_id = #{gradeId, jdbcType=VARCHAR}
</if>
<if test="classesId != null and classesId != ''">
and t.classes_id = #{classesId, jdbcType=VARCHAR}
</if>
</sql>
5.批量新增
sql
<insert id="createList" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" separator=";" close=";end;">
INSERT INTO t_student_info (id, name)
VALUES(#{item.id},#{item.name})
</foreach>
</insert>