idea 无法将 mybatis 的配置文件放到 classes 中

2017-07-25 21:33:30 +08:00
 chenguoyu
这是我编译后的稳健,UserMapper.xml 没有放在 target 下的 classes 中,导致报错。自己将 UserMapper.xml 拷贝下来程序就可以运行。但是这终究不是解决的办法。
5444 次点击
所在节点    Java
16 条回复
chenguoyu
2017-07-25 21:34:05 +08:00
sunsulei
2017-07-25 21:37:26 +08:00
@chenguoyu 贴 mybatis.cfg.xml 配置文件
azygote
2017-07-25 21:40:41 +08:00
把 mapper 文件放到 resources 文件夹下,再在 mybatis.cfg.xml 里面改 mapper location。IDEA 编译后默认会把 resources 下的文件放到 target 的 classpath 下,但是 src 下的只有.java 文件编译生成.class 文件放入 classpath 下,其他文件会忽略的。
misaka19000
2017-07-25 21:41:27 +08:00
资源文件不是应该放在 resources 下吗,Maven 会帮你把 resources 文件夹的内容拷贝到 classes 文件夹下的
Cbdy
2017-07-25 21:41:28 +08:00
用构建工具控制构建
chenguoyu
2017-07-25 21:41:39 +08:00
@sunsulei
<configuration>
<!-- 配置 mybatis 运行环境 -->
<properties resource="jdbc.properties"/>
<typeAliases>
<package name="com.cgy.entity"></package>
</typeAliases>
<environments default="development">
<environment id="development">
<!-- type="JDBC" 代表直接使用 JDBC 的提交和回滚设置 -->
<transactionManager type="JDBC"></transactionManager>
<!-- POOLED 表示支持 JDBC 数据源连接池 -->
<!-- 数据库连接池,由 Mybatis 管理,数据库名是 mybatis,MySQL 用户名 root,密码为空 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.cgy.dao"></package>
</mappers>

</configuration>
wly19960911
2017-07-25 21:44:31 +08:00
把 xml 放进 resource 目录,properties 自己看看 mybatis 的 XML 怎么导入,通过 mapperLocations 指定一下目录。

然后 interface 上标注 @Mapper 注解,之后在配置类上使用 @MapperScan 扫描下 interface 即可注入。
chenguoyu
2017-07-25 21:44:53 +08:00
@azygote
@misaka19000
如果放在 resources 里面的话,其他人用 eclipse 开发不需要放到 eclipse 里面,这不就冲突了吗,这样就留没法提交到版本控制了啊
luban
2017-07-25 21:50:15 +08:00
maven 的话加插件,现在在路上,如果我回去你还没解决的话,给你发代码
mosliu
2017-07-25 21:56:32 +08:00
Pom.xml 里面 加上这个 文件就好了吧?

<resources>
<resource>
<directory>src/to/UserMapper.xml </directory>
</resource>
</resources>
xmh51
2017-07-25 21:57:05 +08:00
maven 可用 <resource>
<directory>${basedir}/src/main/java</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
解决
wly19960911
2017-07-25 21:58:11 +08:00
@chenguoyu 没看懂你的意思,你意思是其他人用 eclipse 没有 resource 文件夹?不是 maven 工程项目?
chenguoyu
2017-07-25 22:03:41 +08:00
@wly19960911 因为 eclipse 不需要把 mybatis 的 mapper.xml 放到 resources 中就可以编译到 classes 下,而 idea 如果全放到 resources 中就冲突了
chenguoyu
2017-07-25 22:11:18 +08:00
@mosliu 感谢已解决
在 pox.xml 中添加
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
就可以了
wly19960911
2017-07-25 22:21:00 +08:00
@chenguoyu 这个跟 idea 还是 eclipse 无关,完全看 maven 的配置,因为你的 spring boot 项目就是这样配置的…我也单纯配过普通的和你说的一样的,现在 spring boot 习惯了也就这样了。
MajorAdam
2017-07-26 10:29:21 +08:00
maven 配置

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

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

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

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

© 2021 V2EX