• SSM(三)Mybatis动态SQL


    1.查询语句,where:

    1 <resultMap id="xxx" type="xx..Student" autoMapping="false">...</resultMap>
    2 <select id="findAll" resultMap="xxx">
    3     select * from student
    4     <where>
    5         <if test="stuNo!=null and stuNo!=''">and stuNo=#{stuNo}</if>
    6         <if test="stuName!=null and stuName!=''">and stuName like '%' #{stuName} '%'</if>
    7     </where>
    8 </select>

    2.插入语句,trim:

     1 <insert id="saveAttendances">
     2         insert into tms_attendance
     3                 <trim prefix="(" suffixOverrides="," suffix=")">
     4                     <if test="at_tid != null">at_tid,</if>
     5                     <if test="at_labnum != null">at_labnum,</if>
     6                     ...
     7                 </trim>
     8             VALUES
     9         <trim prefix="(" suffixOverrides="," suffix=")">
    10             <if test="at_tid != null">#{at_tid},</if>
    11             <if test="at_labnum != null">#{at_labnum},</if>
    12             ...
    13         </trim>
    14     </insert>

    3.修改语句,trim:

    1 <update id="editUserById" parameterType="cn.happy.entity.Smbms_user">
    2         update smbms_user
    3                 <trim prefix="set" suffixOverrides=",">
    4                     <if test="username !=null and username !=''">userName=#{username},</if>
    5                     <if test="userpassword !=null">userPassword=#{userpassword},</if>
    6                 </trim>
    7         where id=#{id}
    8     </update>

     4.查询语句,foreach:

    1 <select id="findByList" resultType="Student">
    2     select * from student
    3     <if test="list.size>0">
    4         WHERE stuno in
    5         <foreach collection="list" open="(" close=")" separator="," item="myid">
    6             #{myid}
    7         </foreach>
    8     </if>
    9 </select>
  • 相关阅读:
    滑动窗口法学习
    209. Minimum Size Subarray Sum
    485. Max Consecutive Ones
    27. Remove Element
    167. Two Sum II
    561. Array Partition I
    344. Reverse String
    14. 最长公共前缀
    layui上传文件时出现 请求上传接口出错
    Linux-5.13将初步支持苹果M1 Soc
  • 原文地址:https://www.cnblogs.com/tomasman/p/7664882.html
Copyright © 2020-2023  润新知