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

2017-05-07 10:20:00 +08:00
 sweetcode

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

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

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

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

但是我在 Main 类中用 getBean 的方式获取就获取不到了
Lonely
2017-05-07 10:56:56 +08:00
web.xml 里没配好
sweetcode
2017-05-07 11:42:15 +08:00
@Lonely 应该不会吧,我等下回去检查下
Ouyangan
2017-05-07 12:17:52 +08:00
不要问这么傻的问题
cs4814751
2017-05-07 12:42:15 +08:00
mappersLocation 你用了通配符* 所以必须是 classpath*: 不然 mapper 找不到
sweetcode
2017-05-07 12:59:58 +08:00
@cs4814751 这样子的?加了通配符就要用带星的?我去查查资料
Ouyangan
2017-05-07 15:38:37 +08:00
@cs4814751 #11 不是这个原因吧 , 我这边项目中就是跟楼主这样配置的
```
<constructor-arg name="mapperLocations" value="classpath:mapper/*.xml"/>
```
sweetcode
2017-05-07 17:46:27 +08:00
@Ouyangan 我刚才查了下 classpath* 和 classpath 的区别不是在于是否用了通配符啊
sweetcode
2017-05-07 18:25:17 +08:00
@Ouyangan
@cs4814751
@Lonely
@nutting
解决了= =!很迷。。。
用的 idea 在 facets 里删掉 spring 立马好

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/359651

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX