sweetcode
V2EX  ›  问与答

Spring 整合 mybatis 的配置文件一定要命名为 spring-mybatis.xml 么?

  •  
  •   sweetcode · May 7, 2017 · 4929 views
    This topic created in 3470 days ago, the information mentioned may be changed or developed.

    我一开始的文件名是:spring-config.xml

    根据官方文档的指导一步步配置 sqlsessionfactory 之类的配置后,在 service 层自动装配的时候就提示找不到 bean,我一开始以为是哪里错了,检查了半天没问题啊,结果后来我改了个名字,spring-mybatis.xml 就神奇的好了,这是啥缘故?

    必须要指定叫做这个文件名么?哪个参数能设置这个加载路径的?

    15 replies  •  2017-05-07 18:25:17 +08:00
    sweetcode
    sweetcode
    OP
    May 7, 2017
    1
    更正一下,refresh 一下以后还是找不到...
    Finest
    Finest
    May 7, 2017
    2
    你配置问题
    sweetcode
    sweetcode
    OP
    May 7, 2017
    3
    @hand515
    ```
    <?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.sweetcode" />

    <context:annotation-config />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://localhost:3306/sweetcode?characterEncoding=UTF-8</value>

    </property>
    <property name="username">
    <value>root</value>
    </property>
    <property name="password">
    <value>5201314666</value>
    </property>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath:com/sweetcode/mapper/*.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.sweetcode.mapper"/>
    </bean>

    </beans>
    ```
    nutting
    nutting
    May 7, 2017 via Android
    4
    web.xml 里配置 spring.xml 路径,然后 spring 里包含各个子配置文件,名字随便吧,只是为了清晰。你这是配置内容的问题
    sweetcode
    sweetcode
    OP
    May 7, 2017
    5
    @hand515 有哪里有问题么?
    sweetcode
    sweetcode
    OP
    May 7, 2017
    6
    @nutting 我这么配有问题么?
    sweetcode
    sweetcode
    OP
    May 7, 2017
    7
    @nutting
    @hand515
    Junit 里面我加载了
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:config/spring-mybatis.xml")
    @Autowired

    但是我在 Main 类中用 getBean 的方式获取就获取不到了
    Lonely
    Lonely
    May 7, 2017 via iPhone
    8
    web.xml 里没配好
    sweetcode
    sweetcode
    OP
    May 7, 2017 via Android
    9
    @Lonely 应该不会吧,我等下回去检查下
    Ouyangan
    Ouyangan
    May 7, 2017
    10
    不要问这么傻的问题
    cs4814751
    cs4814751
    May 7, 2017
    11
    mappersLocation 你用了通配符* 所以必须是 classpath*: 不然 mapper 找不到
    sweetcode
    sweetcode
    OP
    May 7, 2017 via Android
    12
    @cs4814751 这样子的?加了通配符就要用带星的?我去查查资料
    Ouyangan
    Ouyangan
    May 7, 2017
    13
    @cs4814751 #11 不是这个原因吧 , 我这边项目中就是跟楼主这样配置的
    ```
    <constructor-arg name="mapperLocations" value="classpath:mapper/*.xml"/>
    ```
    sweetcode
    sweetcode
    OP
    May 7, 2017
    14
    @Ouyangan 我刚才查了下 classpath* 和 classpath 的区别不是在于是否用了通配符啊
    sweetcode
    sweetcode
    OP
    May 7, 2017
    15
    @Ouyangan
    @cs4814751
    @Lonely
    @nutting
    解决了= =!很迷。。。
    用的 idea 在 facets 里删掉 spring 立马好
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   929 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 22:48 · PVG 06:48 · LAX 14:48 · JFK 17:48
    ♥ Do have faith in what you're doing.