2 asd5629626 asd5629626 于 2017.08.30 10:50 提问

spring+mybatis 使用mapper配置无法加载properties 文件

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




<!-- spring4.3以后,这里引入的用户名变量,不能是username -->









value="com.yz.assurance.dao" />

3个回答

asd5629626
asd5629626   2017.08.30 10:51
<context:annotation-config></context:annotation-config>
<!-- 扫描包 -->
<context:component-scan base-package="com.yz.assurance">
    <context:exclude-filter type="annotation"
                            expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--引入properties文件 -->
<context:property-placeholder location="classpath*:jdbc.properties"/>
<!--aop注解扫描开启-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${mydriver}"></property>
    <property name="url" value="${myurl}"></property>
    <!-- spring4.3以后,这里引入的用户名变量,不能是username -->
    <property name="username" value="${myuser}"></property>
    <property name="password" value="${mypwd}"></property>
</bean>
<!-- sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:mybatis_cfg.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <property name="basePackage"
              value="com.yz.assurance.dao" />
</bean>

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

<!-- 事务处理注解开启 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

Small_Mouse0
Small_Mouse0   Ds   Rxr 2017.08.30 11:09

按住ctrl能不能点进去,,,或者出现下划线,,,不能的话,,去掉“*”,,试试

要能够点进去,,不行的话换相对路径试试

 <!--引入properties文件 -->
<context:property-placeholder location="classpath*:jdbc.properties"/>
qq_34197553
qq_34197553   2017.08.31 10:02

MyBatis的配置应该是这样子的:

 <!--添加扫描的驱动支持-->
    <context:annotation-config/>
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.ssm"/>

    <!-- 加载一个properties文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${driverClasss}"/>
        <property name="url" value="${jdbcUrl}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:mapping/*.xml"/>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.ssm.dao"/>
    </bean>

我觉得是你的propertyConfigurer的配置出了问题

Csdn user default icon
上传中...
上传图片
插入图片