V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chenguoyu
V2EX  ›  Java

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

  •  
  •   chenguoyu · 2017-07-25 21:33:30 +08:00 · 5429 次点击
    这是一个创建于 2469 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这是我编译后的稳健,UserMapper.xml 没有放在 target 下的 classes 中,导致报错。自己将 UserMapper.xml 拷贝下来程序就可以运行。但是这终究不是解决的办法。
    16 条回复    2017-07-26 10:29:21 +08:00
    chenguoyu
        1
    chenguoyu  
    OP
       2017-07-25 21:34:05 +08:00
    sunsulei
        2
    sunsulei  
       2017-07-25 21:37:26 +08:00
    @chenguoyu 贴 mybatis.cfg.xml 配置文件
    azygote
        3
    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
        4
    misaka19000  
       2017-07-25 21:41:27 +08:00
    资源文件不是应该放在 resources 下吗,Maven 会帮你把 resources 文件夹的内容拷贝到 classes 文件夹下的
    Cbdy
        5
    Cbdy  
       2017-07-25 21:41:28 +08:00 via Android
    用构建工具控制构建
    chenguoyu
        6
    chenguoyu  
    OP
       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
        7
    wly19960911  
       2017-07-25 21:44:31 +08:00 via Android
    把 xml 放进 resource 目录,properties 自己看看 mybatis 的 XML 怎么导入,通过 mapperLocations 指定一下目录。

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

    <resources>
    <resource>
    <directory>src/to/UserMapper.xml </directory>
    </resource>
    </resources>
    xmh51
        11
    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
        12
    wly19960911  
       2017-07-25 21:58:11 +08:00 via Android
    @chenguoyu 没看懂你的意思,你意思是其他人用 eclipse 没有 resource 文件夹?不是 maven 工程项目?
    chenguoyu
        13
    chenguoyu  
    OP
       2017-07-25 22:03:41 +08:00
    @wly19960911 因为 eclipse 不需要把 mybatis 的 mapper.xml 放到 resources 中就可以编译到 classes 下,而 idea 如果全放到 resources 中就冲突了
    chenguoyu
        14
    chenguoyu  
    OP
       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
        15
    wly19960911  
       2017-07-25 22:21:00 +08:00 via Android
    @chenguoyu 这个跟 idea 还是 eclipse 无关,完全看 maven 的配置,因为你的 spring boot 项目就是这样配置的…我也单纯配过普通的和你说的一样的,现在 spring boot 习惯了也就这样了。
    MajorAdam
        16
    MajorAdam  
       2017-07-26 10:29:21 +08:00
    maven 配置
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4697 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 04:01 · PVG 12:01 · LAX 21:01 · JFK 00:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.