my_sunshine_y 2016-08-04 04:33 采纳率: 0%
浏览 1745
已结题

spring+mybatis+redis 开启缓存后出现MybatisSystemException

错误提示:
图片说明
Mapper.xml
图片说明
spring-mybatis.xml:

 <?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <!-- 自动扫描 -->
    <context:component-scan base-package="com.yc.mybank" />
    <context:annotation-config></context:annotation-config>
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <!-- 抽象bean,在這里配置所有的通用 otomikos聯接池配置屬性 -->
    <bean id="abstractXADataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean"
        init-method="init" destroy-method="close" abstract="true">
        <property name="poolSize" value="3" />
        <property name="minPoolSize" value="1" />
        <property name="maxPoolSize" value="10" />
        <property name="borrowConnectionTimeout" value="60" />
        <property name="reapTimeout" value="20" />
        <!-- 最大空闲时间 -->
        <property name="maxIdleTime" value="60" />
        <property name="maintenanceInterval" value="60" />
        <property name="loginTimeout" value="60" />
    </bean>

    <bean id="OdataSource" parent="abstractXADataSource">
        <!-- value只要两个数据源不同就行,随便取名 -->
        <property name="uniqueResourceName" value="oracle/db" />
        <property name="user">
            <value>${uname}</value>
        </property>
        <property name="password">
            <value>${pwd}</value>
        </property>
        <property name="url">
            <value>${url}</value>
        </property>
        <property name="driverClassName">
            <value>${driverName}</value>
        </property>
    </bean>
    <bean id="SdataSource" parent="abstractXADataSource">
        <!-- value只要两个数据源不同就行,随便取名 -->
        <property name="uniqueResourceName" value="mysql" />
        <property name="user">
            <value>${mysql.uname}</value>
        </property>
        <property name="password">
            <value>${mysql.pwd}</value>
        </property>
        <property name="url">
            <value>${mysql.url}</value>
        </property>
        <property name="driverClassName">
            <value>${mysql.driverName}</value>
        </property>
    </bean>
    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="OdataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations"
            value="classpath:com/yc/mybank/mapper/userMapper.xml"></property>
        <property name="configurationProperties">
            <props>
                <prop key="cacheEnabled">true</prop>
                <prop key="lazyLoadingEnabled">false</prop>
                <prop key="aggressiveLazyLoading">true</prop>
            </props>
        </property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="SdataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations"
            value="classpath:com/yc/mybank/mapper/userMapper.xml"></property>
        <property name="configurationProperties">
            <props>
                <prop key="cacheEnabled">true</prop>
                <prop key="lazyLoadingEnabled">false</prop>
                <prop key="aggressiveLazyLoading">true</prop>
            </props>
        </property>
    </bean>

    <!-- 创建sqlSession -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!-- 只能通过 构造方法注入 sqlSessionFactory -->
        <constructor-arg ref="sqlSessionFactory"></constructor-arg>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.yc.mybank.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

</beans>  

单元测试类代码:

 package mybank_springMVC;

import javax.annotation.Resource;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.yc.mybank.bean.BankAccount;
import com.yc.mybank.biz.UserBiz;
import com.yc.mybank.dao.UserDao;
import com.yc.mybank.dao.cache.RedisCache;
import com.yc.mybank.dao.cache.RedisPool;

@RunWith(SpringJUnit4ClassRunner.class)     //表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class Test {

    @Resource
    private UserBiz userBiz;

    @Resource
    private SqlSessionTemplate session;

    @Resource
    private UserDao userDao;


    @org.junit.Test
    public void testSqlsession(){
        //System.out.println(userBiz);
        //System.out.println(session);
        //System.out.println(userDao);


        System.out.println(RedisCache.createRedis());
        //System.out.println(RedisCache.createRedis());
        System.out.println(userBiz.getUser(3));
        //ApplicationContext ac=new ClassPathXmlApplicationContext("spring-mybatis.xml");
        //System.err.println(ac.getBean("userDao"));



    }
}

Redis代码:

 package com.yc.mybank.dao.cache;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ibatis.cache.Cache;
import com.sun.org.apache.xml.internal.utils.SerializableLocatorImpl;
import com.yc.mybank.util.SerializableUtil;

import redis.clients.jedis.Jedis;

public class RedisCache implements Cache {

    private String id;
    private Jedis jedisClient;
    //同步锁
    private ReadWriteLock readWriteLock=new ReentrantReadWriteLock();
     public RedisCache(String id) {
         if(id==null){
             throw new RuntimeException("cache instance required an ID");
         }
        this.jedisClient=createRedis();
        this.id=id;
    }
     /**
      * 将缓存中的数据删掉
      */
    public void clear() {
        // TODO Auto-generated method stub
        this.jedisClient.flushDB();
    }
    public String getId() {
        // TODO Auto-generated method stub
        return id;
    }
    /**
     * 通过key到缓存中取值
     */
    public Object getObject(Object key) {
        // TODO Auto-generated method stub
        byte[] valueBytes=SerializableUtil.serialize(key);
        Object obj=SerializableUtil.unserializable(valueBytes);
        return obj;
    }
    public static Jedis createRedis(){
        Jedis jedis=RedisPool.getPool().getResource();      
        return jedis;
    }
    /**
     * 同步锁
     */
    public ReadWriteLock getReadWriteLock() {
        // TODO Auto-generated method stub
        return readWriteLock;
    }
    public int getSize() {
        Long size=this.jedisClient.dbSize();
        int re=Integer.valueOf(size+"");
        return re;
    }

    public void putObject(Object key, Object value) {
        // TODO Auto-generated method stub
        byte[] keyByte=SerializableUtil.serialize(key);
        byte[] valueByte=SerializableUtil.serialize(value);
        this.jedisClient.set(keyByte, valueByte);

    }

    public Object removeObject(Object key) {
        byte[] keyByte=SerializableUtil.serialize(key); 
        return this.jedisClient.expire(keyByte,0);
    }

}

redis测试通过没问题,能取得到,在不开启缓存的情况下业务能进行

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题