V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
sweetcode
V2EX  ›  问与答

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

  •  
  •   sweetcode · 2017-05-07 10:20:00 +08:00 · 4024 次点击
    这是一个创建于 2548 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

    15 条回复    2017-05-07 18:25:17 +08:00
    sweetcode
        1
    sweetcode  
    OP
       2017-05-07 10:37:46 +08:00
    更正一下,refresh 一下以后还是找不到...
    hand515
        2
    hand515  
       2017-05-07 10:45:58 +08:00
    你配置问题
    sweetcode
        3
    sweetcode  
    OP
       2017-05-07 10:50:46 +08:00
    @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
        4
    nutting  
       2017-05-07 10:50:56 +08:00 via Android
    web.xml 里配置 spring.xml 路径,然后 spring 里包含各个子配置文件,名字随便吧,只是为了清晰。你这是配置内容的问题
    sweetcode
        5
    sweetcode  
    OP
       2017-05-07 10:51:04 +08:00
    @hand515 有哪里有问题么?
    sweetcode
        6
    sweetcode  
    OP
       2017-05-07 10:53:11 +08:00
    @nutting 我这么配有问题么?
    sweetcode
        7
    sweetcode  
    OP
       2017-05-07 10:56:06 +08:00
    @nutting
    @hand515
    Junit 里面我加载了
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:config/spring-mybatis.xml")
    @Autowired

    但是我在 Main 类中用 getBean 的方式获取就获取不到了
    Lonely
        8
    Lonely  
       2017-05-07 10:56:56 +08:00 via iPhone
    web.xml 里没配好
    sweetcode
        9
    sweetcode  
    OP
       2017-05-07 11:42:15 +08:00 via Android
    @Lonely 应该不会吧,我等下回去检查下
    Ouyangan
        10
    Ouyangan  
       2017-05-07 12:17:52 +08:00
    不要问这么傻的问题
    cs4814751
        11
    cs4814751  
       2017-05-07 12:42:15 +08:00
    mappersLocation 你用了通配符* 所以必须是 classpath*: 不然 mapper 找不到
    sweetcode
        12
    sweetcode  
    OP
       2017-05-07 12:59:58 +08:00 via Android
    @cs4814751 这样子的?加了通配符就要用带星的?我去查查资料
    Ouyangan
        13
    Ouyangan  
       2017-05-07 15:38:37 +08:00
    @cs4814751 #11 不是这个原因吧 , 我这边项目中就是跟楼主这样配置的
    ```
    <constructor-arg name="mapperLocations" value="classpath:mapper/*.xml"/>
    ```
    sweetcode
        14
    sweetcode  
    OP
       2017-05-07 17:46:27 +08:00
    @Ouyangan 我刚才查了下 classpath* 和 classpath 的区别不是在于是否用了通配符啊
    sweetcode
        15
    sweetcode  
    OP
       2017-05-07 18:25:17 +08:00
    @Ouyangan
    @cs4814751
    @Lonely
    @nutting
    解决了= =!很迷。。。
    用的 idea 在 facets 里删掉 spring 立马好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5635 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 03:19 · PVG 11:19 · LAX 20:19 · JFK 23:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.