注:本文是对本人的Mybatis入门(中)的补充,或者说进阶,是在程序跑起来的基础上进行。
关于Mybatis的入门教学
关于动态sql
MyBatis的映射文件中支持在基础SQL上添加一些逻辑操作,并动态拼接成完整的SQL之后再执行,以达到SQL复用、简化编程的效果。
1.sql片段
<mapper namespace="com.it.dao.UserDao">
<sql id="BaseSql">
select id,name,password
</sql>
<!--配置查询所有-->
<select id="findAll" resultType="user">
<!-- 引入sql片段 -->
<include refid="BaseSql"></include>
FROM t_user
</select>
</mapper>
sql标签可将重复出现的代码定义为一个sql片段,从而直接引用。
2.if判断
<select id="findByUser" resultType="com.it.entity.User">
<include refid="BaseSql"></include>
FROM t_user
where
<!-- 对user对象的name属性进行判断,如果不为空则添加这条判断,为空则跳过 -->
<if test="name!=null and name!=''">
name=#{name}
</if>
<if test="password!=null">
password=#{password}
</if>
</select>
if判断可用于多条件查询.
3.where条件
<select id="findByUser" resultType="com.it.entity.User">
<include refid="BaseSql"></include>
FROM t_user
<where>
<!-- WHERE,会自动忽略前后缀(如:and | or) -->
<if test="name!=null">
name=#{name}
</if>
<if test="password!=null">
and password=#{password}
</if>
</where>
</select>
4.set
<update id="UpdateByUser">
update t_user
<set>
<if test="name!=null">
name=#{name},
</if>
<if test="password!=null">
password=#{password}
</if>
</set>
<where>
<if test="id!=null">
id=#{id}
</if>
</where>
</update>
在set标签中,如果在结尾有逗号的话,会直接去掉。
5.trim
<select id="findByUser" resultType="com.it.pojo.User">
<include refid="BaseSql"></include>
FROM t_user
<trim prefix="WHERE" prefixOverrides="AND|OR"> <!-- 增加WHERE前缀,自动忽略前缀 -->
<if test="name!=null">
and name=#{name}
</if>
<if test="password!=null">
and password=#{password}
</if>
</trim>
</select>
<update id="UpdateByUser">
update t_user
<trim prefix="SET" suffixOverrides=","> <!-- 增加SET前缀,自动忽略后缀 -->
<if test="name!=null">
name=#{name},
</if>
<if test="password!=null">
password=#{password},
</if>
</trim>
<where>
<if test="id!=null">
id=#{id}
</if>
</where>
</update>
< trim prefix="" suffix="" prefixOverrides="" suffixOverrides="" >代替< where > ,< set >
6.foreach
<!-- 多个id查询 -->
<!--collection="list"-->
<select id="findUserByIds" resultType="user">
<include refid="BaseSql"></include>
from t_user
<where>
id in
<foreach collection="array" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</where>
</select>
缓存(Cache)
一级缓存
SqlSession级别的缓存,同一个SqlSession的发起多次同构查询,会将数据保存在一级缓存中。
- 注意:无需任何配置,默认开启一级缓存。
@Test
public void testfindById()throws Exception {
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSession session = factory.openSession();
//---------------------------------------------
UserDao userDao = session.getMapper(UserDao.class);
//证明SqlSession级别的一级缓存存在
User user1 = userDao.findById(1);
//当调用SqlSession的修改,添加,删除,commit(),close()等方法时也会清空一级缓存。
//session.clearCache();//清空缓存
//session.close();
//session = factory.openSession();
//userDao = session.getMapper(UserDao.class);
User user2 = userDao.findById(1);
System.out.println(user1 == user2);
//---------------------------------------------
session.close();
in.close();
}
二级缓存
SqlSessionFactory级别的缓存,同一个SqlSessionFactory构建的SqlSession发起的多次同构查询,会将数据保存在二级缓存中。
- 注意:在sqlSession.commit()或者sqlSession.close()之后生效。
开启缓存
<configuration>
<properties .../>
<!-- 注意书写位置 -->
<settings>
<setting name="cacheEnabled" value="true"/> <!-- mybaits-config.xml中开启全局缓存(默认开启) -->
</settings>
<typeAliases></typeAliases>
</configuration>
指定Mapper缓存
<mapper namespace="com.it.dao.UserDao">
<cache /> <!-- 指定缓存 -->
<!-- 查询单个对象 -->
<select id="findById" resultType="com.it.pojo.User" >
select id,name,password FROM t_user where id=#{id}
</select>
</mapper>
测试
@Test
public void testfindById2() throws Exception {
//1.读取配置文件
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
//3.使用工厂生产SqlSession对象
SqlSession sqlSession1 = factory.openSession();
UserDao dao1 = sqlSession1.getMapper(UserDao.class);
User user1 = dao1.findById(1);
System.out.println(user1);
sqlSession1.close();//一级缓存消失
SqlSession sqlSession2 = factory.openSession();
UserDao dao2 = sqlSession2.getMapper(UserDao.class);
User user2 = dao2.findById(1);
System.out.println(user2);
sqlSession2.close();
in.close();
}