目錄
一.基礎操作
1.刪除
2.新增
3.更新
4.查詢
5.XML映射文件
二、動態SQL
1.<if>
2.<where>
3.<set>
4.<foreach>
5.<sql>
6.<include>
一.基礎操作
1.刪除
參數占位符:
注意:?
#{...}相比于${...}具有性能高、防止SQL注入的優勢
2.新增
3.更新
4.查詢
普通查詢:
條件查詢:
注意:#{...}不能放在' '當中,使用${..,},會發生SQL注入,解決辦法是使用contact()函數
數據封裝:?
5.XML映射文件
實例:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace為Mapper接口的全限定名-->
<mapper namespace="com.itheima.springbootmybatisquickstart.mapper.EmpMapper"><!--id為函數名,resultType為返回的對象的全限定名--><select id="IndistinctSelect" resultType="com.itheima.springbootmybatisquickstart.pojo.Emp"><!--執行的SQL語句-->select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime, update_time updateTime from emp where name like concat('%',#{name},'%') and gender=#{gender} and entrydate between #{begin} and #{end}</select>
</mapper>
二、動態SQL
1.<if>
用于判斷條件是否成立。使用test屬性進行條件判斷,如果條件為true,則拼接SQL
<!--執行的SQL語句-->select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime,update_time updateTime from emp where<if test="name!=null">name like concat('%',#{name},'%')</if><if test="gender!=null">and gender=#{gender}</if><if test="begin!=null and end!=null">and entrydate between #{begin} and #{end}</if>
2.<where>
where元素只會在子元素有內容的情況下才插入where語句。而且會自動去除子句的開頭的AND或OR
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace為Mapper接口的全限定名-->
<mapper namespace="com.itheima.springbootmybatisquickstart.mapper.EmpMapper"><!--id為函數名,resultType為返回的對象的全限定名--><select id="IndistinctSelect" resultType="com.itheima.springbootmybatisquickstart.pojo.Emp"><!--執行的SQL語句-->select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime,update_time updateTime from emp<where><if test="name!=null">name like concat('%',#{name},'%')</if><if test="gender!=null">and gender=#{gender}</if><if test="begin!=null and end!=null">and entrydate between #{begin} and #{end}</if></where></select>
</mapper>
3.<set>
動態地在行首插入SET關鍵字,并會刪除額外的逗號。(用在update語句中)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace為Mapper接口的全限定名-->
<mapper namespace="com.itheima.springbootmybatisquickstart.mapper.EmpMapper"><update id="update">update emp<set><if test="username!=null">username=#{username},</if><if test="name!=null">name=#{name},</if><if test="gender!=null">gender=#{gender},</if><if test="image!=null">image=#{image},</if><if test="job!=null">job=#{job},</if><if test="entrydate!=null">entrydate=#{entrydate},</if><if test="deptId!=null">dept_id=#{deptId},</if><if test="updateTime!=null">update_time=#{updateTime}</if></set>where id = #{id}</update>
</mapper>
4.<foreach>
?
5.<sql>
定義可重復的SQL片段
<sql id="commonSelect">select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime,update_time updateTime from emp</sql>
6.<include>
?通過屬性refid,指定包含的sql片段
<include refid="commonSelect"/>