首页 新闻 赞助 找找看

springboot + mybatis, mapper.xml 报错"前言中不允许有内容"

0
悬赏园豆:5 [已解决问题] 解决于 2022-10-12 16:26

百度过了,都说是 xml 文件的编码问题,但是各种文本编辑器都试过转为无 BOM 的UTF8编码,都照样报这个错。最后,在CentOS下,用Java程序生成的 xml 再下载后复制到对应目录下,错误一模一样。

真不知道咋办了!

xml 内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hynn.ems.models.db.mapper.TestMapper">
    <resultMap id="testResult" type="com.hynn.ems.models.db.Test">
        <result column="Id" jdbcType="varchar" property="id" />
        <result column="Creator_Id" jdbcType="bigint" property="creatorId" />
        <result column="Create_Time" jdbcType="datetime" property="createTime" />
        <result column="Updater_Id" jdbcType="bigint" property="updaterId" />
        <result column="Update_Time" jdbcType="datetime" property="updateTime" />
        <result column="Name" jdbcType="varchar" property="name" />
    </resultMap>
    <select id="findById" resultMap="testResult">
        Select * From Test Where Id = #{id}
    </select>
</mapper>

 

在 CentOS 下生成 xml 的代码:

package test;

import java.io.*;

public class WriteFile {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\r\n");
        sb.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">").append("\r\n");
        sb.append("<mapper namespace=\"com.hynn.ems.models.db.mapper.TestMapper\">").append("\r\n");
        sb.append("    <resultMap id=\"testResult\" type=\"com.hynn.ems.models.db.Test\">").append("\r\n");
        sb.append("        <result column=\"Id\" jdbcType=\"varchar\" property=\"id\" />").append("\r\n");
        sb.append("        <result column=\"Creator_Id\" jdbcType=\"bigint\" property=\"creatorId\" />").append("\r\n");
        sb.append("        <result column=\"Create_Time\" jdbcType=\"datetime\" property=\"createTime\" />").append("\r\n");
        sb.append("        <result column=\"Updater_Id\" jdbcType=\"bigint\" property=\"updaterId\" />").append("\r\n");
        sb.append("        <result column=\"Update_Time\" jdbcType=\"datetime\" property=\"updateTime\" />").append("\r\n");
        sb.append("        <result column=\"Name\" jdbcType=\"varchar\" property=\"name\" />").append("\r\n");
        sb.append("    </resultMap>").append("\r\n");
        sb.append("    <select id=\"findById\" resultMap=\"testResult\">").append("\r\n");
        sb.append("        Select * From Test Where Id = #{id}").append("\r\n");
        sb.append("    </select>").append("\r\n");
        sb.append("</mapper>").append("\r\n");
        String content = sb.toString();
        
        try(FileOutputStream fos = new FileOutputStream("D:\\UserMapping.xml")){
            byte[] data = content.getBytes("UTF8");
            fos.write(data, 0, data.length);
        }catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
背锅狼的主页 背锅狼 | 初学一级 | 园豆:63
提问于:2022-10-11 15:11
< >
分享
最佳答案
0
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--Mybatis-Plus——使用 Wrapper 自定义SQL: https://www.bilibili.com/read/cv14498117-->
<mapper namespace="com.zhike.blogdao.mapper.ArticleMapper">

    <select id="selectArticleByUserId" resultType="com.zhike.blogpojo.DO.Article">
        SELECT a.* FROM `article` a
        INNER JOIN `article_type` b
        ON b.`Id`=a.`ArticleTypeId`
        WHERE `UserId`=#{userId}

    </select>
</mapper>

收获园豆:5
智客工坊 | 小虾三级 |园豆:1855 | 2022-10-11 21:45
其他回答(1)
0

自己解决了

原来是 application.yml 配置不对,原来是:

mybatis:
  mapper-locations: classpath:mapping
其中 mapping 是目录,结果 spring 把这个目录下的文件名当作xml内容来解析,改成 mapping/*.xml 就没事了
背锅狼 | 园豆:63 (初学一级) | 2022-10-12 16:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册