mybatis-ehcache-1.0.0-reference

合集下载

MyBatis中XML映射文件中常见的标签说明

MyBatis中XML映射文件中常见的标签说明

MyBatis中XML映射⽂件中常见的标签说明SQL 映射⽂件只有很少的⼏个顶级元素(按照应被定义的顺序列出):cache – 对给定命名空间的缓存配置。

cache-ref – 对其他命名空间缓存配置的引⽤。

resultMap – 是最复杂也是最强⼤的元素,⽤来描述如何从数据库结果集中来加载对象。

parameterMap – 已被废弃!⽼式风格的参数映射。

更好的办法是使⽤内联参数,此元素可能在将来被移除。

sql – 可被其他语句引⽤的可重⽤语句块。

insert – 映射插⼊语句update – 映射更新语句delete – 映射删除语句select – 映射查询语句select<select id="selectPerson" parameterType="int" resultType="hashmap">SELECT * FROM PERSON WHERE ID = #{id}</select>这个语句被称作 selectPerson,接受⼀个 int(或 Integer)类型的参数,并返回⼀个 HashMap 类型的对象,其中的键是列名,值便是结果⾏中的对应值。

注意参数符号:#{id}这就告诉 MyBatis 创建⼀个预处理语句(PreparedStatement)参数,在 JDBC 中,这样的⼀个参数在 SQL 中会由⼀个“?”来标识,并被传递到⼀个新的预处理语句中,就像这样:// 近似的 JDBC 代码,⾮ MyBatis 代码...String selectPerson = "SELECT * FROM PERSON WHERE ID=?";PreparedStatement ps = conn.prepareStatement(selectPerson);ps.setInt(1,id);<selectid="selectPerson"parameterType="int"parameterMap="deprecated"resultType="hashmap"resultMap="personResultMap"flushCache="false"useCache="true"timeout="10"fetchSize="256"statementType="PREPARED"resultSetType="FORWARD_ONLY">属性描述id在命名空间中唯⼀的标识符,可以被⽤来引⽤这条语句。

Mybatis与Ehcache整合

Mybatis与Ehcache整合

Mybatis与Ehcache整合Mybatis与Ehcache整合可以提高性能,降低数据库压力。

查询百度发现整合Mybatis与Ehcache 其实非常简单的。

1.下载mybatis相关包与ehcache相关包下载地址为:https://github./mybatis/ehcache-cache/releases作者下载的是mybatis-ehcache-1.0.3版本其中自带了mybatis-ehcache-1.0.3.jarehcache-core-2.6.8.jarslf4j-api-1.6.1.jar将这三个包导入到项目的jar文件下,就可以使用ehcache功能了。

当然前提是保证mybatis没有引入ehcache前,项目也能正常运行。

作者用的mybatis3.2版本,框架用了SpringMVC3.2等等。

现在就试试效果吧。

在Map文件中打开echached效果,account-mapper.xml文件内容如下,(有的文章说还需要配置ehcache.xml,作者测试根本不需要这个配置):<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN""/dtd/mybatis-3-mapper.dtd"><mapper namespace=".springdemo.mapper.AccountMapper"><!--mybatis ehcache缓存配置--><!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志--><cache type="org.mybatis.caches.ehcache.LoggingEhcache" /><cache type="org.mybatis.caches.ehcache.EhcacheCache"/><!-- 以下与实体类的中字段一致--><sql id="selectId">id,accountName,(select group_concat(name) from ly_rolewhere ly_role.idin (SELECT role_id FROM acc_role WHEREacc_id=account.id) ) roleName,password,description,state,createTime</sql><!--resultType="Account" 每返回一条结果封装到Account里--><select id="queryAll" resultType="Account" parameterType="Account"> select<include refid="selectId" />from account<where><if test="accountName != null and accountName != ''">accountName like '%${accountName}%'</if></where></select><select id="isExist" resultType="Account" parameterType="String"> select <include refid="selectId" />from accountwhere accountName = #{accountName}</select><!--resultType="Account" 每返回一条结果封装到Account里--><select id="query" resultType="Account" parameterType="Java.util.HashMap"> select<include refid="selectId" />from account<where><if test="t.accountName != null and t.accountName != ''">accountName like '%${t.accountName}%'</if></where><if test="paging.startPage != null ">limit #{paging.startPage} , #{paging.pageSize}</if></select><!--resultType="Account" 记录条数,用于翻页查询--><select id="queryCount" resultType="long" parameterType="java.util.HashMap"> select count(*)from account<where><if test="t.accountName != null and t.accountName != ''">accountName like '%${t.accountName}%'</if></where></select><select id="queryNoMatch" resultType="Account" parameterType="java.util.HashMap"> selecta.id,a.accountName,a.password,a.accountType, a.description,a.state,a.createTime,(SELECT from department dp where dp.id =d.subdep_id) depNamefrom account a LEFT JOIN dep_account d ona.id=d.account_id<where><if test="t.accountName != null and t.accountName != ''"> accountName like '%${t.accountName}%'</if></where></select><!-- 增加用户--><insert id="add" parameterType="Account">insert into account (accountName,password,description,state )values (#{accountName},#{password}, #{description},#{state})</insert><delete id="delete" parameterType="String">delete from account whereid=#{id}</delete><select id="getById" parameterType="String" resultType="Account"> select<include refid="selectId" />from account where id=#{id}</select> <update id="update" parameterType="Account"> update account<set><if test="accountName != null and accountName != ''"> accountName=#{accountName},</if><if test="password != null and password != ''">password=#{password},</if><if test="description != null and description != ''">description=#{description},</if><if test="state != null and state != ''">state=#{state},</if><if test="createTime != null and createTime != ''">createTime=#{createTime}</if></set>where id=#{id}</update><!-- 验证用户登陆--><select id="countAccount" parameterType="Account" resultType="Account"> select<include refid="selectId" />from account whereaccountName=#{accountName} and password=#{password}</select><!-- 根据用户名查出id --><select id="querySingleAccount" parameterType="String"resultType="Account">select<include refid="selectId" />from account where accountName=#{accountName} </select></mapper>然后运行Account的查询程序,未启用cache的日志如下:DEBUG 2014-10-11 14:07:16,712org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 DispatcherServlet with name 'spring3' processing GET request for [/AnnExp2/background/account/list.do] DEBUG 2014-10-11 14:07:16,712org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-1 Looking up handler method for path /background/account/list.do DEBUG 2014-10-11 14:07:16,722org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-1 Returning handler method [publicng.String ermgr.controller.AccountController.list(org.springframework.ui.Model,ermgr.vo.Resources,ng.String)]DEBUG 2014-10-11 14:07:16,722org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-1 Returning cached instance of singleton bean 'accountController'DEBUG 2014-10-11 14:07:16,722org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 Last-Modified value for [/AnnExp2/background/account/list.do] is: -13DEBUG 2014-10-11 14:07:16,742org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-1 Invoking afterPropertiesSet() on bean with name 'background/account/list'DEBUG 2014-10-11 14:07:16,742org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 Rendering view [org.springframework.web.servlet.view.JstlView: name 'background/account/list'; URL[/WEB-INF/view/background/account/list.jsp]] in DispatcherServlet with name 'spring3' DEBUG 2014-10-11 14:07:16,742org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-1 Added model object'resources' of type [ermgr.vo.Resources] to request in view with name'background/account/list'DEBUG 2014-10-11 14:07:16,742org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-1 Added model object'org.springframework.validation.BindingResult.resources' of type[org.springframework.validation.BeanPropertyBindingResult] to request in view with name'background/account/list'DEBUG 2014-10-11 14:07:16,742org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-1 Forwarding to resource [/WEB-INF/view/background/account/list.jsp] in InternalResourceView'background/account/list'2DEBUG 2014-10-11 14:07:16,762org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 Successfully pleted requestDEBUG 2014-10-11 14:07:16,762org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-1 Returning cached instance of singleton bean 'sqlSessionFactory'DEBUG 2014-10-11 14:07:16,995org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 DispatcherServletwith name 'spring3' processing POST request for [/AnnExp2/background/account/query.do] DEBUG 2014-10-11 14:07:16,995org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-1 Looking up handler method for path /background/account/query.do DEBUG 2014-10-11 14:07:17,005org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-1 Returning handler method[public .lanyuan.pulgin.mybatis.plugin.PageView ermgr.controller.AccountCont roller.query(ermgr.vo.Account,ng.String,ng.String)]DEBUG 2014-10-11 14:07:17,005org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-1 Returning cached instance of singleton bean 'accountController'DEBUG 2014-10-11 14:07:17,005 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1 Creating a new SqlSessionDEBUG 2014-10-11 14:07:17,005 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession1a891095] was not registered for synchronization because synchronization is not activeDEBUG 2014-10-11 14:07:17,049 .springdemo.mapper.AccountMapper.http-bio-8080-exec-1 Cache Hit Ratio [.springdemo.mapper.AccountMapper]: 0.0DEBUG 2014-10-11 14:07:17,050 org.springframework.jdbc.datasource.DataSourceUtils.http-bio-8080-exec-1 Fetching JDBC Connection from DataSourceDEBUG 2014-10-11 14:07:17,051org.mybatis.spring.transaction.SpringManagedTransaction.http-bio-8080-exec-1 JDBC Connection [jdbc:MySQL://localhost:3306/test, UserName=rootlocalhost, mysql-AB JDBC Driver] will not be managed by SpringDEBUG 2014-10-1114:07:17,051 .springdemo.mapper.AccountMapper.queryCount.http-bio-8080-exec-1==> Preparing: select count(*) from accountDEBUG 2014-10-1114:07:17,051 .springdemo.mapper.AccountMapper.queryCount.http-bio-8080-exec-1 ==> Parameters:DEBUG 2014-10-1114:07:17,052 .springdemo.mapper.AccountMapper.queryCount.http-bio-8080-exec-1<== Total: 1DEBUG 2014-10-11 14:07:17,053 net.sf.ehcache.store.disk.Segment.http-bio-8080-exec-1 put added 0 on heapDEBUG 2014-10-11 14:07:17,053 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1Closing non transactional SqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession1a891095]DEBUG 2014-10-11 14:07:17,053org.springframework.jdbc.datasource.DataSourceUtils.http-bio-8080-exec-1 Returning JDBC Connection to DataSourceDEBUG 2014-10-11 14:07:17,053 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1 Creating a new SqlSessionDEBUG 2014-10-11 14:07:17,054 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession271ffe2f] was not registered for synchronization because synchronization is not activeDEBUG 2014-10-11 14:07:17,058 .springdemo.mapper.AccountMapper.http-bio-8080-exec-1 Cache Hit Ratio [.springdemo.mapper.AccountMapper]: 0.0DEBUG 2014-10-11 14:07:17,058 org.springframework.jdbc.datasource.DataSourceUtils.http-bio-8080-exec-1 Fetching JDBC Connection from DataSourceDEBUG 2014-10-11 14:07:17,059org.mybatis.spring.transaction.SpringManagedTransaction.http-bio-8080-exec-1 JDBC Connection [jdbc:mysql://localhost:3306/test, UserName=rootlocalhost, MySQL-AB JDBC Driver] will not be managed by SpringDEBUG 2014-10-1114:07:17,060 .springdemo.mapper.AccountMapper.query.http-bio-8080-exec-1==> Preparing: select id, accountName, (select group_concat(name) from ly_role wherely_role.id in (SELECT role_id FROM acc_role WHERE acc_id=account.id) ) roleName, password, description, state, createTime from account limit ? , ?DEBUG 2014-10-1114:07:17,060 .springdemo.mapper.AccountMapper.query.http-bio-8080-exec-1 ==> Parameters: 0(Integer), 5(Integer)DEBUG 2014-10-1114:07:17,066 .springdemo.mapper.AccountMapper.query.http-bio-8080-exec-1 <== Total: 5DEBUG 2014-10-11 14:07:17,066 net.sf.ehcache.store.disk.Segment.http-bio-8080-exec-1 put added 0 on heapDEBUG 2014-10-11 14:07:17,067 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-1 Closing non transactional SqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession271ffe2f]DEBUG 2014-10-11 14:07:17,067org.springframework.jdbc.datasource.DataSourceUtils.http-bio-8080-exec-1 Returning JDBC Connection to DataSourceDEBUG 2014-10-11 14:07:17,078org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProc essor.http-bio-8080-exec-1 Written [.lanyuan.pulgin.mybatis.plugin.PageView380b2e5d] as "application/json;charset=UTF-8" using[org.springframework.http.converter.json.MappingJacksonHttpMessageConverter1da6205a] 3DEBUG 2014-10-11 14:07:17,078org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 Null ModelAndView returned to DispatcherServlet with name 'spring3': assuming HandlerAdapter pleted request handling2DEBUG 2014-10-11 14:07:17,078org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-1 Successfully pleted requestDEBUG 2014-10-11 14:07:17,078org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-1 Returning cached instance of singleton bean 'sqlSessionFactory'DEBUG 2014-10-11 14:07:17,099net.sf.ehcache.store.disk.Segment.%002espringdemo%002emapper%002e%0041ccount%0 04dapper.data fault removed 0 from heapDEBUG 2014-10-11 14:07:17,099net.sf.ehcache.store.disk.Segment.%002espringdemo%002emapper%002e%0041ccount%0 04dapper.data fault added 0 on diskDEBUG 2014-10-11 14:07:17,100net.sf.ehcache.store.disk.Segment.%002espringdemo%002emapper%002e%0041ccount%0 04dapper.data fault removed 0 from heapDEBUG 2014-10-11 14:07:17,101net.sf.ehcache.store.disk.Segment.%002espringdemo%002emapper%002e%0041ccount%0 04dapper.data fault added 0 on disk启用cache的命中后日志如下:DEBUG 2014-10-11 14:07:52,907org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 DispatcherServlet with name 'spring3' processing GET request for [/AnnExp2/background/account/list.do] DEBUG 2014-10-11 14:07:52,907org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-5 Looking up handler method for path /background/account/list.do DEBUG 2014-10-11 14:07:52,917org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.http-bio-8080-exec-5 Returning handler method [publicng.String ermgr.controller.AccountController.list(org.springframework.ui. Model,ermgr.vo.Resources,ng.String)]DEBUG 2014-10-11 14:07:52,917org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-5 Returning cached instance of singleton bean 'accountController'DEBUG 2014-10-11 14:07:52,917org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 Last-Modified value for [/AnnExp2/background/account/list.do] is: -13DEBUG 2014-10-11 14:07:52,917org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 Rendering view [org.springframework.web.servlet.view.JstlView: name 'background/account/list'; URL[/WEB-INF/view/background/account/list.jsp]] in DispatcherServlet with name 'spring3' DEBUG 2014-10-11 14:07:52,917org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-5 Added model object'resources' of type [ermgr.vo.Resources] to request in view with name'background/account/list'DEBUG 2014-10-11 14:07:52,917org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-5 Added model object'org.springframework.validation.BindingResult.resources' of type[org.springframework.validation.BeanPropertyBindingResult] to request in view with name'background/account/list'DEBUG 2014-10-11 14:07:52,917org.springframework.web.servlet.view.JstlView.http-bio-8080-exec-5 Forwarding to resource [/WEB-INF/view/background/account/list.jsp] in InternalResourceView'background/account/list'2DEBUG 2014-10-11 14:07:52,917org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 Successfully pleted requestDEBUG 2014-10-11 14:07:52,917org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-5 Returning cached instance of singleton bean 'sqlSessionFactory'DEBUG 2014-10-11 14:07:53,121org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 DispatcherServlet with name 'spring3' processing POST request for [/AnnExp2/background/account/query.do]org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-5 Looking up handler method for path /background/account/query.do DEBUG 2014-10-11 14:07:53,131org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.ht tp-bio-8080-exec-5 Returning handler method[public .lanyuan.pulgin.mybatis.plugin.PageView ermgr.controller.AccountCont roller.query(ermgr.vo.Account,ng.String,ng.String)]DEBUG 2014-10-11 14:07:53,131org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-5 Returning cached instance of singleton bean 'accountController'DEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 Creating a new SqlSessionDEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession61b38786] was not registered for synchronization because synchronization is not active DEBUG 2014-10-11 14:07:53,131 .springdemo.mapper.AccountMapper.http-bio-8080-exec-5 Cache Hit Ratio [.springdemo.mapper.AccountMapper]: 0.25DEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 Closing non transactional SqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession61b38786]DEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 Creating a new SqlSessionDEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession5d94e73a] was not registered for synchronization because synchronization is not active DEBUG 2014-10-11 14:07:53,131 .springdemo.mapper.AccountMapper.http-bio-8080-exec-5 Cache Hit Ratio [.springdemo.mapper.AccountMapper]: 0.4DEBUG 2014-10-11 14:07:53,131 org.mybatis.spring.SqlSessionUtils.http-bio-8080-exec-5 Closing non transactional SqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession5d94e73a]DEBUG 2014-10-11 14:07:53,131org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProc essor.http-bio-8080-exec-5 Written [.lanyuan.pulgin.mybatis.plugin.PageView4c5c0e8b] as "application/json;charset=UTF-8" using[org.springframework.http.converter.json.MappingJacksonHttpMessageConverter1da6205a] 3org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 Null ModelAndView returned to DispatcherServlet with name 'spring3': assuming HandlerAdapter pleted request handling2DEBUG 2014-10-11 14:07:53,131org.springframework.web.servlet.DispatcherServlet.http-bio-8080-exec-5 Successfully pleted requestDEBUG 2014-10-11 14:07:53,131org.springframework.beans.factory.support.DefaultListableBeanFactory.http-bio-8080-exec-5 Returning cached instance of singleton bean 'sqlSessionFactory'可以看到前后明显差异,命中cache后,查询效率要明显提高。

详细解析Ehcache的底层实现原理和逻辑

详细解析Ehcache的底层实现原理和逻辑

详细解析Ehcache的底层实现原理和逻辑(英文介绍):Ehcache is a widely used open-source caching library for Java-based applications. Its underlying implementation principles and logic are centered around providing an efficient, scalable, and easy-to-use caching solution.At the core of Ehcache lies the concept of a cache manager and cache instances. The cache manager, represented by the CacheManager class, serves as the entry point for accessing and managing caches. It handles tasks such as cache creation, configuration, and lifecycle management. Each cache instance, represented by the Cache interface, represents a named and configurable storage container for caching data.Ehcache supports both in-memory and disk-based caching. In-memory caching stores data in the JVM's heap space, providing fast access times. Disk-based caching, on the other hand, persists data to the file system, allowing for larger cache sizes and data survival across application restarts. Ehcache efficiently manages the transfer of data between these two storage tiers based on configuration parameters and runtime conditions.The caching logic in Ehcache revolves around key-value pairs. When a data item is put into the cache, it is associated with a unique key, which is used for subsequent retrieval operations. Ehcache provides various eviction policies, such as LRU (Least Recently Used) and TTL (Time To Live), to automatically remove old or expired entries from the cache, ensuring optimal cache performance. Additionally, Ehcache supports caching strategies like caching-through and caching-aside. Caching-through involves intercepting all data access requests and serving them from the cache if possible, while also updating the cache with the latest data from the underlying data source. Caching-aside, on the other hand, leaves the decision of when to populate or evict the cache to the application logic.Ehcache also provides features like cache replication and clustering, allowing distributed caching across multiple nodes for improved scalability and fault tolerance.Overall, Ehcache's underlying implementation principles and logic are designed to offer a robust and flexible caching solution that can be seamlessly integrated into a wide range of Java applications.详细解析Ehcache的底层实现原理和逻辑(中文介绍):Ehcache是一个广泛使用的基于Java的开源缓存库。

mybatis hbase 写法 -回复

mybatis hbase 写法 -回复

mybatis hbase 写法-回复MyBatis与HBase的整合方式及使用介绍HBase是一种高可靠性、高性能、分布式的开源NoSQL数据库,而MyBatis是Java中一款优秀的持久层框架。

将两者结合可以实现数据的快速访问和灵活的查询。

在本篇文章中,我们将以如下主题为线索,一步一步介绍MyBatis与HBase的整合方式及使用。

1. 简介1.1 HBase概述1.2 MyBatis概述1.3 MyBatis与HBase的整合优势2. 搭建环境2.1 安装HBase2.2 配置HBase2.3 导入MyBatis依赖3. 创建数据模型3.1 定义表结构3.2 创建实体类3.3 创建Mapper接口和Mapper XML文件4. 配置实体类与表4.1 配置实体类的数据映射4.2 配置HBase的连接信息4.3 配置Mapper接口与Mapper XML的关联5. 数据访问操作5.1 插入数据5.2 更新数据5.3 查询数据5.4 删除数据6. 总结与扩展6.1 MyBatis与HBase整合的优缺点6.2 典型应用场景6.3 学习资源推荐第一部分:简介1.1 HBase概述HBase是一种在Hadoop生态系统上构建的面向列的开源数据库。

它提供了高可靠性、高性能、可伸缩性的数据存储服务,并能支持海量数据的访问和查询。

1.2 MyBatis概述MyBatis是一种持久层框架,通过XML或注解的方式将SQL语句与Java代码分离,提供了便捷的数据库操作接口,能够方便地执行增删改查等操作。

1.3 MyBatis与HBase的整合优势- MyBatis具有简单、灵活的特点,方便开发人员进行数据访问;- HBase提供了高性能的数据存储和查询能力;- MyBatis与HBase的整合可以使开发人员更加方便地进行数据操作,提高开发效率。

第二部分:搭建环境2.1 安装HBase首先,我们需要安装和配置HBase。

mybatis注解大全

mybatis注解大全

mybatis注解⼤全注解⽬标相对应的XML描述@CacheNamespace类<cache>为给定的命名空间 (⽐如类) 配置缓存。

属性:implemetation,eviction, flushInterval,size 和readWrite。

@CacheNamespaceRef类<cacheRef>参照另外⼀个命名空间的缓存来使⽤。

属性:value,应该是⼀个名空间的字符串值(也就是类的完全限定名) 。

@ConstructorArgs Method<constructor>收集⼀组结果传递给⼀个劫夺对象的构造⽅法。

属性:value,是形式参数的数组。

@Arg⽅法<arg><idArg>单独的构造⽅法参数 , 是 ConstructorArgs 集合的⼀部分。

属性:id,column,javaType,typeHandler。

id 属性是布尔值, 来标识⽤于⽐较的属性,和<idArg>XML 元素相似。

@TypeDiscriminator⽅法<discriminator>⼀组实例值被⽤来决定结果映射的表现。

属性: column, javaType, jdbcType, typeHandler,cases。

cases 属性就是实例的数组。

@Case⽅法<case>单独实例的值和它对应的映射。

属性: value,type,results。

Results 属性是结果数组,因此这个注解和实际的 ResultMap 很相似,由下⾯的 Results 注解指定。

@Results⽅法<resultMap>结果映射的列表, 包含了⼀个特别结果列如何被映射到属性或字段的详情。

属性:value, id。

value 属性是 Result 注解的数组。

The id attribute is the name of the result mapping.@Result⽅法<result><id>在列和属性或字段之间的单独结果映射。

MyBatis 缓存机制深度解剖

MyBatis 缓存机制深度解剖

缓存概述∙正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持;∙一级缓存基于PerpetualCache的 HashMap 本地缓存,其存储作用域为Session,当 Session flush 或 close 之后,该Session中的所有 Cache 就将清空。

∙二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache、Hazelcast等。

∙对于缓存数据更新机制,当某一个作用域(一级缓存Session/二级缓存Namespaces)的进行了 C/U/D 操作后,默认该作用域下所有 select 中的缓存将被clear。

∙MyBatis 的缓存采用了delegate机制及装饰器模式设计,当put、get、remove时,其中会经过多层 delegate cache 处理,其Cache类别有:BaseCache(基础缓存)、EvictionCache(排除算法缓存) 、DecoratorCache(装饰器缓存):BaseCache :为缓存数据最终存储的处理类,默认为 PerpetualCache,基于Map存储;可自定义存储处理,如基于EhCache、Memcached等;EvictionCache :当缓存数量达到一定大小后,将通过算法对缓存数据进行清除。

默认采用 Lru 算法(LruCache),提供有 fifo 算法(FifoCache)等;DecoratorCache:缓存put/get处理前后的装饰器,如使用 LoggingCache 输出缓存命中日志信息、使用 SerializedCache 对 Cache的数据 put或get 进行序列化及反序列化处理、当设置flushInterval(默认1/h)后,则使用 ScheduledCache 对缓存数据进行定时刷新等。

【Mybatis】Mybatis实战2(一对一、一对多、多对多的设计及实现,高级特性及二级缓存)

【Mybatis】Mybatis实战2(一对一、一对多、多对多的设计及实现,高级特性及二级缓存)

【Mybatis】Mybatis实战2(⼀对⼀、⼀对多、多对多的设计及实现,⾼级特性及⼆级缓存)6).多表查询-“⼀对多”(表设计、实体设计、DAO(mapper)设计)(1)关联关系操作(⼀对多)①表设计:以员⼯和部门表为例思想: 1个员⼯对应1个部门,1个部门对应多个员⼯添加数据原则:先添加没有外键的数据(部门信息),再添加存在外键的数据(员⼯信息)注意:将外键添加在n的⼀⽅部门表:create table t_dept(id varchar2(36) primary key,name varchar2(50));员⼯表:create table t_emp(id varchar2(36) primary key,name varchar2(50),age number(3),salary number(10,2),dept_id references t_dept(id));②实体设计a. 在实体中添加关系属性,来表⽰实体之间的关系(对应表数据的关系)b. 在N的⼀⽅添加1的⼀个关系属性。

c. 在1的⼀⽅添加N的⼀个List的关系属性DAO:(MyBatis如何查询两张表信息)需求1:查询员⼯信息(⼯号,名字,年龄,薪资,所属部门的编号和名称)根据员⼯⼯号?DAO接⼝⽅法:public Emp selectById(String id);Mapper⽂件:①SQL:select e.id,,e.age,e.salary,d.id, from t_emp e left join t_dept d on e.dept_id = d.id where e.id = '5';②参数③将查询结果映射成⼀个实体对象特点: 如果关系属性是”1” ,使⽤ <association></association>需求2:根据id查询部门信息,及其内部的所有员⼯信息?DAO接⼝⽅法:public Dept selectById(String id);Mapper⽂件中①SQL:select d.id,,e.id as eid, as ename,e.age as eage,e.salary as salary from t_dept d left join t_emp e on d.id = e.dept_idwhere d.id = ?;②参数绑定③结果映射:ReusultMap映射集合关系属性特点: 关系属性是”n”个的集合 ,使⽤ <collection></ collection >7).多表查询-“⼀对⼀”(表设计、实体设计、DAO(mapper)设计)关联关系操作(⼀对⼀)例如:需求: 学⽣电脑管理系统①库表设计表⽰1对1的关系a. 添加外键(那张表添加都可以)①从业务的⾓度分析,后添加的数据对应的表。

mybatis中文版教程

mybatis中文版教程

MyBatis Spring1.0.0-RC3参考文档MyBatis 社区()Copyright © 2010本文档的拷贝仅允许您个人使用或分发给其他用户,但是不能收取任何费用,后期的发布无论是印刷版或电子版,也会进行版权声明。

本文档由南磊(nanlei1987@)翻译目录第一章介绍 (3)1.1 整合动机 (3)1.2 要求 (3)1.3 感谢 (3)第二章入门 (4)2.1 安装 (4)2.2 快速创建 (4)第三章SqlSessionFactoryBean (6)3.1 创建 (6)3.2 属性 (6)第四章事务 (8)4.1 标准配置 (8)4.2 容器管理事务 (8)第五章使用SqlSession (9)5.1 SqlSessionSupport (9)5.2 SqlSessionTemplate (9)第六章MapperFactoryBean (11)6.1 创建 (11)6.2 注入映射器 (11)6.3 自动配置 (12)第七章使用MyBatis API (13)第八章示例代码 (14)第一章介绍1.1 整合动机正如第二版,Spring仅支持iBatis2。

那么我们就想将MyBatis3的支持加入到Spring3.0(参考Spring的Jira的问题)中。

不幸的是,Spring 3.0的开发在MyBatis 3.0官方发布前就结束了。

因为Spring开发团队不想发布一个基于非发行版的MyBatis的整合支持,那么Spring 官方的支持就不得不等到至少3.1版本了。

要在Spring中支持MyBatis,MyBatis社区认为现在应该是自己团结贡献者和有兴趣的人一起来开始进行Spring和MyBatis整合的时候了。

这个小类库就来创建丢失的粘贴Spring和MyBtatis这两个流行框架的胶水。

减少用户不得不来配置MyBatis和Spring 3.X上下文环境的样板和冗余代码。

Mybatis的配置文件和映射文件详解

Mybatis的配置文件和映射文件详解

Mybatis的配置⽂件和映射⽂件详解⼀、Mybatis的全局配置⽂件1、SqlMapConfig.xml(名称可变)是mybatis的全局配置⽂件,配置内容如下:properties(属性)settings(全局配置参数)typeAliases(类型别名)typeHandlers(类型处理器)objectFactory(对象⼯⼚)plugins(插件)environments(环境集合属性对象)environment(环境⼦属性对象)transactionManager(事务管理)dataSource(数据源)mappers(映射器)2、properties将数据库连接参数单独配置在db.properties(名称可变)中,放在类路径下。

这样只需要在SqlMapConfig.xml中加载db.properties的属性值。

这样在SqlMapConfig.xml中就不需要对数据库连接参数硬编码。

将数据库连接参数只配置在db.properties中,原因:⽅便对参数进⾏统⼀管理,其它xml可以引⽤该db.properties例如:db.propertiesjdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatisername=rootjdbc.password=root相应的SqlMapConfig.xml<properties resource="db.properties"/><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${ername}"/><property name="password" value="${jdbc.password}"/></dataSource></environment></environments>注意: MyBatis 将按照下⾯的顺序来加载属性:⾸先、在properties标签中指定的属性⽂件⾸先被读取。

mybatis语法和介绍详细

mybatis语法和介绍详细

mybatis语法和介绍详细MyBatis(原名为iBATIS)是一种开源的持久化框架,它主要用于将Java对象与SQL语句进行映射,从而实现对象关系映射(ORM)。

MyBatis有一套独立的SQL映射文件,其中定义了对象属性与数据库列之间的映射关系。

这些SQL映射文件通过XML进行定义,使得开发人员可以将SQL语句与Java代码进行解耦。

SQL映射文件可以包含各种插入、更新、删除和查询语句,同时还支持存储过程和函数的调用。

MyBatis的核心组件包括SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession和Mapper。

SqlSessionFactoryBuilder负责创建SqlSessionFactory对象,SqlSessionFactory负责创建SqlSession对象,SqlSession则是与数据库进行交互的核心类,而Mapper则负责定义与数据库交互的接口。

MyBatis的使用非常灵活,可以通过注解或者XML进行配置。

注解是直接在Java代码中使用的标记,通过注解可以直接在Java代码中定义SQL语句,从而省去了编写XML文件的过程。

XML配置方式则需要额外编写SQL映射文件,但是更加灵活和可维护。

下面是一个使用MyBatis的例子,其中使用了XML配置方式:首先,我们需要编写一个User类,用于与数据库中的user表进行映射:```javapublic class Userprivate int id;private String name;private String email;// 省略getter和setter方法```接下来,我们需要编写一个UserMapper接口,用于定义与数据库交互的方法:```javapublic interface UserMapperUser selectUser(int id);void insertUser(User user);void updateUser(User user);void deleteUser(int id);```然后,我们需要创建一个SQL映射文件UserMapper.xml,其中定义了与User类对应的SQL语句:```xmlSELECT * FROM user WHERE id = #{id}</select>INSERT INTO user (name, email) VALUES (#{name}, #{email})</insert>UPDATE user SET name = #{name}, email = #{email} WHERE id = #{id}</update><delete id="deleteUser" parameterType="int">DELETE FROM user WHERE id = #{id}</delete></mapper>```最后,我们可以通过SqlSessionFactoryBuilder来创建SqlSessionFactory对象:```javaString resource = "mybatis-config.xml";InputStream inputStream =Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder(.build(inputStream);```接下来,我们可以通过SqlSessionFactory对象创建SqlSession对象:```javaSqlSession session = sqlSessionFactory.openSession(;```然后,我们可以通过SqlSession对象获取Mapper接口的实例:```javaUserMapper userMapper = session.getMapper(UserMapper.class);```最后,我们可以通过Mapper接口的实例来调用与数据库交互的方法:```javaUser user = userMapper.selectUser(1);```以上就是一个简单的使用MyBatis的例子。

mybatis的缓存机制(一级缓存二级缓存和刷新缓存)和mybatis整合ehcache

mybatis的缓存机制(一级缓存二级缓存和刷新缓存)和mybatis整合ehcache

mybatis的缓存机制(一级缓存二级缓存和刷新缓存)和mybatis整合ehcache1 查询缓存1.1 什么是查询缓存mybatis提供查询缓存,用于减轻数据压力,提高数据库性能。

mybaits 提供一级缓存,和二级缓存。

一级缓存是SqlSession级别的缓存。

在操作数据库时需要构造sqlSession对象,在对象中有一个(内存区域)数据结构(HashMap)用于存储缓存数据。

不同的sqlSession之间的缓存数据区域(HashMap)是互相不影响的。

一级缓存的作用域是同一个SqlSession,在同一个sqlSession中两次执行相同的sql语句,第一次执行完毕会将数据库中查询的数据写到缓存(内存),第二次会从缓存中获取数据将不再从数据库查询,从而提高查询效率。

当一个sqlSession结束后该sqlSession中的一级缓存也就不存在了。

Mybatis默认开启一级缓存。

二级缓存是mapper级别的缓存,多个SqlSession去操作同一个Mapper 的sql语句,多个SqlSession去操作数据库得到数据会存在二级缓存区域,多个SqlSession可以共用二级缓存,二级缓存是跨SqlSession的。

二级缓存是多个SqlSession 共享的,其作用域是mapper的同一个namespace,不同的sqlSession两次执行相同namespace下的sql语句且向sql 中传递参数也相同即最终执行相同的sql语句,第一次执行完毕会将数据库中查询的数据写到缓存(内存),第二次会从缓存中获取数据将不再从数据库查询,从而提高查询效率。

Mybatis默认没有开启二级缓存需要在setting全局参数中配置开启二级缓存。

如果缓存中有数据就不用从数据库中获取,大大提高系统性能。

1.2 一级缓存1.2.1 一级缓存工作原理下图是根据id查询用户的一级缓存图解第一次发起查询用户id为1的用户信息,先去找缓存中是否有id为1的用户信息,如果没有,从数据库查询用户信息。

MyBatis官方中文文档

MyBatis官方中文文档

提示 命名空间的一点注释
命名空间(Namespaces)在之前版本的 MyBatis 中是可选的,容易引起混淆因此是没有益处的。现在的命名空 间则是必须的,目的是希望能比只是简单的使用更长的完全限定名来更进一步区分语句。 命名空间使得你所见到的接口绑定成为可能,尽管你觉得这些东西未必用得上,你还是应该遵循这里的规定以防哪 天你改变了主意。出于长远考虑,使用命名空间,并将它置于合适的 Java 包命名空间之下,你将拥有一份更加整 洁的代码并提高了 MyBatis 的可用性。 命名解析:为了减少输入量,MyBatis 对所有的命名配置元素(包括语句,结果映射,缓存等)使用了如下的命名 解析规则。 完全限定名(比如“com.mypackage.MyMapper.selectAllThings”)将被直接查找并且找到即用。 短名称(比如“selectAllThings”)如果全局唯一也可以作为一个单独的引用。如果不唯一,有两个或两个以上的相 同名称(比如“com.foo.selectAllThings ”和“com.bar.selectAllThings”),那么使用时就会收到错误报告说短名称 是不唯一的,这种情况下就必须使用完全限定名。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN" "/dtd/mybatis-3-config.dtd"> <configuration> <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> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration>

springboot项目中,ehcache报错,需要CacheManager单例的解决办法

springboot项目中,ehcache报错,需要CacheManager单例的解决办法

springboot项⽬中,ehcache报错,需要CacheManager单例的解决办法在新的springboot项⽬中,如果在mybatis中使⽤了ehcache后,再第⼆次使⽤ehcache,会提⽰错误Another CacheManager with same name 'default' already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:原因是在ehcache2.5版本后,cachemanager必须是单例的,不允许多次创建。

解决办法在config的配置⽂件中,新建⼀个bean,然后署名,在程序引⽤他时,标注这个name为新建的cachemanager配置代码@Configurationpublic class EhCacheConfig {// @Bean// public CacheManager getCacheManager() {//// return CacheManager.create("classpath:ehcache.xml");// }@Bean(name = "demo")public CacheManager getCacheManagerDemo() {return CacheManager.create("classpath:ehcache2.xml");}}ehcache2.xml ( ehcache.xml⽂件为系统默认加载的额,这⾥必须另建⼀个xml⽂件,以⽰区分 )<?xml version="1.0" encoding="UTF-8"?><ehcache name="demo" xmlns:xsi="/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="/ehcache.xsd"updateCheck="false"><!-- xsi:noNamespaceSchemaLocation="/ehcache.xsd" --><!-- 磁盘保存路径 --><diskStore path="D:\44test\ehcache"/><defaultCache maxElementsInMemory="1000"maxElementsOnDisk="10000000" eternal="false" overflowToDisk="true"timeToIdleSeconds="120" timeToLiveSeconds="120"diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"></defaultCache></ehcache>引⽤部分在service层@Cacheable(cacheManager = "demo")public List<User> queryAll() {// TODO Auto-generated method stubreturn erMapper.queryAll();}启动⼊⼝类@SpringBootApplication@MapperScan(basePackages = {"cn.taotao.dao"})@EnableCachingpublic class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}。

Mybatis整合Redis实现二级缓存

Mybatis整合Redis实现二级缓存

Mybatis整合Redis实现⼆级缓存mybatis集成ehcache1、集成ehcache2、集成redis1. 为什么需要缓存拉⾼程序的性能2. 什么样的数据需要缓存很少被修改或根本不改的数据业务场景⽐如:耗时较⾼的统计分析sql、电话账单查询sql等3. ehcache是什么Ehcache 是现在最流⾏的纯Java开源缓存框架,配置简单、结构清晰、功能强⼤注1:本章介绍的是2.X版本,3.x的版本和2.x的版本API差异⽐较⼤4. ehcache的特点4.1够快Ehcache的发⾏有⼀段时长了,经过⼏年的努⼒和不计其数的性能测试,Ehcache终被设计于large, high concurrency systems.4.2够简单开发者提供的接⼝⾮常简单明了,从Ehcache的搭建到运⽤运⾏仅仅需要的是你宝贵的⼏分钟。

其实很多开发者都不知道⾃⼰⽤在⽤Ehcache,Ehcache被⼴泛的运⽤于其他的开4.3够袖珍关于这点的特性,官⽅给了⼀个很可爱的名字small foot print ,⼀般Ehcache的发布版本不会到2M,V 2.2.3才 668KB。

4.4够轻量核⼼程序仅仅依赖slf4j这⼀个包,没有之⼀!4.5好扩展Ehcache提供了对⼤数据的内存和硬盘的存储,最近版本允许多实例、保存对象⾼灵活性、提供LRU、LFU、FIFO淘汰算法,基础属性⽀持热配置、⽀持的插件多4.6监听器缓存管理器监听器(CacheManagerListener)和缓存监听器(CacheEvenListener),做⼀些统计或数据⼀致性⼴播挺好⽤的4.7分布式缓存从Ehcache 1.2开始,⽀持⾼性能的分布式缓存,兼具灵活性和扩展性3、ehcache的使⽤3.1 导⼊相关依赖3.2 核⼼接⼝CacheManager:缓存管理器Cache:缓存对象,缓存管理器内可以放置若⼲cache,存放数据的实质,所有cache都实现了Ehcache接⼝Element:单条缓存数据的组成单位4. ssm中整合ehcache4.1 导⼊相关依赖<dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><!--mybatis与ehcache整合--><dependency><groupId>org.mybatis.caches</groupId><artifactId>mybatis-ehcache</artifactId><version>1.1.0</version></dependency><!--ehcache依赖--><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.10.0</version></dependency>4.2 修改⽇志配置,因为ehcache使⽤了Slf4j作为⽇志输出⽇志我们使⽤slf4j,并⽤log4j来实现。

ehcache技术详解

ehcache技术详解

Ehcache技术详解1 基本概念EHCache是一个快速的、轻量级的、易于使用的、进程内的缓存。

它支持read-only和read/write 缓存,内存和磁盘缓存。

是一个非常轻量级的缓存实现,而且从1.2 之后就支持了集群,目前的最新版本是2.8。

1.1 主要特性(1)快速、简单(2)提供LRU、LFU和FIFO 缓存策略(3)缓存数据有两级:内存和磁盘,因此无需担心容量问题(4)缓存数据会在虚拟机重启的过程中写入磁盘(5)支持分布式缓存(6)支持多缓存管理器实例,以及一个实例的多个缓存区域2 部署2.1 引入官网地址:/下载发布包ehcache-2.8.1-distribution.tar.gz并解压1、将lib下的jar包引入自己的工程包括核心包:ehcache-2.8.1.jar,依赖jar包:slf4j-api-1.6.6.jar,slf4j-jdk14-1.6.6.jar2、将ehcache.xml放到src目录下。

2.2 配置ehcache.xml中的内容如下:<?xml version="1.0"encoding="UTF-8"?><ehcache xmlns:xsi="/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="/ehcache.xsd"> <diskStore path="java.io.tmpdir"/><cache name="sampleCache1"maxEntriesLocalHeap="10000"maxEntriesLocalDisk="1000"eternal="false"overflowToDisk="true"diskPersistent="true"diskSpoolBufferSizeMB="20"timeToIdleSeconds="300"timeToLiveSeconds="600"memoryStoreEvictionPolicy="LFU"diskExpiryThreadIntervalSeconds ="120"transactionalMode="off"><cacheEventListenerFactoryclass="com.ehcache.EhcacheListenerTest"properties="wxwtestkey=200,wxtestkey2=300"/><persistence strategy="localTempSwap"/></cache></ehcache>配置项详解:<diskStore path="java.io.tmpdir"/>此配置项配置的是,磁盘缓存的位置。

mybatis补充教程之三:缓存(cache)的使用

mybatis补充教程之三:缓存(cache)的使用

mybatis补充教程之三:缓存(cache)的使用许多应用程序,为了提高性能而增加缓存, 特别是从数据库中获取的数据. 在默认情况下,mybatis 的一级缓存是默认开启的。

类似于hibernate, 所谓一级缓存,也就是基于同一个sqlsession 的查询语句,即session 级别的缓存,非全局缓存,或者非二级缓存.如果要实现mybatis 的二级缓存,一般来说有如下两种方式:1. 采用mybatis 内置的cache 机制。

2. 采用三方cache 框架,比如ehcache, oscache 等等. 采用mybatis 内置的cache 机制。

在sql 语句映射文件中加入&lt;cache /&gt; 语句, 并且相应的model 类要实现java Serializable 接口,因为缓存说白了就是序列化与反序列化的过程,所以需要实现这个接口. 单纯的&lt;cache /&gt; 表示如下意思:1.所有在映射文件里的select 语句都将被缓存。

2.所有在映射文件里insert,update 和delete 语句会清空缓存。

3.缓存使用“最近很少使用”算法来回收4.缓存不会被设定的时间所清空。

5.每个缓存可以存储1024 个列表或对象的引用(不管查询出来的结果是什么)。

6.缓存将作为“读/写”缓存,意味着获取的对象不是共享的且对调用者是安全的。

不会有其它的调用者或线程潜在修改。

缓存元素的所有特性都可以通过属性来修改。

比如:程序代码&lt;cache eviction="FIFO" flushInterval="60000"size="512" readOnly="true" /&gt; 采用ehcache 来实现mybatis 的二级缓存首先需要在mybatis 的官网上下载相关jar 包:https:///p/mybatis/ 写文档的时候下载的是:mybatis-ehcache-1.0.2.zip ,里面包括了程序代码mybatis-ehcache-1.0.2.jarehcache-core-2.6.5.jarslf4j-api-1.6.1.jar当然,采用ehcache 就必须在classpath 下加入ehcache 的配置文件ehcache.xml:程序代码&lt;cache name="default"maxElementsInMemory="10000"eternal="false"timeToIdleSeconds="3600"timeToLiveSeconds="10"overflowToDisk="true"diskPersistent="true"diskExpiryThreadIntervalSeconds="120"maxElementsOnDisk="10000"/&gt;那么在sql 映射文件中要如何配置呢,参考如下:程序代码&lt;cachetype="org.mybatis.caches.ehcache.LoggingEhcache" &gt;&lt;property name="timeToIdleSeconds"value="3600"/&gt;&lt;!--1 hour--&gt;&lt;property name="timeToLiveSeconds"value="3600"/&gt;&lt;!--1 hour--&gt;&lt;property name="maxEntriesLocalHeap"value="1000"/&gt;&lt;property name="maxEntriesLocalDisk"value="10000000"/&gt;&lt;property name="memoryStoreEvictionPolicy" value="LRU"/&gt;&lt;/cache&gt;总结:无论是采用mybatis 自身的cache 还是三方的cache , 这样的配置,就是对所有的select 语句都全局缓存,但事实上,并不总是这样,比如,我在这系列教程中第七章中/article/java/326.htm,自己写的分页算法,就不能用这种情况。

ehcache

ehcache

Ehcache缓存配置文章分类:Java编程简介Cache的配置很灵活,官方提供的Cache配置方式有好几种。

你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。

你可以将Cache的配置从代码中剥离出来,也可以在使用运行时配置,所谓的运行时配置无非也就是在代码中配置。

以下是运行时配置的好处:·在同一个地方配置所有的Cache,这样很容易管理Cache的内存和磁盘消耗。

·发布时可更改Cache配置。

·可再安装阶段就检查出配置错误信息,而避免了运行时错误。

本文将会对ehcache.xml配置文件进行详细的阐述。

在配置的时可以拷贝一个现有的ehcache.xml,如果没有请点击这里去下载。

ehcache-failsafe.xml如果你调用了CacheManager默认构造方法去创建CacheManager的实例,此方法会到classpath中找ehcache.xml文件,否则它会到类路径下找ehcache-failsafe.xml文件。

而ehcache-failsafe.xml被包含在jar包中,所有它肯定能找的到。

ehcache-failsafe.xml提供了一个非常简单的默认配置,这样可以使用户在没有创建ehcache.xml的情况下使用Ehcache。

不过这样做Ehcache会提醒用户创建一个正确的Ehcache配置。

ehcache.xml片段:<ehcache><diskStore path="java.io.tmpdir"/><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="true"maxElementsOnDisk="10000000"diskPersistent="false"diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"/></ehcache>ehcache.xml和其他配置文件在Ehcache-1.6之前的版本,只支持ASCII编码的ehcache.xml配置文件。

SSM整合ehcache

SSM整合ehcache

1.<dependency>2.<groupId>org.mybatis</groupId>3.<artifactId>mybatis-ehcache</artifactId>4.<version>1.0.0</version>5.</dependency>6.<dependency>7.<groupId>org.ehcache</groupId>8.<artifactId>ehcache</artifactId>9.<version>3.0.0.m3</version>10.</dependency>1.<?xml version="1.0"encoding="UTF-8"?>2.<ehcache xmlns:xsi="http://www.w/2001/XMLSchema-instance"3.xsi:noNamespaceSchemaLocation="/ehcache.xsd"4.updateCheck="false">5.<diskStore path="java.io.tmpdir"/>6.<defaultCache eternal="false"maxElementsInMemory="1000"7.overflowToDisk="false"diskPersistent="false"timeToIdleSeconds="0"8.timeToLiveSeconds="600"memoryStoreEvictionPolicy="LRU"/>9.<cache name="testCache"eternal="false"maxElementsInMemory="100"10.overflowToDisk="false"diskPersistent="false"timeToIdleSeconds="0"11.timeToLiveSeconds="300"memoryStoreEvictionPolicy="LRU"/>12.</ehcache>说明:name:Cache的唯一标识maxElementsInMemory:内存中最大缓存对象数maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大eternal:Element是否永久有效,一但设置了,timeout将不起作用overflowToDisk:配置此属性,当内存中Element数量达到maxElementsIn Memory时,Ehcache将会Element写到磁盘中timeToIdleSeconds:设置Element在失效前的允许闲置时间。

MyBatis中文帮助文档

MyBatis中文帮助文档
下面这个表格描述了默认的类类型处理器java类型jdbc类型byte任何兼容的数字或字节类型float任何兼容的数字或单精度浮点型任何兼容的数字或双精度浮点型类型处理器java类型jdbc类型任何兼容的数字或十进制小数类型charvarchar类型cloblongvarchar类型nvarcharnchar类型nclob类型任何兼容的字节流类型bloblongvarbinary类型timestamp类型date类型time类型timestamp类型date类型time类型any其他或未指定类型enumerationtypevarchar任何兼容的字符串类型作为代码存储而不是索引类型处理器java类型jdbc类型enumerationtypeanycompatiblecodeitself
XML 配置文件,或从 Configuration 类的习惯准备的实 例中构建 SqlSessionFactory 对象。 从 XML 文件中构建 SqlSessionFactory 的实例非常简单。这里建议你使用类路径下的 资 源文件来配置,但是你可以使用任意的 Reader 实例,这个实例包括由文字形式的文件 路径 或 URL 形式的文件路径 file://来创建。MyBatis 包含了一些工具类,称作为资源, 这些工具 类包含一些方法,这些方法使得从类路径或其他位置加载资源文件更加简单。
个概念应该很熟悉了,但是 XML 映射文件也有 很多的改进,后面我们会详细来说。这里 给出一个基于 XML 映射语句的示例,这些语句应 该可以满足上述示例中 SqlSession 对象的调用。
这个简单的例子中看起来有很多额外的东西, 但是也相当简洁了。 你可以在一个单独的 XML 映射文件中定义很多的映射语句,除 XML 头部和文档类型声明之外,你可以得到很 多 方 便 之 处 。 在 文 件 的 剩 余 部 分 是 很 好 的 自 我 解 释 。 在 命 名 空 间 “com.mybatis.example.BlogMapper”中,它定义了一个名为“selectBlog”的映射语 句,这 样它允许你使用完全限定名 “org.mybatis.example.BlogMapper.selectBlog” 来调 用映射语句, 我们下面示例中所有的写法也是这样的。
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

MyBatis EHCache integration-Reference
Documentation
The MyBatis Community()
Copyright©2010
Copies of this document may be made for your own use and for distribution to others,provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice,whether
distributed in print or electronically.
1.The MyBatis EHCache integration (1)
1.1.How to (1)
Chapter1.The MyBatis EHCache integration
1.1.How to
EHCache is a widely used java distributed cache for general purpose caching,Java EE and light-weight containers.
The EHCache integration is built on top of the ehcache-core and comes without any EHCache3rd part applications.Please refeer to official EHCache documentation if you need plugins.
Users that want to use EHCache into their applications,have to download the1.0.0zip bundle,decompress it and add the jars in the classpath;Apache Maven users instead can simply add in the pom.xml the following dependency:
then,just configure it in the mapper XML
If users need to log cache operations,they can plug the Cache logging version:
Users that need to configure EHCache through XML configuration file,have to put in the classpath the /ehcache.xml resource;please refeer to the official EHCache documentation to know more details.
If the/ehcache.xml resource is not found or something goes wrong while loading it,the default configuration will be used.。

相关文档
最新文档