1.使用模板方式:
1 <!--使用模板类实现mybatis --> 2 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 3 <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> 4 <constructor-arg name="executorType" value="BATCH"></constructor-arg> 5 <constructor-arg name="exceptionTranslator" ref="myBatisExceptionTranslator"></constructor-arg> 6 </bean>
后台的调用:
1 public bookServiceImpl implements bookService{ 2 3 @Autowired 4 public SqlSessionTemplate sqlSession;//注入sqlsessionTemplate 5 6 public List<book> getBookInformation(book param){ 7 logger.info("getBookInformation()"+param.toString()); 8 bookMapper mapper = sqlSession.getMapper(bookMapper.class);//调用mapper接口 9 List<book> bookList=mapper.queryBookInfo(param); 10 11 return bookList; 12 } 13 }
2.使用扫描接口方式:
<!--<!– 5.配置mybatisDao接口扫描MapperScannerConfigurer –>--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.alice.dao"></property> </bean>
后台调用:
1 public class UserServiceImpl implements IUserService { 2 3 @Autowired 4 private UserMapper mapper; //直接注入mapper dao接口的bean 5 6 public List<User> selectAll(String keyword1,String keyword2) { 7 8 return mapper.selectAll(keyword1,keyword2); 9 } 10 }
原文地址:https://www.cnblogs.com/alice-cj/p/10470342.html
时间: 2024-10-09 23:13:24