詳情參照mybatis官方文檔:https://mybatis.org/mybatis-3/zh/index.html
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hz.platform.demo.dao.StudentDao">
<!-- 中文文檔https://mybatis.org/mybatis-3/zh/dynamic-sql.html-->
<resultMap id="studentResult" type="com.hz.platform.demo.entity.Student">
<result column="stu_name" property="stuName" jdbcType="VARCHAR"/>
<result column="address" property="address" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
<result column="birthday" property="birthday" jdbcType="TIMESTAMP"/>
<result column="gender" property="gender" jdbcType="VARCHAR"/>
</resultMap>
<!-- 動態sql測試 where if -->
<select id="findAll" resultMap="studentResult">
select stu_name,address,age,birthday,gender from student
<trim prefix="where" prefixOverrides="AND|OR">
<if test="stuName != '' and stuName != null">
and stu_name = #{stuName,jdbcType=VARCHAR}
</if>
<if test="address != '' and address != null">
and address = #{address,jdbcType=VARCHAR}
</if>
</trim>
</select>
<!-- 有時我們不想應用到所有的條件語句,而只想從中擇其一項。針對這種情況,MyBatis 提供了 choose 元素,它有點像 Java 中的 switch 語句。-->
<select id="find1" resultMap="studentResult">
select stu_name,address,age,birthday,gender from student
<where>
<choose>
<when test="stuName != '' and stuName != null">
and stu_name = #{stuName,jdbcType=VARCHAR}
</when>
<when test="address != '' and address != null">
and address = #{address,jdbcType=VARCHAR}
</when>
<otherwise>
AND gender = "男"
</otherwise>
</choose>
</where>
</select>
<insert id="insertStudent" parameterType="com.hz.platform.demo.entity.Student">
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
uuid,
<if test="stuName != null and stuName != ''">
stu_name,
</if>
<if test="address != null and address != ''">
address,
</if>
<if test="age != null and age != ''">
age,
</if>
<if test="birthday != null">
birthday,
</if>
<if test="gender != null and gender != ''">
gender,
</if>
</trim>
<trim prefix="VALUES(" suffix=")" suffixOverrides=",">
(SELECT uuid()),
<if test="stuName != null">
#{stuName,jdbcType=VARCHAR},
</if>
<if test="address != null">
#{address,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="birthday != null">
#{birthday,jdbcType=TIMESTAMP},
</if>
<if test="gender != null">
#{gender,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateStudent" parameterType="com.hz.platform.demo.entity.Student">
update student
<trim prefix="set" suffixOverrides=",">
<if test="stuName != null">
stu_name = #{stuName,jdbcType=VARCHAR},
</if>
<if test="address != null">
address = #{address,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
<if test="birthday != null">
birthday = #{birthday,jdbcType=TIMESTAMP},
</if>
<if test="gender != null">
gender = #{gender,jdbcType=VARCHAR},
</if>
</trim>
<trim prefix="where" prefixOverrides="AND|OR" suffixOverrides=",">
uuid =#{uuid,jdbcType=VARCHAR}
</trim>
</update>
</mapper>