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" 必须后跟属性规范 ">" 或 "/>"。
求解答,谢谢!
一毛钱
相关分类