联合查询情况下:
- 使用级联属性封装联合查询后的结果
<!--自定义封装规则,使用级联属性封装联合查询出来的结果-->
<resultMap id="myKey" type="com.hy.bean.Key">
<id property="id" column="id"></id>
<result property="keyName" column="keyName"></result>
<result column="lid" property="lock.id"></result>
<result property="lock.lockName" column="lockName"></result>
</resultMap>
在key对象中有一个lock字段,对应这一个lock对象 在赋值的时候可以使用联合查询和级联的方式为lock中的每一条数据赋值
如: 将查到的lock对象的id赋值<result column="lid" property="lock.id"></result>
- 使用association标签
<!--采用mybtis推荐的方式-->
<resultMap id="myKey" type="com.hy.bean.Key">
<id property="id" column="id"></id>
<result property="keyName" column="keyName"></result>
<!--接下来的属性是一个对象,自定义这个对象的封装规则 使用association表示联合了一个对象-->
<!--javaType表示指定这个对象的类型-->
<association property="lock" javaType="com.hy.bean.Lock">
<!--定义lock属性对应的lock的对象如何封装-->
<id property="id" column="lid"></id>
<result property="lockName" column="lockName"></result>
</association>
</resultMap>
- 使用collection标签
<select id="getLockByID" resultMap="mylock" >
select l.id lid ,l.lockName, k.id ,k.keyName,k.lockId from t_lock l LEFT JOIN t_key k ON k.lockId=l.id WHERE l.id=#{id};
</select>
<resultMap id="mylock" type="com.hy.bean.Lock">
<id property="id" column="lid"></id>
<result property="lockName" column="lockName"></result>
<!-- ofType指定集合里面元素的类型 -->
<collection property="keys" ofType="com.hy.bean.Key">
<id property="id" column="id"></id>
<result property="keyName" column="keyName"></result>
</collection>
</resultMap>
4.使用select属性进行分步查询
<!--简单查询-->
<select id="getKeyByIdSimple" resultMap="mykey02" >
select id , keyName ,lockId from t_key where id=#{id}
</select>
<resultMap id="mykey02" type="com.hy.bean.Key">
<id property="id" column="id"></id>
<result property="keyName" column="keyName"></result>
<!--告诉mybatis我们去执行一个子查询
select="" ;指定一个查询sql的唯一标识(namespace+id),mybtis自动调用指定的sql将查出的lock封装起来,
需要传入的参数 用column=""传入
-->
<association property="lock" select="com.hy.dao.LockDao.getLockByIdSimple" column="lockId"></association>
</resultMap>
//collection标签也可以使用分步查询
在<association>标签中使用select指定一个方法,mybtis会自动帮我们执行这方法,得到数据会自动封装到lock对象中.
总共执行了两次sql语句 (性能浪费)
image.png
5.按照需要加载和延时加载
//需要的时候再去加载
//延时加载:不着急加载(查询对象)
设置
<settings>
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
image.png
image.png
动态sql
<foreach>标签
<!--foreach标签-->
<select id="getTeacherByIdIn" resultType="com.hy.bean.Teacher">
select * from teacher where id in
<foreach collection="ids" item="id_item" separator="," open="(" close=")">
#{id_item}
</foreach>
</select>
<!--close="" 以什么结束
index="i" 索引
如果遍历的是一个list index指定的变量就保存了索引
如果遍历的是一个map index就是就保存了遍历的当前元素的key
item就是保存了当前元素的值
item="变量名" 每一次遍历的元素起一个变量名 方便使用
open="" 以什么开始
separator="" 每次元素遍历的分隔符-->
<set>标签
set标签会帮我们自动去掉最后一个sql语句中的","
<update id="updateTeacherByCondition">
update teacher
<set>
<if test="null!=teachername and !teachername.equals("")">
teacherName =#{teachername},
</if>
<if test="null!=className and !className.equals("")">
class_name =#{className},
</if>
<if test="null!=address and !address.equals("")">
address =#{address},
</if>
<if test="null!=brithDate ">
birth_date =#{brithDate},
</if>
</set>
<where>
id=#{id}
</where>
</update>
<trim>标签 帮我们去除
<!--根据条件查询老师-->
<select id="getTeacherByCondition" resultType="com.hy.bean.Teacher">
select * from teacher
<trim prefix="where" prefixOverrides="and">
<if test="null!=id and 0!=id">
id=#{id}
</if>
<if test="null!=teachername">
and teacherName like #{teachername}
</if>
<if test="null!=address">
and address=#{address}
</if>
</trim>
</select>
<!--
<trim suffix="" 为整体加一个后缀 suffixOverrides=""后面哪个字符多了帮我去除 ></trim>
prefix=""前缀 为我们下面的sql整体添加一个前缀
prefixOverrides="" 去除整体字符串前面
-->
OGNL对象导航图语言
在mybatis中传入的参数还可以用来做判断
还有额外的两个东西
_parameter : 代表传过来的参数
1)传入单个参数 _parameter表示这个参数
2)传入多个参数 _parameter 表示封装了参数的集合map
_databaseId 代表数据库的环境
<if test="_databaseId=='mysql'">