实现mapper接口注入的两种方式,以及后台的使用区别

时间:2024-01-10 15:40:26

1.使用模板方式:

 <!--使用模板类实现mybatis -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
<constructor-arg name="exceptionTranslator" ref="myBatisExceptionTranslator"></constructor-arg>
</bean>

后台的调用:

 public bookServiceImpl implements bookService{

    @Autowired
public SqlSessionTemplate sqlSession;//注入sqlsessionTemplate public List<book> getBookInformation(book param){
logger.info("getBookInformation()"+param.toString());
bookMapper mapper = sqlSession.getMapper(bookMapper.class);//调用mapper接口
List<book> bookList=mapper.queryBookInfo(param); return bookList;
}
}

2.使用扫描接口方式:

<!--&lt;!&ndash; 5.配置mybatisDao接口扫描MapperScannerConfigurer &ndash;&gt;-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.alice.dao"></property>
</bean>

后台调用:

 public class UserServiceImpl implements IUserService {

     @Autowired
private UserMapper mapper; //直接注入mapper dao接口的bean public List<User> selectAll(String keyword1,String keyword2) { return mapper.selectAll(keyword1,keyword2);
}
}

相关文章