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.使用扫描接口方式:

<!--&lt;!&ndash; 5.配置mybatisDao接口扫描MapperScannerConfigurer &ndash;&gt;-->
    <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 }

 

posted on 2019-03-04 14:07  小白coder  阅读(2819)  评论(0编辑  收藏  举报