为了账号安全,请及时绑定邮箱和手机立即绑定

mybatis 中configuration.xml报错

mybatis 中configuration.xml报错

慕粉4163093 2016-11-29 23:10:06
configuration.xml代码:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>      <environments default="development" >        <environment id="development" >            <transactionManager type="JDBC" />            <dataSource type="POOLED">                <property name="driver" value="com.mysql.jdbc.Driver" />                <property name="url" value="jdbc:mysql://localhost:3306/test" />                <property name="username" value="root" />                <property name="password" value="123" />            </dataSource>        </environment>    </environments>    <mappers>        <mapper resource="mybatistest/Trainee.xml"/>    </mappers></configuration>映射文件Trainee.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="mybatistest">         <select id="selectUserById" parameterType="int" resultType="mybatistest.Trainee">          select * from person where id=#{id}         </select>   </mapper>测试类Test.java代码:package mybatistest;import java.io.IOException;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import mybatistest.Trainee;public class Test { public static void main(String[] args){  SqlSessionFactory factory=null;  try{   factory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatistest/Configuration.xml"));  }catch(IOException e){   e.printStackTrace();  }  SqlSession sqlSession=factory.openSession();  Trainee t=(Trainee)sqlSession.selectOne("mybatis.selectUserById", 5);  System.out.println(t.getName()); }}目录结构:不做任何修改,关掉重新打开eclipse,在eclipse中会报出两种错误:一种是在声明<!DOCTYE...>行报“content is not allowed in prolog.”另一种是在代码<environments default="development" > 行报:Element type "environments" must be followed by either attribute specifications, ">" or "/>".运行结果:第一种:找不到configuration.xml第二种:Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 18; 元素类型 "environments" 必须后跟属性规范 ">" 或 "/>"。求解答,谢谢!
查看完整描述

3 回答

?
一毛钱

TA贡献156条经验 获得超57个赞

Class.getResourceAsStream(String path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。 

所以你应该改为factory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("Configuration.xml"));

查看完整回答
反对 回复 2016-11-30
  • 慕粉4163093
    慕粉4163093
    是xml的问题,在编译时就报错,如图,实际上我的xml并没有错。
  • 3 回答
  • 0 关注
  • 3854 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信