Mybatis 基础知识
properties
使用方式有三种:
1:
在properties 属性中增加 property 属性,用来设置一些属性的name, value
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
2:
引入外部配置类
<properties resource="db.properties" />
3:
Java 代码中使用,获取配置文件中的属性
String resource = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(is);
String userName = properties.getProperty("db.username");
String pwd = properties.getProperty("db.pwd");
properties.setProperty("db.username", CyperTool.decodeByBase64(userName));
settings
<settings>
<setting name="cacheEnabled" value="true" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
<setting name="logImpl" value="STDOUT_LOGGING" /> //
</settings>
- cacheEnabled // 全局开启或者关闭缓存, true/false
- lazyLoadingEnabled // 全局懒加载开关
- logimpl // 指定 mybatis 日志的具体实现,未指定时将自动查找
Mybatis 分页
1. limit
2. 利用 RowBounds
注解
1. 绑定接口
public interface UserMapper {
@Select("select * from user")
List<User> findAll();
}
多个参数 @Param
只能在元素参数上使用
Public User selectUser(@param(“userName”)String name,@param(“userArea”)String area);
<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>
动态 SQL
根据不同的条件生成不同的 SQL, Mybatis 的动态SQL 基于 OGNL 表达式
if
<select id="dynamicIfTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 1 = 1
<if test="title != null">
and title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
and owner = #{owner}
</if>
</select>
choose
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 1 = 1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
</select>
trim
<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">
select * from t_blog
<trim prefix="where" prefixOverrides="and |or">
<if test="title != null">
title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
or owner = #{owner}
</if>
</trim>
</select>
where
<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">
select * from t_blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
and owner = #{owner}
</if>
</where>
</select>
set
<update id="dynamicSetTest" parameterType="Blog">
update t_blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="content != null">
content = #{content},
</if>
<if test="owner != null">
owner = #{owner}
</if>
</set>
where id = #{id}
</update>
foreach
对集合进行遍历
<select id="dynamicForeachTest" resultType="Blog">
select * from t_blog where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
MyBatis 缓存
经常查询的数据存在内存中的临时数据,为了减少与数据库的交互
两级缓存。默认只开启一级缓存(sqlsession 级别的,也成为本地缓存),
二级缓存需要手动开启,它是基于nameSpace 级别的缓存,
Mybatis 提供了 Cache 接口,可以通过它来自定义二级缓存
一级缓存
也叫本地缓存,与数据库同一次查询期间(SqlSession 开启到关闭期间)缓存到本地缓存,下次查询相同数据,直接获取
缓存失效的情况
- 查询不同的东西
- 增删改
- 关闭清楚缓存, sqlSession.clearCache()
二级缓存
<!--开启二级缓存 -->
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
其次在 UserMapper.xml 文件中开启缓存
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
Mybatis 的缓存机制原理
第一次查询先看二级缓存,再看一级缓存,都没有走数据库,放入一级缓存;
Spring 整合 Mybatis
步骤:
-
- 导入相关 jar 包
- junit
- mybatis
- mysql 数据库
- Spring 相关
- aop 织入
- mybatis-spring
- 导入相关 jar 包
-
- 编写配置文件