Spring 整合 mybatis 的配置文件一定要命名为 spring-mybatis.xml 么? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sweetcode
V2EX    问与答

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

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

    我一开始的文件名是: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 一下以后还是找不到...
    Finest
        2
    Finest  
       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     Solana     2805 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 00:28 PVG 08:28 LAX 17:28 JFK 20:28
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86