• MyBatis SQL语句写法


    一、forEach

    接口:

    public List<Entity> queryById(List<String> userids);

    语法:

    <select id="queryById" resultMap="BaseReslutMap" >
        select * FROM entity
        where id in
        <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
            #{userid}
        </foreach>
    </select>

    二、concat (模糊查询)

    语法:

    <select id="queryById" resultMap="BascResultMap" parameterType="entity">
         SELECT * from entity
        <where>
            <if test="name!=null">
                name like concat('%',concat(#{name},'%'))
            </if>
        </where>
    </select>        
    

      

    三、choose(when、otherwise)标签

    语法:

    <!-- choose(判断参数) - 按顺序将实体类 User 第一个不为空的属性作为:where条件 -->
    <select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">
       SELECT *
       FROM User u
         <where>
           <choose>
             <when test="username !=null ">
               u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')
             </when >
             <when test="sex != null and sex != '' ">
               AND u.sex = #{sex, jdbcType=INTEGER}
             </when >
             <when test="birthday != null ">
               AND u.birthday = #{birthday, jdbcType=DATE}
             </when >
           <otherwise>
           </otherwise>
         </choose>
       </where>
    </select>

    四、selectKey(自动赋值添加对象生成的组件)

    <!-- 插入学生 自动主键-->
    <insert id="createStudentAutoKey" parameterType="liming.student.manager.data.model.StudentEntity" keyProperty="studentId">
         <selectKey keyProperty="studentId" resultType="String" order="BEFORE">
            select nextval('student')
         </selectKey>
             INSERT INTO STUDENT_TBL(STUDENT_ID,
             STUDENT_NAME,
             STUDENT_SEX,
             STUDENT_BIRTHDAY,
             STUDENT_PHOTO,
             CLASS_ID,
             PLACE_ID)
             VALUES (#{studentId},
             #{studentName},
             #{studentSex},
             #{studentBirthday},
             #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},
             #{classId},
             #{placeId})
    </insert>

    五、参数为ArrayList类型

    <!— 7.1 foreach(循环array参数) - 作为where中in的条件 -->
    <select id="getStudentListByClassIds_foreach_array" resultMap="resultMap_studentEntity">
    	 SELECT ST.STUDENT_ID,
    	 ST.STUDENT_NAME,
    	 ST.STUDENT_SEX,
    	 ST.STUDENT_BIRTHDAY,
    	 ST.STUDENT_PHOTO,
    	 ST.CLASS_ID,
    	 ST.PLACE_ID
    	 FROM STUDENT_TBL ST
    	 WHERE ST.CLASS_ID IN
    	 <foreach collection="array" item="classIds" open="(" separator="," close=")">
    		#{classIds}
    	 </foreach>
    </select> 

    六、if + where

    <!-- 3 select - where/if(判断参数) - 将实体类不为空的属性作为where条件 -->
    <select id="getStudentList_whereIf" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">
         SELECT ST.STUDENT_ID,
         ST.STUDENT_NAME,
         ST.STUDENT_SEX,
         ST.STUDENT_BIRTHDAY,
         ST.STUDENT_PHOTO,
         ST.CLASS_ID,
         ST.PLACE_ID
         FROM STUDENT_TBL ST
         <where>
             <if test="studentName !=null ">
             ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')
             </if>
             <if test="studentSex != null and studentSex != '' ">
             AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}
             </if>
             <if test="studentBirthday != null ">
             AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}
             </if>
             <if test="classId != null and classId!= '' ">
             AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}
             </if>
             <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">
             AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}
             </if>
             <if test="placeId != null and placeId != '' ">
             AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}
             </if>
             <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">
             AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}
             </if>
             <if test="studentId != null and studentId != '' ">
             AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}
             </if>
         </where>
    </select>
  • 相关阅读:
    selenium屏蔽浏览器检测
    cookies字符串转化为字典
    python压缩图片大小
    python异步爬虫aiohttp
    python通过命令行接收参数
    hustoj实现远程判题的两种方案
    SqlLocalDB工具的一些有趣发现
    Chrome中编译安装vue-devtools插件
    用友政务表格技术应用开发实践:预算一体化产品核心功能搭建
    企业表格技术应用开发案例大赛圆满落幕!
  • 原文地址:https://www.cnblogs.com/ldl326308/p/10956504.html
Copyright © 2020-2023  润新知