zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    hibernate10--命名查询
    mybatis13--2级缓存
    mybatis12--一级缓存
    hibernate09--连接查询
    hibernate08--OpenSessionInView
    mybatis11--多对多关联查询
    mybatis10--自连接多对一查询
    mybatis09--自连接一对多查询
    mybatis08--关联查询多对一
    Oracle job启动与关闭
  • 原文地址:https://www.cnblogs.com/tomasman/p/7664882.html
Copyright © 2011-2022 走看看