java - 问: Spring怎么在一个biz层的实现类里使用多个dao层的mapper的接口?
PHPz
PHPz 2017-04-18 09:55:04
[Java讨论组]


这个是我的方法所在的模块的配置:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName" default-lazy-init="true">


 
    
        classpath:mybatis-jdbc.properties
    




    
        ${database.test.connection}
    
    
        ${conn.initial.size}
    
    
        ${conn.max.idle}
    
    
        ${conn.min.idle}
    
    
        ${conn.max.wait}
    
    
        ${conn.max.active}
    
    
        ${conn.remove.abandoned.timeout}
    
    
        ${conn.time.between.eviction.runs.millis}
    
    
        ${conn.min.evictable.idle.time.millis}
    
    
        ${conn.default.auto.commit}
    
    
        ${conn.log.abandoned}
    
    
        ${conn.remove.abandoned}
    
    
        ${conn.test.on.borrow}
    
    
        ${conn.test.on.return}
    
    
        ${conn.test.while.idle}
    




    



    



    
        
    



    
        



    



    



    
    
        
            
        
    
    
        classpath:mybatis-config.xml
    


    
          



    
    






这个是Biz层的配置:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

 
 
 
 
 
 
 
 
 
 
 
 

这个是dao层的配置:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

 
 
     
 

其他方法都没有问题。但是就是当调用多个接口的时候出现错误

出错方法如下:
@Component
public class EsbExcelHandler {

private static EsbExcelHandler excelHandler = new EsbExcelHandler();
private int flag = 0;//用于标识
@Autowired
private SoaCfgMessageControlMapper soaCfgMessageControlMapper;    
@Autowired
private SoaCfgMessageListMapper soaCfgMessageListMapper;
@Autowired
private SoaCfgServListMapper soaCfgServListMapper;
@Autowired
private SoaCfgMessageMetaDataMapper soaCfgMessageMetaDataMapper;
@Autowired
private SoaCfgMessageStructureMapper soaCfgMessageStructureMapper;

public int importMsgHeaderExcel(File file) {

    try {
        int res  = parseMsgHeaderExcel(file);
        if(res>0){
            flag = 1;
            return flag;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return flag;
}

public int parseMsgHeaderExcel(File importFile) throws Exception {
    //....解析文件代码省略
    // 报文头对象
    List listMsgCtrl = new ArrayList();
    // 报文字段表
    List listmetaData = new ArrayList();
    SoaCfgMessageControl msgCtrl = new SoaCfgMessageControl();
    SoaCfgMessageMetaData metaData = new SoaCfgMessageMetaData();
    //取出appsSysId
        for(int i = 0;i

exampleMsgCtrl.createCriteria().andAppsSysIdEqualTo(appsSysId);

            try{
                if(exampleMsgCtrl != null){
                    int ctrlRes =soaCfgMessageControlMapper.countByExample(exampleMsgCtrl);//出错。
                    System.out.println("一共有:"+ctrlRes+"条记录");
            }
        }catch(Exception e){
            flag = 0;
            System.out.println("请检查数据库连接或者sql语句,导入操作失败!!!");
            e.printStackTrace();
        }
    }
        return flag;
}    


PHPz
PHPz

学习是最好的投资!

全部回复(2)
大家讲道理

把你的配置po上来,理论上你单个接口可以正常调用,多个也不会报错

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.XXXXXXX.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    加上这个再试下
巴扎黑

Mapper接口你使用bean注解了吗,比如@Service 或者 @Component ?

看了下你贴出的配置,没有找到你spring 扫描包的配置,可以加上这个试试
<context:component-scan base-package="com.ibs.clearing"/>

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号