我是用mybatis替换jdbc做的Java web项目,现在的报错如下所示。并且诡异的是:我用单元测试测试自己的代码没问题 但是用tomcat启动项目,查询数据库就报如下的错误。
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 27; columnNumber: 17; XML 文档结构必须从头至尾包含在同一个实体内。
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:54)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:38)
at com.syl.demo.util.MybatisUtil.<clinit>(MybatisUtil.java:17)
at com.syl.demo.service.DeptService.getDeptInfo(DeptService.java:21)
at com.syl.demo.action.UserAction.doGet(UserAction.java:67)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2521)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2510)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 27; columnNumber: 17; XML 文档结构必须从头至尾包含在同一个实体内。
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:256)
at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:120)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.<init>(XMLConfigBuilder.java:63)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:51)
... 27 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 27; columnNumber: 17; XML 文档结构必须从头至尾包含在同一个实体内。
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1436)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.endEntity(XMLDocumentFragmentScannerImpl.java:903)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.endEntity(XMLDocumentScannerImpl.java:563)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.endEntity(XMLEntityManager.java:1384)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1774)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanData(XMLEntityScanner.java:1252)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanComment(XMLScanner.java:778)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanComment(XMLDocumentFragmentScannerImpl.java:1038)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2988)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:254)
... 30 more
这是报错。
以下是我的配置
mybatis-config.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>
<!--定义别名-->
<typeAliases>
<typeAlias alias="dept" type="com.syl.demo.pojo.Dept"></typeAlias>
</typeAliases>
<!-- 对事务的管理和连接池的配置 -->
<environments default="development">
<environment id="development">
<!--采用JDBC数据库事务管理-->
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/demo_02?useSSL=true" />
<property name="username" value="syl_demo_02" />
<property name="password" value="song" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/syl/demo/dao/mapper/deptMapper.xml" />
</mappers>
</configuration>
deptMapper.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.syl.demo.dao.DeptDao">
<sql id="Base_Column_List" >
dept_id as deptId,dept_name as deptName,dept_up as deptUp
</sql>
<select id="getAllDeptInfo" resultType="dept" >
select
<include refid="Base_Column_List" />
from dept
</select>
<select id="getDeptInfo" resultType="dept" >
select
<include refid="Base_Column_List" />
from dept
where dept_id=#{deptId}
</select>
<insert id="createDept" parameterType="dept" keyColumn="dept_id" >
INSERT INTO dept (dept_id,dept_name,dept_up) VALUES (#{deptId},#{deptName},#{deptUp})
</insert>
</mapper>
mybatis工具类:MybatisUtil .java
package com.syl.demo.util;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtil {
public static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
//public static SqlSession session ;
static {
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public MybatisUtil () {
}
public static SqlSession getSession () {
return sqlSessionFactory.openSession();
}
}
mybatis 工具类的测试正常
MybatisUtilTest .java
package com.syl.demo.util;
import com.syl.demo.dao.DeptDao;
import com.syl.demo.dao.NoticeDao;
import com.syl.demo.pojo.Dept;
import com.syl.demo.pojo.Notice;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class MybatisUtilTest {
@Test
public void testMybatis(){
SqlSession session = null;
try {
session =MybatisUtil.getSession();
DeptDao deptDao = session.getMapper(DeptDao.class);
List<Dept> dept =deptDao.getAllDeptInfo();
System.out.println(dept.toString());
/* Dept d = new Dept();
d.setDeptId("20170909001");
d.setDeptName("第一支部一分部");
d.setDeptUp("20170909");
deptDao.createDept(d);
session.commit();*/
List<Notice> notice ;
session = MybatisUtil.getSession();
NoticeDao noticeDao = session.getMapper(NoticeDao.class);
notice =noticeDao.getNoticeObject();
System.out.println(notice.toString());
} finally {
session.close();
}
}
}
但是在Tomcat启动后,运行业务类就不正常,报上面的错误:
DeptService .java 这个类就普通的servlet调用
package com.syl.demo.service;
import com.syl.demo.dao.DeptDao;
import com.syl.demo.pojo.Dept;
import com.syl.demo.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
public class DeptService extends CommonService {
@Override
public String ObjectToJson(Object object){
return super.ObjectToJson(object);
}
public Dept getDeptInfo (String dept_id) {
SqlSession session = null;
Dept dept;
try {
session = MybatisUtil.getSession();
DeptDao deptDao = session.getMapper(DeptDao.class);
dept = deptDao.getDeptInfo(dept_id);
System.out.println(dept.toString());
} finally {
session.close();
}
return dept;
}
}
该错误出现的原因是mybatis对应的mybatis-config.xml中的文字注释"<!--文字-->",文字后面无空格,正确注释方式应该是文字前后都需要空格如"<!--文字注释-->"
请去掉所有注释
谢谢您解决了我的问题版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。