<bean id="wspSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="wspdataSource" />
<property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
</bean>
<bean id="wspDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="wspSqlSessionFactory"/>
<property name="mapperInterface" value="com.lodige.clcs.dao.WSPDao" />
</bean>
我想wspSqlSessionFactory
从java访问上面,下面是代码:
ApplicationContext ctx = new ClassPathXmlApplicationContext("/WEB-INF/dispatcher-servlet.xml");
Object myBean = (SqlSessionFactory) ctx.getBean("dashbSqlSessionFactory");
Configuration configuration = ((SqlSessionFactory) myBean).getConfiguration();
Collection<Cache> caches = configuration.getCaches();
//Cache cache = configuration.getCache("src.main.domain.SysKeyEQ"); // namespace of particular XML
//cache.clear();
//If you have multiple caches and want a particular to get deleted.
for (Cache cache : caches) {
System.out.println("cache Name: "+caches);
Lock w = cache.getReadWriteLock().writeLock();
w.lock();
try {
//cache.clear();
} finally {
w.unlock();
}
}
我是否正确地获得了 mybatis 会话,或者我需要以不同的方式进行。如果我做得正确,即使我在上述路径中有调度程序-servlet,我仍然会得到文件未找到异常。即使我只是尝试过"dispatcher-servlet.xml"
可能的解决方案:
@Autowired
@Qualifier("wspSqlSessionFactory")
private SqlSessionFactoryBean wspSqlSessionFactory;
Configuration configuration = wspSqlSessionFactory.getObject().getConfiguration();
Collection<Cache> caches = configuration.getCaches();
for (Cache cache : caches) {
System.out.println("cache Name: "+cache);
Lock w = cache.getReadWriteLock().writeLock();
w.lock();
try {
cache.clear();
} finally {
w.unlock();
}
}
虽然我得到了缓存并且只有在我删除时才能清除它们
Lock w = cache.getReadWriteLock().writeLock();
w.lock();
try {
cache.clear();
} finally {
w.unlock();
}
并直接做cache.clear();
其他我在行中得到空指针异常:Lock w = cache.getReadWriteLock().writeLock();
为什么在那一行是 NPE 可以/推荐不使用锁。