gpt4 book ai didi

Java Mybatis Xml 配置错误

转载 作者:行者123 更新时间:2023-11-29 16:36:09 27 4
gpt4 key购买 nike

我一直在尝试通过遵循几个教程使用 Mybatis 框架连接到 mysql 数据库。主要的是http://webappdevelopmentlab.com/mybatis-tutorial-quick-start-guide/

我两天来一直在尝试解决这个问题,但无法确定这是我的配置问题还是映射器 xml 中的语法问题。我在 mysql 中创建的数据库的列名称与 java 类中的属性名称相同。

该错误表明它可能位于映射器 xml 中,但我没有发现它有任何问题。

这仅使用标准 java 项目,而不是 Maven。

这是我的文件 Project File Structure

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>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>

<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/pmsdb"/>
<property name="username" value="root"/>
<property name="password" value="!Theold5075"/>
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource="resource/DeliverableMapper.xml"/>
</mappers>

</configuration>

DeliverableMapper.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">

<mapper namespace="resource.DeliverableMapper.xml">


<resultMap id="deliverableMap" type="domains.Deliverable">

<id property="ID" column="ID" javaType="int" jdbcType="NUMERIC"/>
<result property="name" column="name" javaType="String" jdbcType="VARCHAR"/>
<result property="description" column="description" javaType="String" jdbcType="VARCHAR"/>
<result property="dueDate" column="dueDate" javaType="java.util.Date" jdbcType="DATE"/>

</resultMap>


<select id="selectAllDeliverables" resultMap="deliverableMap">
select * from deliverables
<select/>


</mapper>

可交付成果.java

public class Deliverable {

private int ID;
private String name;
private String description;
private Date dueDate;

//private TreeSet<Requirement> listOfRequirements;
//private TreeSet<Task> listOfTasks;

@SuppressWarnings("deprecation")
public Deliverable(){
ID = 0;
name = "PLACEHOLDER";
description = "PLACEHOLDER";
dueDate = new Date(2019,8,5);
}

public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@SuppressWarnings("deprecation")
public String getDueDate() {
return dueDate.getYear() + "-" + dueDate.getMonth() + "-" + dueDate.getDate();
}

@SuppressWarnings("deprecation")
public void setDueDate(int year, int month, int day){
dueDate.setYear(year);
dueDate.setDate(day);
dueDate.setMonth(month);
}
}

这是我得到的错误

Exception in thread "main"   org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in resource/DeliverableMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 52; Document root element "mapper", must match DOCTYPE root "configuration".
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
at drivers.DBController.main(DBController.java:23)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 52; Document root element "mapper", must match DOCTYPE root "configuration".
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
... 2 more
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 52; Document root element "mapper", must match DOCTYPE root "configuration".
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:259)
at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:125)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.<init>(XMLMapperBuilder.java:78)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:372)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
... 4 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 52; Document root element "mapper", must match DOCTYPE root "configuration".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.rootElementSpecified(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:257)
... 8 more

最佳答案

标签mapper中的属性namespace应该是mapper的接口(interface),这里应该是data.DeliverableMapper
像这样:

<mapper namespace="data.DeliverableMapper">

关于Java Mybatis Xml 配置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53603933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com