标签:hda 图片 ISE csdn sep gets for https 常量
属性介绍:
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Object"> select * from student where id=#{id} </select>
属性介绍:
<insert id="insert" parameterType="Object"> insert into student <trim prefix="(" suffix=")" suffixOverrides="," > <if test="name != null "> NAME, </if> </trim> <trim prefix="values(" suffix=")" suffixOverrides="," > <if test="name != null "> #{name}, </if> </trim> </insert>
属性同 insert
<delete id="deleteByPrimaryKey" parameterType="Object"> delete from student where id=#{id} </delete>
属性同 insert
resultMap 标签的使用
基本作用:
!注意:与java对象对应的列不是数据库中表的列名,而是查询后结果集的列名
<resultMap id="BaseResultMap" type="com.online.charge.platform.student.model.Student"> <id property="id" column="id" /> <result column="NAME" property="name" /> <result column="HOBBY" property="hobby" /> <result column="MAJOR" property="major" /> <result column="BIRTHDAY" property="birthday" /> <result column="AGE" property="age" /> </resultMap> <!--查询时resultMap引用该resultMap --> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Object"> select id,name,hobby,major,birthday,age from student where id=#{id} </select>
标签说明:
主标签:
子标签:
if标签通常用于WHERE语句、UPDATE语句、INSERT语句中,通过判断参数值来决定是否使用某个查询条件、判断是否更新某一个字段、判断是否插入某个字段的值。
<if test="name != null and name != ‘‘"> and NAME = #{name} </if>
foreach标签主要用于构建in条件,可在sql中对集合进行迭代。也常用到批量删除、添加等操作中。
<!-- in查询所有,不分页 --> <select id="selectIn" resultMap="BaseResultMap"> select name,hobby from student where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
属性介绍:
有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。
if是与(and)的关系,而choose是或(or)的关系。
<select id="getStudentListChoose" parameterType="Student" resultMap="BaseResultMap"> SELECT * from STUDENT WHERE 1=1 <where> <choose> <when test="Name!=null and student!=‘‘ "> AND name LIKE CONCAT(CONCAT(‘%‘, #{student}),‘%‘) </when> <when test="hobby!= null and hobby!= ‘‘ "> AND hobby = #{hobby} </when> <otherwise> AND AGE = 15 </otherwise> </choose> </where> </select>
当if标签较多时,这样的组合可能会导致错误。 如下:
<select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap"> SELECT * from STUDENT WHERE <if test="name!=null and name!=‘‘ "> NAME LIKE CONCAT(CONCAT(‘%‘, #{name}),‘%‘) </if> <if test="hobby!= null and hobby!= ‘‘ "> AND hobby = #{hobby} </if> </select>
当name值为null时,查询语句会出现 “WHERE AND” 的情况,解决该情况除了将"WHERE"改为“WHERE 1=1”之外,还可以利用where标签。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
<select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap"> SELECT * from STUDENT <where> <if test="name!=null and name!=‘‘ "> NAME LIKE CONCAT(CONCAT(‘%‘, #{name}),‘%‘) </if> <if test="hobby!= null and hobby!= ‘‘ "> AND hobby = #{hobby} </if> </where> </select>
没有使用if标签时,如果有一个参数为null,都会导致错误。当在update语句中使用if标签时,如果最后的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置set关键字,和剔除追加到条件末尾的任何不相关的逗号。
<update id="updateStudent" parameterType="Object"> UPDATE STUDENT SET NAME = #{name}, MAJOR = #{major}, HOBBY = #{hobby} WHERE ID = #{id}; </update>
<update id="updateStudent" parameterType="Object"> UPDATE STUDENT SET <if test="name!=null and name!=‘‘ "> NAME = #{name}, </if> <if test="hobby!=null and hobby!=‘‘ "> MAJOR = #{major}, </if> <if test="hobby!=null and hobby!=‘‘ "> HOBBY = #{hobby} </if> WHERE ID = #{id}; </update>
使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值。
<update id="updateStudent" parameterType="Object"> UPDATE STUDENT <set> <if test="name!=null and name!=‘‘ "> NAME = #{name}, </if> <if test="hobby!=null and hobby!=‘‘ "> MAJOR = #{major}, </if> <if test="hobby!=null and hobby!=‘‘ "> HOBBY = #{hobby} </if> </set> WHERE ID = #{id}; </update>
格式化输出,也可以通过trim标签设定或忽略前后缀来实现。
5.1 collection标签
5.2 association标签
关于关联映射关系,详细参考我的这篇博客
当多种类型的查询语句的查询字段或者查询条件相同时,可以将其定义为常量,方便调用。为求<select>结构清晰也可将sql语句分解。
<!-- 查询字段 --> <sql id="Base_Column_List"> ID,MAJOR,BIRTHDAY,AGE,NAME,HOBBY </sql> <!-- 查询条件 --> <sql id="Example_Where_Clause"> where 1=1 <trim suffixOverrides=","> <if test="id != null and id !=‘‘"> and id = #{id} </if> <if test="major != null and major != ‘‘"> and MAJOR = #{major} </if> <if test="birthday != null "> and BIRTHDAY = #{birthday} </if> <if test="age != null "> and AGE = #{age} </if> <if test="name != null and name != ‘‘"> and NAME = #{name} </if> <if test="hobby != null and hobby != ‘‘"> and HOBBY = #{hobby} </if> <if test="sorting != null"> order by #{sorting} </if> <if test="sort!= null and sort != ‘‘ "> order by ${sort} ${order} </if> </trim> </sql>
用于引用定义的常量
<!-- 查询所有,不分页 --> <select id="selectAll" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> FROM student <include refid="Example_Where_Clause" /> </select> <!-- 分页查询 --> <select id="select" resultMap="BaseResultMap"> select * from ( select tt.*,rownum as rowno from ( SELECT <include refid="Base_Column_List" /> FROM student <include refid="Example_Where_Clause" /> ) tt <where> <if test="pageNum != null and rows != null"> and rownum <![CDATA[<=]]>#{page}*#{rows} </if> </where> ) table_alias where table_alias.rowno>#{pageNum} </select> <!-- 根据条件删除 --> <delete id="deleteByEntity" parameterType="java.util.Map"> DELETE FROM student <include refid="Example_Where_Clause" /> </delete>
原文地址:https://blog.csdn.net/m0_38054145/article/details/81906343
标签:hda 图片 ISE csdn sep gets for https 常量
原文地址:https://www.cnblogs.com/kerwincui/p/12491968.html