動態sql與多表連接的案例
動態sql
MyBatis 的一個強大的特性之一通常是它的動態 SQL 能力, 如果你有使用 JDBC 或其他 相似框架的經驗,你就明白條件地串聯 SQL 字串在一起是多么的痛苦,確保不能忘了空 格或在串列的最后省略逗號,動態 SQL 可以徹底處理這種痛苦,
通常使用動態 SQL 不可能是獨立的一部分,MyBatis 當然使用一種強大的動態 SQL 語 言來改進這種情形,這種語言可以被用在任意映射的 SQL 陳述句中,
動態 SQL 元素和使用 JSTL 或其他相似的基于 XML 的文本處理器相似,在 MyBatis 之 前的版本中,有很多的元素需要來了解,MyBatis 3 大大提升了它們,現在用不到原先一半 的元素就能作業了,MyBatis 采用功能強大的基于 OGNL 的運算式來消除其他元素,
- if 判斷標簽 是否需要
- choose (when, otherwise) 與switch相同
- trim (where, set) trim修建,where set 相當于已創建的trim方法,
- where 去除前面的 and
- set去除后面的 ,
- foreach 自動拼接 開始open="( " 分隔 separator=" , " close=")"
if的使用實作表的多重查詢
介面方法:List<Emp> selectEmp(Emp emp);
<select id="selectEmp" resultType="Emp" parameterType="Emp">
select * from emp
where 1=1
<if test="empno != null">
and empno = #{empno}
</if>
<if test="deptno != null">
and deptno = #{deptno}
</if>
</select>
choose (when, otherwise)
介面方法:List<Emp> selectEmpByChoose(Emp emp);
<select id="selectEmpByChoose" resultType="ppl.com.pojo.Emp">
select * from emp where 1=1
<choose>
<when test="empno != null">
and empno = #{empno}
</when>
<when test="deptno != null">
and deptno = #{deptno}
</when>
</choose>
</select>
trim (where, set)
介面方法:List<Emp> selectEmpByTrim(Emp emp);
<select id="selectEmpByTrim" resultType="ppl.com.pojo.Emp">
select * from emp
<where>
<if test="empno != null">
and empno = #{empno}
</if>
<if test="deptno != null">
and deptno = #{deptno}
</if>
</where>
</select>
foreach 自動拼接( , )
item表示集合中每一個元素進行迭代時的別名,
index指 定一個名字,用于表示在迭代程序中,每次迭代到的位置,
open表示該陳述句以什么開始, separator表示在每次進行迭代之間以什么符號作為分隔 符,
close表示以什么結束,
介面方法: List<Emp> listEmp(Integer... deptnos);
<select id="listEmp" parameterType="java.util.ArrayList" resultType="emp">
select * from emp where
deptno in
<foreach collection="array" index="index" item="item"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
mybatis 多表連接
ResultMap:回傳結果映射,自定義映射,可以類屬性與資料庫列名不一致,但需要在映射中提現,
ResultType:回傳結果集,結果型別,使用的是默認映射,需要名稱相同,等于的是類或者基本屬性,
collection:一對多,配置連接的從表,需要的資料,
association:多對一,協議,配置連接屬性,需要連接的資料,
一對多 多對一
一對多
實作一對多的步驟:
- 在主表中添加從表的List集合
- 在主表的Mapper中配置ResultMap多表查詢的回傳值
- 使用查詢陳述句查詢回傳ResultMap
第一步:
public List<Emp> getEmps() {
return emps;
}
public void setEmps(List<Emp> emps) {
this.emps = emps;
}
第二步:
<!--一對多-->
<resultMap id="DeptEmp" type="Dept">
<id column="deptno" property="deptno"/>
<result column="dname" property="dname"/>
<result column="loc" property="loc"/>
<!--實作次表的存盤-->
<collection property="emps" ofType="Emp" column="deptno" select="ppl.com.mapper.MapperEmp.SelectEmpByDeptno"></collection>
</resultMap>
第三步:
<select id="findAll" resultMap="DeptEmp">
select * from dept
</select>

多對一,一對一:兩者了解一個就可以,建議一對多
- 在主表中添加從表的物件
- 在主表的Mapper中配置ResultMap多表查詢的回傳值
- 使用查詢陳述句查詢回傳ResultMap
第一步:
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
第二步:
<resultMap id="EmpDept" type="emp">
<id column="empno" property="empno"/>
<result column="ename" property="ename"/>
<result column="job" property="job"/>
<result column="mgr" property="mgr"/>
<result column="hireDate" property="hireDate"/>
<result column="sal" property="sal"/>
<result column="comm" property="comm"/>
<result column="deptno" property="deptno"/>
<association property="dept" column="deptno" javaType="dept" select="ppl.com.mapper.MapperDept.findByDeptNo" fetchType="eager"/>
</resultMap>
第三步:
<select id="allEmp" resultMap="EmpDept">
select * from emp
</select>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/205117.html
標籤:其他
