Java:Mybatis拓展
一、动态Sql
1、if和where标签
多条件查询使用if标签,
where标签的作用:让where子句更加动态智能。
所有条件都为空时,where标签保证不会生成where子句;自动去除某些条件前面多余的and或or。
<select id="select" parameterType="banner" resultType="banner" >select * from banner<where><if test="bannerDec != null and bannerDec != ''">banner_dec = #{bannerDec}</if><if test="bannerStatus != null and bannerStatus != ''">banner_status = #{bannerStatus}</if></where></select>
2、set标签
主要使用在update语句当中,用来生成set关键字,同时去掉最后多余的“,”
<update id="update" parameterType="banner" >update banner<set><if test="bannerImg != null and bannerImg != ''">banner_img = #{bannerImg},</if><if test="bannerDec != null and bannerDec != ''">banner_dec = #{bannerDec},</if><if test="bannerStatus != null and bannerStatus != ''">banner_status = #{bannerStatus},</if></set>where banner_id = #{bannerId}</update>
3、choose when otherwise
<choose><when></when><when></when> <otherwise></otherwise>
</choose>
相当于if(){}else{}if(){}
4、foreach标签
循环数组或集合,动态生成sql;多数用在批量删除和添加
<delete id="deleteById">delete from banner where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach>
</delete>
<insert id="insert">insert into banner values <foreach collection="banners" item="banner" separator=",">(null,#{bannerDec}, #{bannerStatus})</foreach>
</insert>
5、sql标签和incluede标签
sql标签用来声明sql片段、include标签用来将声明的sql片段包含到某个sql语句当中
作用:代码复用、易维护。
二、Mybatis高级映射与延迟加载
1、多对一
①:级联属性映射、
②:association(关联)---修改resultMap中的配置:association即可。
<resultMap id="studentResultMap" type="Student"><id property="sid" column="sid"/><result property="sname" column="sname"/><association property="clazz" javaType="Clazz"><id property="cid" column="cid"/><result property="cname" column="cname"/></association>
</resultMap>
③:分布查询(用的多) ----可以实现延迟加载
<!--
select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlId)
column:将sql以及查询结果中的某个字段设置为分步查询的条件
-->
<association property="dept"
select="com.isluantic.mapper.DeptMapper.getEmpDeptByStep" column="did">
</association>
2、一对多
一对多的实现,通常是在一的一方中有List集合属性。
①:collection
<resultMap id="clazzResultMap" type="Clazz"><id property="cid" column="cid"/><result property="cname" column="cname"/><collection property="stus" ofType="Student"><id property="sid" column="sid"/><result property="sname" column="sname"/></collection>
</resultMap>
②:分布查询----可以实现延迟加载
<!--
select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlId)
column:将sql以及查询结果中的某个字段设置为分步查询的条件
-->
<association property="dept"
select="com.isluantic.mapper.DeptMapper.getEmpDeptByStep" column="did">
</association>
3、多对一延迟加载
延迟加载:需要在association标签中添加fetchType="lazy"
<association property="clazz" column="cid"fetchType="lazy"/>
全局延迟加载:开启全局延迟加载之后,所有的sql都会支持延迟加载。
<settings><setting name="lazyLoadingEnabled" value="true"/>
</settings>
不想全局加载,需要单独配置fetchType="eager",针对某个特定的sql,就关闭了延迟加载机制。
三、缓存机制
1、缓存的作用:
通过减少IO的方式,来提高程序的执行效率。
2、mybatis的缓存:
将select语句的查询结果放到缓存(内存)当中,下一次还是这条select语句的话,直接从缓存中取,不再查数据库。一方面是减少了IO。另一方面不再执行繁琐的查找算法。效率大大提升。
3、mybatis缓存包括:
①一级缓存:将查询到的数据存储到SqlSession中。
一级缓存默认是开启的。不需要做任何配置。
原理:只要使用同一个SqlSession对象执行同一条SQL语句,就会走缓存。
②二级缓存:将查询到的数据存储到SqlSessionFactory中。
二级缓存的范围是SqlSessionFactory。
使用二级缓存需要具备以下几个条件:
全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。默认就是true,无需设置。
<setting name="cacheEnabled" value="true">
在需要使用二级缓存的SqlMapper.xml文件中添加配置:<cache />。
使用二级缓存的实体类对象必须是可序列化的,也就是必须实现java.io.Serializable接口。
SqlSession对象关闭或提交后,一级缓存中的数据才会被写入到二级缓存中,二级缓存才可用。
4、一级缓存失效
①不同的SqlSession对象。
第一次查询和第二次查询之间,手动清空了一级缓存。
②:查询条件变化了。
第一次查询和第二次查询之间,执行了增删改操作。
这个增删改和哪张表没有关系,只要有insert delete update操作,一级缓存就失效。
缓存只针对于DQL语句,也就是说缓存机制只对应select语句。
5、二级缓存失效
二级缓存的失效:只要两次查询之间出现了增删改操作。二级缓存就会失效。一级缓存也会失效。
6、缓存顺序
四、分页功能
1、添加依赖
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.3.0</version></dependency>
2、在mybatis-config.xml里面添加组件
<plugins><!--设置分页插件--><plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>