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>

 

posted @ 2017-10-14 02:24  Tomas曼  Views(369)  Comments(0Edit  收藏  举报