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上下文环境的样板和冗余代码。

  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.。

相关文档
最新文档