mybatis-spring整合总结05_Injecting Mappers
Injecting Mappers
DAO除了手动使用SqlSessionDaoSupport或是SqlSessionTemplate之外,Mybatis-Spring提供了另外一种方法,使用代理工厂—MapperFactoryBean。它允许你直接将map接口注入到Service beans中去,而不用编写实现类(DAOImpl)。因为Mybatis-Spring会为你创建代理。
将mapper加入Spring的配置方法:
<bean id="userMapper" value="org.mybatis.spring.sample.mapper.UserMapper" /><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>
<bean id="fooService" ref="userMapper" /></bean>
public class FooServiceImpl implements FooService {private UserMapper userMapper;public void setUserMapper(UserMapper userMapper) {this.userMapper = userMapper;}public User doSomeBusinessStuff(String userId) {return this.userMapper.getUser(userId);}}