文章目錄
一.select查詢返回List/Map
1.查詢多條數據返回List
如果使用的是自動映射 , resultType指定的是集合中元素的類型。或者說
理解爲想讓Mybatis將一條數據封裝成哪種類型的java對象。就指定成哪種類型
2.查詢返回Map
- a.查詢一條數據返回Map,MyBatis會自動使用表的列名作爲key,列值作爲value返回.
- b.查詢多條數據返回Map,需要使用@MapKey(“key”)指定封裝map的時候所使用的key
Map中的value就是 一條數據封裝成的java對象
接口中:
public Map<String,Object> getEmpsByIdReturnMap(Integer id);
配置文件中:
<!--public Map<String,Object> getEmpsByIdReturnMap(Integer id); resultType返回的類型與封裝的類型要一致-->
<select id="getEmpsByIdReturnMap" resultType="map">
select * from tb1_employee where id=#{id}
</select>
測試:
public void testGetEmpsByIdNameReturnMap()throws Exception{
SqlSessionFactory ssf = getSqlSessionFactory();
SqlSession session = ssf.openSession();
try{
EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
Map<String,Object> map = employeeMapper.getEmpsByIdReturnMap(1004);
System.out.println(map);
}finally {
session.close();
}
}
二.resultMap 自定義映射
1. association 用來映射聯合屬性
接口:
//測試級聯外鍵,查詢員工的同時查詢員工所屬部門信息
public Employee getEmpAndDepart(Integer id);
配置文件:
<select id="getEmpAndDepart" resultMap="MyEmpDepart">
SELECT e.id eid,e.last_name,e.gender,e.email,d.id did,d.dept_name
FROM tb1_employee e,tb1_depart d
WHERE e.d_id = d.id AND e.id = #{id};
</select>
<resultMap id="MyEmpDepart" type="com.atguigu.mybatis.beans.Employee">
<id column="eid" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!--使用級聯的方式
<result column="did" property="depart.id"/>
<result column="dept_name" property="depart.departmentName"/>-->
<!--使用關聯映射-->
<association property="depart" javaType="com.atguigu.mybatis.beans.Department">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
</association>
</resultMap>
2.association使用分步查詢
接口:
public Employee getEmpAndDepartStep(Integer id);
員工配置文件:
<select id="getEmpAndDepartStep" resultMap="MyEmpDepartStep">
select * from tb1_employee where id=#{id}
</select>
<resultMap id="MyEmpDepartStep" type="com.atguigu.mybatis.beans.Employee">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!--使用association進行分步查詢-->
<association property="depart" javaType="com.atguigu.mybatis.beans.Department"
select="com.atguigu.mybatis.mapper.DepartmentMapper.getDepartById" column="d_id">
</association>
</resultMap>
部門配置文件:
<mapper namespace="com.atguigu.mybatis.mapper.DepartmentMapper">
<!--public Department getDepartById(Integer id);-->
<select id="getDepartById" resultType="com.atguigu.mybatis.beans.Department">
select id,dept_name departmentName from tb1_depart where id=#{id}
</select>
3.association使用分步查詢可以使用延遲加載
4.collection 用來映射集合類型的聯合屬性
部門配置文件:(查詢各部門下的員工信息)
<select id="getDeptAndEmps" resultMap="MyDeptAndEmps">
SELECT
d.id did,d.dept_name,e.id eid,e.last_name,e.gender,e.email
FROM tb1_depart d LEFT JOIN tb1_employee e
ON d.id = e.d_id
WHERE d.id=#{id};
</select>
<resultMap id="MyDeptAndEmps" type="com.atguigu.mybatis.beans.Department">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
<!--使用collection映射集合類型的關聯屬性-->
<collection property="emps" ofType="com.atguigu.mybatis.beans.Employee">
<id column="eid" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
</collection>
</resultMap>
collection使用分步查詢
collection分步查詢使用延遲加載
三.動態sql
- if: 用於條件的判斷. test中寫的OGNL表達式.
- where:用於解決拼裝sql的時候where 以及 前面多出或者少and的問題.
- trim: 用於解決拼裝sql的時候前面或者後面 缺少或者多出某些字符的問題
- set: 用於解決修改操作拼裝sql的時候,的問題.
- choose: 類似於帶了break的switch case語句. when可以出現多次 otherwise出現一次.
- foreach: 主要用於對集合的遍歷 . 使用foreach完成Mysql的批量操作.
if,where代碼:
<select id="getEmpsDynamicSql" resultType="com.atguigu.mybatis.beans.Employee">
select * from tb1_employee
<where>
<if test="id!=null">
and id=#{id}
</if>
<if test="lastName!=null && lastName!=" "">
and last_name=#{lastName}
</if>
<if test="email!=null and email.trim()!=''">
and email=#{email}
</if>
<if test="("m").equals(gender) or ("f").equals(gender)">
and gender=#{gender}
</if>
</where>
</select>
trim代碼:
trim:
- prefix: 前綴 給拼串後的整個字符串加一個前綴.
- prefixOverrides: 前綴覆蓋 給拼串後的整個字符串去掉前面多餘的字符
- suffix: 後綴 給拼串後的整個字符串加一個後綴
- suffixOverrides: 後綴覆蓋 給拼串後的整個字符串去掉後面多餘的字符
select * from tbl_employee
<trim prefix="where" suffixOverrides="and">
<!-- test: OGNL表達式 -->
<if test="id!=null">
id = #{id} and
</if>
<if test="lastName!=null && lastName!="" ">
last_name like #{lastName} and
</if>
<if test="email!=null and email.trim()!=''">
email = #{email} and
</if>
<if test="("m").equals(gender) or ("f").equals(gender) ">
gender = #{gender}
</if>
</trim>
set代碼:
<select id="getUpdateSet" resultType="com.atguigu.mybatis.beans.Employee">
update tb1_employee
<set>
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="gender!=null">
gender=#{gender}
</if>
</set>
where id=#{id}
</select>
choose代碼:
<select id="getEmpsByConditionChoose" resultType="com.atguigu.mybatis.beans.Employee">
select * from tb1_employee
<where>
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="lastName!=null">
last_name=#{lastName}
</when>
<otherwise>
gender='m'
</otherwise>
</choose>
</where>
</select>
foreach代碼:
<select id="getForeach" resultType="com.atguigu.mybatis.beans.Employee">
select * from tb1_employee where id in
<foreach collection="ids" item="curr_id" open="(" close=")" separator=",">
#{curr_id}
</foreach>
</select>
<!-- public void addEmps(@Param("emps")List<Employee> emps);-->
<insert id="addEmps">
insert into tb1_employee (last_name,gender,email) values
<foreach collection="emps" item="curr_emp" separator=",">
(#{curr_emp.lastName},#{curr_emp.gender},#{curr_emp.email})
</foreach>
</insert>
四.內置參數
- a._parameter: 代表整個參數.
如果是單個參數,則代表整個參數
如果是多個參數,MyBatis會封裝map,_parameter代表封裝後的map。 - b._databaseId:
如果配置了databaseIdProvider,_databaseId代表當前使用的數據庫的別名.
五.抽取可重用的sql
可以使用<sql id=""></sql> 將重複使用率高的sql語句抽取出來。方便多次使用.
在使用的地方可以用<include refid=""> 來引用已經抽取好的sql.
<!-- sql: 抽取可重用的sql語句-->
<sql id="selectSql">
select * from
</sql>
id值與refid一致