ibatis resultclass "java.util.hashmap" 缓存问题

在做ibatis项目过程中遇到如下样式动态查询

<select id="searchByKW"      resultClass="java.util.HashMap"   
        parameterClass="java.util.HashMap">   
        <![CDATA[ 
     select * from $username$.$tablename$ where contains($username$.$tablename$.$colname$,'$kw$')>0 
     ]]>   
</select>  

 

这样的可以对不同表进行查询,返回结果映射到HashMap中,但是使用中发现当第一次使用这个语句时是可以的,但是当使用这个语句再查询别的表时报错了,这个问题是因为你查询的sql的列是变化的,但是ibatis默认的会缓存RS中的meta信息,如果你第一次查询的列和第二次查询的列不一样的话,那么第二次ibatis还会以第一次查询的列为key从RS里面获取数据,但是你的列是变化的,所以第二次取数据的时候,RS里面已经没有了你第一次的那个列了,所以会出错。 错误原因是从结果集到HashMap的映射时还按照上个表的字段进行装载,说明ibatis对上个表的结构进行了映射缓存。

使用remapResults这个属性,可以重新映射结果集。

配置文件修改如下

<select id="searchByKW"  remapResults="true"   resultClass="java.util.HashMap"    parameterClass="java.util.HashMap">   
    <![CDATA[ 
         select * from $username$.$tablename$ where contains($username$.$tablename$.$colname$,'$kw$')>0 
     ]]>   
</select>   
 

 

 

 

#spring: # jpa: # hibernate: # ddl-auto: none # ★★记得上线时改回来 # show-sql: false # database-platform: org.hibernate.dialect.Oracle10gDialect # open-in-view: false # datasource: # url: jdbc:oracle:thin:@192.168.88.26:1521:orcl # driver-class-name: oracle.jdbc.OracleDriver # username: dba_mgr # password: totodir #hikari数据库连接池 # hikari: # pool-name: Retail_HikariCP # minimum-idle: 5 #最小空闲连接数量 # idle-timeout: 300000 #空闲连接存活最大时间,默认600000(10分钟) # maximum-pool-size: 10 #连接池最大连接数,默认是10 # auto-commit: true #此属性控制从池返回的连接的默认自动提交行为,默认值:true # max-lifetime: 600000 #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟 # connection-timeout: 30000 #数据库连接超时时间,默认30秒,即30000 # connection-test-query: select 1 from dual #server: # servlet: # session: # timeout: 300m # port: 7086 # undertow: # url-charset: UTF-8 #logging: # file: # path: /hyerpinterfacelog # name: hyerpinterface.log server: port: 7086 servlet: session: timeout: 1800s spring: #jpa: # show-sql: true # properties: # hibernate: # format_sql: true datasource: dynamic: primary: oracle strict: false datasource: syoracle: url: jdbc:oracle:thin:@192.168.88.26:1521:orcl username: dba_mgr password: totodir driver-class-name: oracle.jdbc.OracleDriver druid: initial-size: 10 min-idle: 10 max-active: 500 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 lyoracle: url: jdbc:oracle:thin:@192.168.88.30:1521:orcl username: dba_mgr password: totodir driver-class-name: oracle.jdbc.OracleDriver druid: initial-size: 10 min-idle: 10 max-active: 500 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 mybatis-plus: global-config: db-config: # 主键ID类型 # id-type: none logic-delete-field: deleted logic-delete-value: 1 logic-not-delete-value: 0 configuration: # 驼峰下划线转换 map-underscore-to-camel-case: true # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl import com.alibaba.druid.pool.DruidDataSource; import com.hyerp.hyerpinterface.config.DynamicDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.sql.DataSource; import java.util.HashMap; import java.util.Map; @Configuration public class DataSourceConfig { // 沈阳数据源 (0101) @Bean("syOracleDataSource") @ConfigurationProperties(prefix = "spring.datasource.syoracle") public DataSource syOracleDataSource() { return new DruidDataSource(); // 使用Druid连接池 } // 洛阳数据源 (0102) @Bean("lyOracleDataSource") @ConfigurationProperties(prefix = "spring.datasource.lyoracle") public DataSource lyOracleDataSource() { return new DruidDataSource(); // 使用Druid连接池 } // 主数据源(动态路由) @Primary @Bean("dynamicDataSource") public DataSource dynamicDataSource() { Map<Object, Object> targetDataSources = new HashMap<>(2); // 使用部门编码作为数据源标识 targetDataSources.put("-5716907732163998808", syOracleDataSource()); // 沈阳连接 targetDataSources.put("4294613579574035536", syOracleDataSource()); // 沈阳传感 targetDataSources.put("0103", lyOracleDataSource()); // 洛阳 // 默认使用沈阳数据源 return new DynamicDataSource(syOracleDataSource(), targetDataSources); } }package com.hyerp.hyerpinterface.dao; import com.baomidou.dynamic.datasource.annotation.DS; import com.hyerp.hyerpinterface.domain.entity.OaItemAbove; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import javax.transaction.Transactional; import java.util.List; import java.util.Optional; /** * @Author GGZ * @Date 2023/3/20 14:06 * @Version 1.5 */ public interface OaitemRepo extends JpaRepository<OaItemAbove, String> { List<OaItemAbove> findByIdnum(String Idnum); List<OaItemAbove> findByShowvalueAndRef(String showvalue, String refid); List<OaItemAbove> findByShowvalue(String showvalue); @Transactional @DS("syoracle") @Query(value = "SELECT SHOWVALUE FROM OACTP_ENUM_ITEM WHERE IDNUM = ?", nativeQuery = true) Optional<OaItemAbove> selectidnumop(@Param("idnum") String idnum); @Transactional @DS("syoracle") @Query(value = "SELECT SHOWVALUE FROM OACTP_ENUM_ITEM WHERE IDNUM = 8347348803478963605", nativeQuery = true) } } 该sql语句在数据库能查到SELECT SHOWVALUE FROM OACTP_ENUM_ITEM WHERE IDNUM = 8347348803478963605在程序中查询是空怎么解决
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值