idea 无法将 mybatis 的配置文件放到 classes 中 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chenguoyu
V2EX    Java

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

  •  
  •   chenguoyu 2017-07-25 21:33:30 +08:00 5890 次点击
    这是一个创建于 3010 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这是我编译后的稳健,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
    lotmany
        2
    lotmany  
       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>

    &t;/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     Solana     5400 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 36ms UTC 07:34 PVG 15:34 LAX 00:34 JFK 03:34
    Do have faith in what you're doing.
    ubao msn 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