• mybatis 缓存 + spring 整合详解


    @Test
        // 测试自动映射
        public void testAutoMapping() throws IOException {
            // 2.获取sqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession();
            // 3.获取对应mapper
            TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);
            // 4.执行查询语句并返回结果
            TUser user = mapper.selectByPrimaryKey(1);
            TUser user2 = mapper.selectByPrimaryKey(1);
            TUser user3 = mapper.selectByPrimaryKey(1);
            TUser user4 = mapper.selectByPrimaryKey(1);
            System.out.println(user == user4);
        }


    2020-10-22 19:45:46.216 [main] DEBUG c.e.mybatis.mapper.TUserMapper.selectByPrimaryKey - <== Total: 1
    true

     

    一级缓存 是 sqlSession的, 一级 缓存是线程级别的

    优先从 二级缓存【线程共享缓存】 查找,然后再查找一级缓存【线程独享】, 没有,然后再查找数据库

     

     控制台上 出现 cache Hit  的 就是 二级缓存了,mybatis 会自己统计缓存命中率

     多个 mapper 可以通过 配置 cache-ref  配置同一个缓存数据源

    使用二级缓存,容易脏读,没有提交的数据被回滚了,你就用了没有提交的脏数据,一般用专业的第三方缓存根据在业务层控制

     

    使用 mybatis-spring 整合在一起

     

    mybatis 的异常 封装到了 spring 的 DataAccessException

        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="typeAliasesPackage" value="com.enjoylearning.mybatis.entity" />
            <property name="mapperLocations" value="classpath*:sqlmapper/*.xml" />
        </bean>
        <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.enjoylearning.mybatis.mapper" />
        </bean>

    总结:

        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="typeAliasesPackage" value="com.enjoylearning.mybatis.entity" />
            <property name="mapperLocations" value="classpath*:sqlmapper/*.xml" />
        </bean>
        <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.enjoylearning.mybatis.mapper" />
        </bean>
    
    
    
        <!-- (事务管理)transaction manager -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
            <qualifier value="transactionManager" />
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManager" />
    
    
        <context:component-scan base-package="com.*">
        </context:component-scan>

    还需要 添加一个 mybatis-config.xml 配置 mybatis 部分配置

    <?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>
        
        <properties resource="db.properties"/>
        
        <settings>
            
            
            <setting name="mapUnderscoreToCamelCase" value="true" />
            
            
        
        </settings>
    
        <!-- 别名定义 -->
        
        <typeAliases>
            <package name="com.enjoylearning.mybatis.entity"/>
        </typeAliases>
    
    
        <!--配置environment环境 -->
        <environments default="development">
            <!-- 环境配置1,每个SqlSessionFactory对应一个环境 -->
            <environment id="development">
                <transactionManager type="JDBC" />
                <dataSource type="UNPOOLED">
                    <property name="driver" value="${jdbc_driver}" />
                    <property name="url" value="${jdbc_url}" />
                    <property name="username" value="${jdbc_username}" />
                    <property name="password" value="${jdbc_password}" />
                </dataSource>
            </environment>
    
    
        </environments>
    
        <!-- 映射文件,mapper的配置文件 -->
        <mappers>
            <!--直接映射到相应的mapper文件 -->
            <mapper resource="sqlmapper/TUserMapper.xml" />
            <mapper resource="sqlmapper/TJobHistoryMapper.xml" /> 
            <!-- <mapper class="com.enjoylearning.mybatis.mapper.TJobHistoryAnnoMapper"/> -->
        </mappers>
    
    </configuration>  

    完整整合配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:cache="http://www.springframework.org/schema/cache" xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context-4.3.xsd
                            http://www.springframework.org/schema/cache
                            http://www.springframework.org/schema/cache/spring-cache.xsd
                            http://www.springframework.org/schema/tx 
                              http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                              http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    
        <context:property-placeholder location="classpath:db.properties"
            ignore-unresolvable="true" />
    
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
            init-method="init" destroy-method="close">
            <!-- 基本属性 url、user、password -->
            <property name="driverClassName" value="${jdbc_driver}" />
            <property name="url" value="${jdbc_url}" />
            <property name="username" value="${jdbc_username}" />
            <property name="password" value="${jdbc_password}" />
    
            <!-- 配置初始化大小、最小、最大 -->
            <property name="initialSize" value="1" />
            <property name="minIdle" value="1" />
            <property name="maxActive" value="20" />
    
            <!-- 配置获取连接等待超时的时间 -->
            <property name="maxWait" value="60000" />
    
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
    
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="300000" />
    
            <property name="validationQuery" value="SELECT 'x'" />
            <property name="testWhileIdle" value="true" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
    
            <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
            <property name="poolPreparedStatements" value="true" />
            <property name="maxPoolPreparedStatementPerConnectionSize"
                value="20" />
            <!-- 配置监控统计拦截的filters -->
            <property name="filters" value="stat" />
        </bean>
    
    
        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="typeAliasesPackage" value="com.enjoylearning.mybatis.entity" />
            <property name="mapperLocations" value="classpath*:sqlmapper/*.xml" />
        </bean>
        <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.enjoylearning.mybatis.mapper" />
        </bean>
    
    
    
        <!-- (事务管理)transaction manager -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
            <qualifier value="transactionManager" />
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManager" />
    
    
        <context:component-scan base-package="com.*">
        </context:component-scan>
    
    </beans>
  • 相关阅读:
    python
    Yii框架的学习指南(策码秀才篇)1-1 如何认识Yii framework
    yii执行流程简单介绍
    html5页面编码如何确定
    防止表单重复提交的几种策略
    2维数组排序
    YII框架开发一个项目的通用目录结构:
    phpcms添加图片投票
    windows下面apache配置虚拟目录(测试使用,发布网站不建议目录访问)
    js获取浏览器的版本代码
  • 原文地址:https://www.cnblogs.com/lyr-2000/p/13860719.html
Copyright © 2020-2023  润新知