java - 问: Spring怎么在一个biz层的实现类里使用多个dao层的mapper的接口?

查看:123
本文介绍了java - 问: Spring怎么在一个biz层的实现类里使用多个dao层的mapper的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题


这个是我的方法所在的模块的配置:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

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">

<!--1. 配置jdbc.properties文件的位置信息 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location">
        <value>classpath:mybatis-jdbc.properties</value>
    </property>
</bean>

<!--2. 配置数据源,读取jdbc.properties文件,其中{}中的名称就是上面jdbc.properties文件中对应的字段名称 。  -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${database.driver}" p:url="${database.url}" p:username="${database.username}" p:password="${database.password}">
    <property name="validationQuery">
        <value>${database.test.connection}</value>
    </property>
    <property name="initialSize">
        <value>${conn.initial.size}</value>
    </property>
    <property name="maxIdle">
        <value>${conn.max.idle}</value>
    </property>
    <property name="minIdle">
        <value>${conn.min.idle}</value>
    </property>
    <property name="maxWait">
        <value>${conn.max.wait}</value>
    </property>
    <property name="maxActive">
        <value>${conn.max.active}</value>
    </property>
    <property name="removeAbandonedTimeout">
        <value>${conn.remove.abandoned.timeout}</value>
    </property>
    <property name="timeBetweenEvictionRunsMillis">
        <value>${conn.time.between.eviction.runs.millis}</value>
    </property>
    <property name="minEvictableIdleTimeMillis">
        <value>${conn.min.evictable.idle.time.millis}</value>
    </property>
    <property name="defaultAutoCommit">
        <value>${conn.default.auto.commit}</value>
    </property>
    <property name="logAbandoned">
        <value>${conn.log.abandoned}</value>
    </property>
    <property name="removeAbandoned">
        <value>${conn.remove.abandoned}</value>
    </property>
    <property name="testOnBorrow">
        <value>${conn.test.on.borrow}</value>
    </property>
    <property name="testOnReturn">
        <value>${conn.test.on.return}</value>
    </property>
    <property name="testWhileIdle">
        <value>${conn.test.while.idle}</value>
    </property>
</bean>

<!-- 事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager" />
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="do*" rollback-for="Throwable" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="serviceTransaction" expression="execution(* com.ibm.*.*.biz.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceTransaction"/>    
</aop:config>

<bean id="dialectHelper" class="com.ibm.ais.base.mybatis.dialect.helper.DialectHelperImpl" scope="singleton">
    <property name="dbType" value="${database.type}" />
</bean>

<bean id="pagination" class="com.ibm.ais.base.mybatis.plugin.PaginationInterceptor">
    <property name="dialectHelper" ref="dialectHelper" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="plugins">
        <array>
            <ref bean="pagination" />
        </array>
    </property>
    <property name="configLocation">
        <value>classpath:mybatis-config.xml</value>
    </property>
</bean>

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">    
      <constructor-arg index="0" ref="sqlSessionFactory" />    
</bean>

<bean id="basicDao" class="com.ibm.ais.base.mybatis.basic.dao.BasicDaoSupport" abstract="true">
    <property name="dialectHelper" ref="dialectHelper" />
    <property name="sqlSessionTemplate" ref="sqlSessionTemplate" />
</bean>

<bean id="springContextUtil" class="com.ibm.ais.base.mybatis.util.SpringContextUtil" scope="singleton" />

<import resource="com/ibm/ais/tool/codegenerator/META-INF/config/spring-biz-ToolCodeGenerator.xml"/>
<import resource="com/ibm/ais/tool/codegenerator/META-INF/config/spring-dao-ToolCodeGenerator.xml"/>

</beans>
这个是Biz层的配置:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

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"> 

<bean id="imtpServiceSoaCfgMultiMessageList" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgMultiMessageListImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgMessageControl" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgMessageControlImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgServStructure" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgServStructureImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgMessageList" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgMessageListImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgMultiResMsg" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgMultiResMsgImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgMessageMetaData" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgMessageMetaDataImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgMultiReqMsg" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgMultiReqMsgImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgMessageStructure" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgMessageStructureImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgServList" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgServListImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgServStructureSub" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgServStructureSubImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgServConstant" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgServConstantImpl" scope="singleton" /> 
<bean id="imtpServiceSoaCfgServAuthorization" class="com.ibs.clearing.imtp.service.biz.impl.ImtpSoaCfgServAuthorizationImpl" scope="singleton" /> 

</beans>

这个是dao层的配置:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

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"> 

<!-- scan for Dao/mappers and let them be autowired --> 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
    <property name="basePackage" value="com.ibs.clearing.imtp.service.dao.mapper" /> 
</bean> 

</beans>

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

出错方法如下:
@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<SoaCfgMessageControl> listMsgCtrl = new ArrayList<SoaCfgMessageControl>();
    // 报文字段表
    List<SoaCfgMessageMetaData> listmetaData = new ArrayList<SoaCfgMessageMetaData>();
    SoaCfgMessageControl msgCtrl = new SoaCfgMessageControl();
    SoaCfgMessageMetaData metaData = new SoaCfgMessageMetaData();
    //取出appsSysId
        for(int i = 0;i<listMsgCtrl.size();i++){
            String appsSysId = listMsgCtrl.get(i).getAppsSysId();// 获得主键 应用系统代码
            //通过appsSysId查询是否有 记录
            SoaCfgMessageControlExample exampleMsgCtrl = new SoaCfgMessageControlExample();
            SoaCfgMessageMetaDataExample exampleMetaData = new SoaCfgMessageMetaDataExample();
            //查询Control表中的记录

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;
}    


解决方案

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

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

这篇关于java - 问: Spring怎么在一个biz层的实现类里使用多个dao层的mapper的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆