<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.StudentDao">
<update id="updates" parameterType="com.pojo.Student">
update school.student
<trim suffix="where sid=#{sid}" suffixOverrides=",">
<set>
<if test="name!=null and name!=''">name = #{name},</if>
<if test="sex!=null and sex!=''">sex = #{sex},</if>
<if test="height!=null and height!=''">height = #{height},</if>
</set>
</trim>
</update>
<select id="findBy" resultType="Student">
select *
from student
<where>
<trim suffixOverrides="and">
<if test="name!=null and name!=''">
name like concat(#{name},'%')
</if>
<if test="sex!=null and sex!=''">
and sex=#{sex}
</if>
</trim>
</where>
</select>
<select id="findByIdName" resultType="com.pojo.Student">
select *
from student
where 1 = 1
<choose>
<when test="sid!=0">
and sid = #{sid}
</when>
<when test="name!=null and name!=''">
and name = #{name}
</when>
<otherwise>
and sid = 0
</otherwise>
</choose>
</select>
<select id="findBysid" resultType="com.pojo.Student">
select *
from school.student
where sid in
<foreach collection="list" item="sid" open="(" separator="," close=")">
#{sid}
</foreach>
</select>
<select id="findBypage" resultType="com.pojo.Student">
select *
from school.student
limit #{pageSize},#{pageCode};
</select>
</mapper>
Mybatis动态sql
最新推荐文章于 2024-10-06 20:16:20 发布
这个博客展示了如何使用MyBatis进行数据库的更新和查询操作。`StudentDao`映射器中包含了更新(updates)和多个查询(findBy、findByIdName、findBysid、findBypage)方法,利用了MyBatis的动态SQL特性,如`<if>`、`<trim>`和`<foreach>`标签来处理条件语句和分页查询。
摘要由CSDN通过智能技术生成