首页 新闻 会员 周边

mybatis,在association,collection使用select,如何跨xml引用select语句

0
悬赏园豆:20 [待解决问题]

详细一点说:

  就是能不能在association,collection中的select属性中引用另外一个xml中定义的select语句?例如

teacher表的select定义

<select id="getTeacherById" resultMap="teacherResultMap" resultType="String">
      SELECT *
      FROM TEACHER_TBL
      WHERE TEACHER_ID = #{teacherID}
</select>

class表的resultMap定义

 

<resultMap type="ClassEntity" id="classResultMap">
  <id property="classId" column="CLASS_ID" />
  <result property="className" column="CLASS_NAME" />
  <result property="classYear" column="CLASS_YEAR" />
  <association property="teacherEntity" column="TEACHER_ID"
    select="getTeacherById" />

</resultMap>

 

错误的意思好像是当前命名空间找不到select

 

## Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.citicbank.km.mis.dao.ClassMapper.getTeacherById
### The error may involve com.citicbank.km.mis.dao.ClassMapper.getClassAllbyClassId-Inline
### The error occurred while setting parameters
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.citicbank.km.mis.dao.ClassMapper.getTeacherById, mergedContextConfiguration = [MergedContextConfiguration@1abe675c testClass = MybatisTest, locations = '{classpath:spring-config.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], class dirties context [false], class mode [null], method dirties context [false].
[org.springframework.test.context.support.DirtiesContextTestExecutionListener]After test class: context [DefaultTestContext@5abc30ad testClass = MybatisTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1abe675c testClass = MybatisTest, locations = '{classpath:spring-config.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], dirtiesContext [false].
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.386 sec <<< FAILURE!
[org.springframework.context.support.GenericApplicationContext]Closing org.springframework.context.support.GenericApplicationContext@c25b3d5: startup date [Fri Aug 29 23:42:48 CST 2014]; root of context hierarchy
[org.springframework.beans.factory.support.DefaultListableBeanFactory]Returning cached instance of singleton bean 'lifecycleProcessor'
[org.springframework.beans.factory.support.DefaultListableBeanFactory]Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2c9b0a66: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,dataSource,transactionManager,sqlSessionFactory,org.mybatis.spring.mapper.MapperScannerConfigurer#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor,classMapper,studentMapper,teacherMapper]; root of factory hierarchy
[org.springframework.beans.factory.support.DefaultListableBeanFactory]Retrieved dependent beans for bean 'classMapper': [com.citicbank.km.mis.test.MybatisTest]
[org.springframework.beans.factory.support.DefaultListableBeanFactory]Retrieved dependent beans for bean 'dataSource': [sqlSessionFactory]
[org.springframework.beans.factory.support.DefaultListableBeanFactory]Retrieved dependent beans for bean 'sqlSessionFactory': [org.mybatis.spring.mapper.MapperScannerConfigurer#0]
[org.springframework.beans.factory.support.DisposableBeanAdapter]Invoking destroy method 'close' on bean with name 'dataSource'
[com.alibaba.druid.pool.DruidDataSource]{dataSource-1} closed

Pride Kin的主页 Pride Kin | 初学一级 | 园豆:164
提问于:2014-08-29 23:43
< >
分享
所有回答(3)
2

那你是用namespace.id试试

也就是假设<select id="getTeacherById">所在的xml文件的命名空间为A,那么就写作A.getTeacherById

乐享程序员 | 园豆:930 (小虾三级) | 2014-09-22 13:18

行不通...

支持(0) 反对(0) 比你大怪我咯 | 园豆:202 (菜鸟二级) | 2016-03-28 15:33
0

解决了吗  我也超好奇这个问题!!

比你大怪我咯 | 园豆:202 (菜鸟二级) | 2016-03-28 15:32
0

可以使用select语句,但是要为该语句的全包名

千里之外kb | 园豆:202 (菜鸟二级) | 2019-04-21 15:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册