代码结构:
spring.xml:
<import resource="classpath:spring/spring-jdbc.xml"/>
<context:component-scan base-package="com.my.test.spring"/>
spring-jdbc.xml:
<context:property-placeholder location="classpath:conf/jdbc.properties" file-encoding="UTF-8"/>
<bean id="dataSourceCW" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceCW"/>
<property name="configLocation" value="classpath:mybatis/configuration.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.my.test.spring.ioc.mapper"/>
</bean>
configuration.xml只配置了驼峰映射
<configuration>
<settings>
<!-- 驼峰自动映射-->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
测试类
public class TestMapperBean {
@Test
public void testMapper111(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring.xml");
TestMapper mapper = context.getBean("testMapper", TestMapper.class);
List<TestBean> beanList = mapper.getTestBeanList();//报错代码
System.out.println("test id and number is : " + JSON.toJSONString(beanList));
}
}
相关类和xml
public interface TestMapper {
List<TestBean> getTestBeanList();
}
<mapper namespace="com.my.test.spring.ioc.mapper.TestMapper">
<select id="getTestBeanList" resultType="com.my.test.spring.ioc.bean.TestBean">
select * from test
</select>
</mapper>
加载时被过滤掉了