applicationContext.xml里面配置了Mapper 自动扫描
<bean class=”org.mybatis.spring.mapper.MapperScannerConfigurer”>
<property name=”basePackage” value=”com.yolly.platform.*” />
</bean>
然后定义了一个userMapper,里面就只有一个方法
@Repository(value=”userMapper”)
public interface UserMapper {
public List<User> getAll();
}
下面是userMapper.xml
<mapper namespace=”com.yolly.platform.user.mapper.UserMapper”>
<select id=”getAll” resultType=”user”>
select * from user
</select>
</mapper>
下面是mybatis 配置文件
<configuration>
<settings>
<setting name=”cacheEnabled” value=”true” />
</settings>
<typeAliases>
<typeAlias alias=”user” type=”com.yolly.platform.user.entity.User”/>
</typeAliases>
<mappers>
<mapper resource=”mappers/UserMapper.xml” />
</mappers>
</configuration>
本人业务层定义了一个接口 UserService 然后 下面是实现类
@Component(value=”userServiceImpl”)
@Transactional
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
@Autowired
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public List<User> getAll() {
return userMapper.getAll();
}
}
本人spring 配置文件加载完成后运行下面的代码可以获取结果
UserServiceImpl userServiceImpl = (UserServiceImpl)PlatformContext.getApplicationContext().getBean(“userServiceImpl”);
List<User> lista = userServiceImpl.getAll();
但是本人现在又写了一个类 叫UserComponent 如下:
@Component(value=”userComponent”)
public class UserComponent {
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
public List<User> getAll(Head head){
return userService.getAll();
}
}
本人这个UserService直接注入了
UserComponent userComponent = (UserComponent)PlatformContext.getApplicationContext().getBean(“userComponent”);
List<User> list = userComponent.getAll();
本人像这样调用的时候就不行了,只是多加了一层!
异常信息:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.yolly.platform.user.service.UserService.getAll
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:178)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:38)
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:49)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:42)
at $Proxy12.getAll(Unknown Source)
at com.yolly.platform.user.component.UserComponent.getAll(UserComponent.java:25)
at com.yolly.core.main.Server$ServerHandler.messageReceived(Server.java:101)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.messageReceived(DefaultIoFilterChain.java:690)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:417)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1200(DefaultIoFilterChain.java:47)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:765)
at org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:74)
at org.apache.mina.core.session.IoEvent.run(IoEvent.java:63)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTask(OrderedThreadPoolExecutor.java:769)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTasks(OrderedThreadPoolExecutor.java:761)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.run(OrderedThreadPoolExecutor.java:703)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.yolly.platform.user.service.UserService.getAll
at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:775)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:615)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:608)
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:176)
… 16 more
select * from user
</select>
修改为
<select id=”getAll” resultMap=”user”>
select * from user
</select>
获得了启示:新建的mapping包要引入mybatis配置文件中,接受自动扫描。
public void setUserService(UserService userService) {
this.userService = userService;
}
应该是这里错了啊
用@Resource byName来注入啊
下面是完整的解释
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以假如使用name属性,则使用byName的自动注入策略