MyBatis?的?XML?配置文件和緩存使用步驟
MyBatis緩存介紹
正如大多數(shù)持久層框架一樣,MyBatis 同樣提供了一級緩存和二級緩存的支持
1.一級緩存: 基于PerpetualCache 的 HashMap本地緩存,其存儲作用域為 Session,當(dāng) Session flush 或 close 之后,該Session中的所有 Cache 就將清空。
2. 二級緩存與一級緩存其機制相同,默認(rèn)也是采用 PerpetualCache,HashMap存儲,不同在于其存儲作用域為 Mapper(Namespace),并且可自定義存儲源,如 Ehcache。
3. 對于緩存數(shù)據(jù)更新機制,當(dāng)某一個作用域(一級緩存Session/二級緩存Namespaces)的進行了 C/U/D 操作后,默認(rèn)該作用域下所有 select 中的緩存將被clear。
MyBatis 包含一個非常強大的查詢緩存特性,它可以非常方便地配置和定制。MyBatis 3 中的緩存實現(xiàn)的很多改進都已經(jīng)實現(xiàn)了,使得它更加強大而且易于配置。下面給大家詳細(xì)介紹 MyBatis 的 XML 配置文件和緩存使用步驟,內(nèi)容如下所示:
MyBatis的XML整體介紹
MyBatis 的 XML 配置文件結(jié)構(gòu)如下: configuration 配置 properties 屬性 settings 設(shè)置 typeAliases 類型命名 typeHandlers 類型處理器 objectFactory 對象工廠 plugins 插件 environments 環(huán)境 environment 環(huán)境變量 transactionManager 事務(wù)管理器 dataSource 數(shù)據(jù)源 databaseIdProviderchinese? mappers 映射器
一、整體配置文件介紹
<properties resource="jdbc.property">
<property name="password" value="123456"/>
</properties>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--其他地方可以使用${password}來引用這個變量-->
<properties resource="jdbc.property">
<property name="password" value="123456"/>
</properties>
<!--MyBatis設(shè)置信息-->
<settings>
<!--啟用延遲加載數(shù)據(jù)、cacheEnabled,lazyLoadingEnabled-->
<!--
1、延遲加載:用的時候就查詢、不用的時候并不會查詢
2、即使加載:不管你用不用、都會去數(shù)據(jù)庫查詢出來
-->
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<!--選擇日志、選擇后需要導(dǎo)入對應(yīng)的jar包和配置-->
<setting name="logImpl" value="log4j"/>
</settings>
<!--別名掃描包-->
<typeAliases>
<package name="package.mybatis.bean"/>
</typeAliases>
<!--數(shù)據(jù)源設(shè)置、可以設(shè)置多個數(shù)據(jù)源environment---
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--Mapper映射文件設(shè)置-->
<mappers>
<!--XML配置-->
<mapper resource="package/mybatis/mapper/StudentMapper.xml"/>
<!--單個接口配置-->
<mapper class="package.mybatis.dao.StudentDao"/>
<!--多個接口配置、包掃描模式、一次性配置package.mybatis.dao包下面的所有接口-->
<package name="package.mybatis.dao"/>
</mappers>
</configuration>二、Mybatis攔截器【不做要求】
| 攔截器名 | 作用 |
|---|---|
| Executor | 攔截執(zhí)行器的方法 |
| ParameterHandler | 攔截參數(shù)的處理 |
| ResultHandler | 攔截結(jié)果集的處理 |
| StatementHandler | 攔截Sql語法構(gòu)建的處理 |
實際上、Executor就能處理所有、其他的并不怎么好用
參考案例
//@Intercepts:標(biāo)識該類是一個攔截器
//@Signature:指明自定義攔截器需要攔截哪一個類型,哪一個方法;
// type:對應(yīng)四種類型中的一種;
// method:對應(yīng)接口中的哪類方法(因為可能存在重載方法);
// args:方法參數(shù)
@Intercepts({
@Signature(type = Executor.class, method = "update", args = { MappedStatement.class,Object.class }),
@Signature(type = Executor.class, method = "query", args = { MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class })
})
public class MyPlugin implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
if ("update".equals(invocation.getMethod().getName())) {
MappedStatement mstt = (MappedStatement) invocation.getArgs()[0];
Object object = invocation.getArgs()[1];
Student info=(Student)object;
//修改用戶sql語句的參數(shù)
info.setName("我不是漢武帝");
}
System.out.println("方法執(zhí)行");
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
}
<!--配置文件-->
<plugins>
<plugin interceptor="package.plugin.MyPlugin">
<property name="name" value="admin"/>
</plugin>
</plugins>三、緩存
一級緩存 session級別、【默認(rèn)開啟】、增刪改默認(rèn)刷新一級緩存(一定要commit哦)
一級緩存是SqlSession級別的緩存。在操作數(shù)據(jù)庫時需要構(gòu)造 sqlSession對象,mybatis 使用HashMap 來存儲緩存數(shù)據(jù)。不同的sqlSession之間的緩存數(shù)據(jù)區(qū)域(HashMap),所以是互不影響的
二級緩存【默認(rèn)是關(guān)閉、不常用】
是mapper級別的緩存,多個SqlSession去操作同一個Mapper的sql語句,多個SqlSession可以共用二級緩存,二級緩存是跨SqlSession的
使用步驟
1、修改mybatis-config.xml的setting設(shè)置
<setting name="cacheEnabled" value="true"/>
2、Mapper.XML:加入:<cache/>
3、緩存的bean要實現(xiàn)序列化接口 Serializable
4、一定要關(guān)閉第一個sqlSession
//SqlSession:連接對象Connection、mybatis的session并不是會話,指的就是Sql的Connection
SqlSession session1 = sqlSessionFactory.openSession();
SqlSession session2 = sqlSessionFactory.openSession();
//為了使用緩存、sql語句一定要統(tǒng)一規(guī)范
StudentDao dao1=session1.getMapper(StudentDao.class);
System.out.println("查詢一次數(shù)據(jù)");
List<Student> list1 = dao1.getStudentAll();
for (Student student : list1) {
System.out.println(student);
}
session1.close();
System.out.println("第二次查詢數(shù)據(jù)");
Thread.sleep(10000);
StudentDao dao2=session2.getMapper(StudentDao.class);
System.out.println("查詢一次數(shù)據(jù)");
List<Student> list2 = dao2.getStudentAll();
for (Student student : list2) {
System.out.println(student);
}到此這篇關(guān)于MyBatis 的 XML 配置文件和緩存的文章就介紹到這了,更多相關(guān)MyBatis XML 配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中@ConditionalOnProperty的使用及作用詳解
這篇文章主要介紹了SpringBoot中@ConditionalOnProperty的使用及作用詳解,@ConditionalOnProperty通過讀取本地配置文件中的值來判斷 某些 Bean 或者 配置類 是否加入spring 中,需要的朋友可以參考下2024-01-01
SpringMVC HttpMessageConverter消息轉(zhuǎn)換器
??HttpMessageConverter???,報文信息轉(zhuǎn)換器,將請求報文轉(zhuǎn)換為Java對象,或?qū)ava對象轉(zhuǎn)換為響應(yīng)報文。???HttpMessageConverter???提供了兩個注解和兩個類型:??@RequestBody,@ResponseBody???,??RequestEntity,ResponseEntity??2023-04-04

