Mybatis SQL動態處理
Where 、if
-
where標簽:相當于sql語法中的where -
if判斷標簽test判斷條件 ,滿足條件,執行if標簽中sql陳述句
<select id="findEmpByCondition" resultType="emp">
select * from emp
<where>
<if test="empno != null">
and empno= #{empno}
</if>
</where>
</select>
choose 、 when、otherwise
-
choose選擇標簽 -
when: 相當于 java 中case語法 -
otherwise:when標簽都不滿足,執行otherwise標簽中內容
<select id="findEmpByCondition2" resultType="emp">
select * from emp
<where>
<choose>
<when test="empno != null">
and empno= #{empno}
</when>
<when test="ename != null and ename != ''">
and ename= #{ename}
</when>
<when test="job != null and job != ''">
and job= #{job}
</when>
<when test="mgr != null ">
and mgr= #{mgr}
</when>
<when test="hiredate != null ">
and hiredate= #{hiredate}
</when>
<when test="sal != null">
and sal= #{sal}
</when>
<when test="comm != null ">
and comm =#{comm}
</when>
<when test="deptno != null ">
and deptno= #{deptno}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</where>
</select>
set
set 代替sql中set 語法
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
trim
trim標簽 增加前綴 、后綴 , 洗掉前綴、后綴
trim標簽中屬性:
-
prefix 增加前綴
-
suffix 增加后綴
-
prefixOverrides 洗掉前綴
-
suffixOverrides 洗掉后綴
<update id="updateAuthorIfNecessary">
update Author
<trim prefix="SET" suffixOverrides=",">
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio},</if>
</trim>
where id=#{id}
</update>
foreach
coollection要迭代的集合,如:list 、 set 、map 、arrayitem迭代臨時變數值index當前迭代序號open以什么開頭后開始遍歷close以什么后結束遍歷
<delete id="delectById">
delete from project where project_id in
<foreach collection="array" item="project_id" index="index"
open="(" separator="," close=")">
#{project_id}
</foreach>
</delete>
bind
bind運算式以外創建一個變數,并將其系結到當前的背景關系
<select id="findAll2" resultMap="selectAll3">
SELECT * FROM orders
<where>
<if test="order_name !=null and order_name != ''">
<bind name="tn" value="'%'+order_name+'%'" />
order_name like #{tn}
</if>
</where>
</select>
sql片段
將sql陳述句共同的部分抽出來形成一個獨立的片段,哪里需要該片段就參考該片段
<sql id="empColumns">
empno,ename,job,mgr,hiredate,sal,comm,deptno
</sql>
<sql id="baseEmpQuery">
select
<include refid="empColumns"/>
from emp
</sql>
主鍵自增回填
方式1
-
useGeneratedKeys=“true” 表示要使用自增的主鍵
-
keyProperty=“deptno” 表示把自增的主鍵賦給JavaBean的哪個成員變數,
使用物件get方法可以獲取主鍵
<insert id="addDept" parameterType="dept" useGeneratedKeys="true"keyProperty="deptno" >
insert into dept values(default,#{dname},#{loc})
</insert>
方式2
- order:取值AFTER|BEFORE,表示在新增之后|之前執行
selectKey標簽中的SQL命令 - keyProperty: 執行select @@identity后結果填充到哪個屬性中
- resultType: 結果型別
<insert id="addDept2" parameterType="dept">
<selectKey order="AFTER" keyProperty="deptno" resultType="int">
select @@identity
</selectKey>
insert into dept values(null,#{dname},#{loc})
</insert>
模糊查詢
方式1: 使用concat() 函式,對模糊條件進行拼接
<select id="getByName" parameterType="string" resultType="emp">
select * from emp where ename like concat( '%',#{ename},'%')
</select>
方式2:使用bind標簽
<select id="selectMore" resultMap="BaseResultMap2">
select * from user
<where>
<if test="name!=null and name!=''">
<bind name="li" value="'%'+project.projectName+'%'"/>
name like #{li}
</if>
<if test="age!=null and age!=''">
<bind name="age" value="'%'+age+'%'"/>
and age=#{age}
</if>
</where>
</select>
resultMap 映射處理
單表映射關系處理
- id 表示表主鍵欄位
- result 表示表普通欄位
屬性
-
type 回傳型別
-
property 對應javaBean 屬性
-
column 對應表欄位 或 對映射表
-
javaType表映射物件型別
<resultMap id="empMap" type="emp">
<id property="empno" column="empno"></id>
<result property="ename" column="ename"></result>
<result property="job" column="job"></result>
<result property="sal" column="sal"></result>
<result property="hiredate" column="hiredate"></result>
<result property="mgr" column="mgr"></result>
<result property="comm" column="comm"></result>
<result property="deptno" column="deptno"></result>
</resultMap>
一對一映射關系處理
標簽
- id 表示表主鍵欄位
- result 表示表普通欄位
association表連接一對一關系映射
屬性
-
type 回傳型別
-
property 對應javaBean 屬性
-
column 對應表欄位 或 對映射表
-
javaType表映射物件型別
<!--使用resultMap定義一對一映射關系-->
<resultMap id="empJoinDept" type="emp">
<!--處理emp原有8個屬性 不能省略-->
<id property="empno" column="empno"></id>
<result property="ename" column="ename"></result>
<result property="job" column="job"></result>
<result property="sal" column="sal"></result>
<result property="hiredate" column="hiredate"></result>
<result property="mgr" column="mgr"></result>
<result property="comm" column="comm"></result>
<result property="deptno" column="deptno"></result>
<!--將deptno dname loc 映射景dept屬性
association 處理一對一映射關系
property 給物體類屬性賦值
javaType 要映射的屬性是哪個類
-->
<association property="dept" javaType="dept" >
<!--處理一對一屬性映射-->
<id property="deptno" column="deptno"></id>
<result property="dname" column="dname"></result>
<result property="loc" column="loc"></result>
</association>
</resultMap
<select id="findByJoinDept" resultMap="empJoinDept">
select * from emp e
left outer join dept d
on e.deptno=d.deptno where empno=#{empno}
</select>
一對多映射關系處理
標簽
- id 表示表主鍵欄位
- result 表示表普通欄位
collection表連接多對多關系映射
屬性
-
type 回傳型別
-
property 對應javaBean 屬性
-
column 對應表欄位 或 對映射表
-
javaType表映射物件型別
<resultMap id="empJoinPs" type="emp" >
<id property="empno" column="empno"></id>
<result property="ename" column="ename"></result>
<result property="job" column="job"></result>
<result property="sal" column="sal"></result>
<result property="hiredate" column="hiredate"></result>
<result property="mgr" column="mgr"></result>
<result property="comm" column="comm"></result>
<result property="deptno" column="deptno"></result>
<collection property="projectrecords" ofType="projectrecords">
<id property="empno" column="empno"></id>
<id property="pid" column="pid"></id>
</collection>
</resultMap>
<select id="getEmpPs" resultMap="empJoinPs">
select * from emp e
join projectrecords s
on e.empno=s.empno
where e.empno=#{empno}
</select>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/203872.html
標籤:其他
