首页 > 代码库 > 初次使用Mybatis配置出现错误待解决

初次使用Mybatis配置出现错误待解决

数据库:

create table STUDENT
(
  STUID    NUMBER(9) not null,
  SNAME    VARCHAR2(50) not null,
  SEX      VARCHAR2(4),
  ADDRESS  VARCHAR2(50),
  BIRTHDAY DATE
)

 目录结构:

技术分享

Student类:

package com.bean;

import java.io.Serializable;
import java.util.Date;

import oracle.jdbc.driver.OracleDriver;

public class Student implements Serializable {	
	private Integer stuid;
	private String sname;
	private String sex;
	private String address;
	private Date birthday;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(Integer stuid, String sname, String sex, String address,
			Date birthday) {
		super();
		this.stuid = stuid;
		this.sname = sname;
		this.sex = sex;
		this.address = address;
		this.birthday = birthday;
	}
	public Student(String sname, String sex, String address, Date birthday) {
		super();
		this.sname = sname;
		this.sex = sex;
		this.address = address;
		this.birthday = birthday;
	}
	//set和get就不写了 
	
	
}

 IStudentMapper接口

package com.mapper;
import com.bean.*;
import java.util.*;
public interface IStudentMapper {
	public boolean save(Student st);
	public boolean update(Student st);
	public boolean delById(Integer sid);
	public Student findById(Integer sid);
	public List<Student> findAll();
}

 StudentMapper.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.mapper.IStudentMapper">
	<!-- Student返回类型映射声明 -->
	<resultMap type="com.bean.Student" id="studentResultMap">
	    <id property="stuid"  column="stuid"></id>
	    <result  property="sname" column="sname"></result> 	
	    <result  property="sex" column="sex"></result> 	
	    <result  property="address" column="address"></result> 	
	    <result  property="birthday" column="birthday"></result> 	
	</resultMap>
	<insert id="save" parameterType="com.bean.Student">
		insert into student values(seqstu.nextval,#{sname},#{sex},#{address},#{birthday})
	</insert>
	
	<update id="update" parameterType="com.bean.Student">
		update student set sname=#{sname},sex=#{sex},address=#{address},birthday=#{birthday} where stuid=#{stuid}
	</update>
	
	<delete id="delById" parameterType="java.lang.Integer">
		delete from  student where stuid=#{sid}
	</delete>
 
    <select id="findById" parameterType="java.lang.Integer" resultType="com.bean.Student">
		select * from student where stuid=#{sid} 
	</select>
 <!--
    <select id="findAll" resultMap="studentResultMap">
        select * from student where 1=1
    </select>
-->
</mapper>

 TastAdd类:

package com.test;
import com.bean.*;

import java.io.*;
import java.util.*;

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 com.mapper.*;
public class TastAdd {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			//获取资源文件的流
			InputStream in=Resources.getResourceAsStream("mybatis-config.xml");
			
			//获取SqlSessionFactory
			SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
			
			//获取sqlSession
			SqlSession session=sqlSessionFactory.openSession();
			
			IStudentMapper dao=session.getMapper(IStudentMapper.class);
			
			Student st=new Student("巨林", "男", "changshan", new Date());
			
			boolean flag= dao.save(st);
			if(flag){
				System.out.println("保存成功!");
				session.commit();
			}else{
				System.out.println("失败!");
				
			}
			session.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

}

 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="POOLED">
				<property name="driver" value="http://www.mamicode.com/oracle.jdbc.driver.OracleDriver" />
				<property name="url" value="http://www.mamicode.com/jdbc:oracle:thin:@localhost:1521:orcl" />
				<property name="username" value="http://www.mamicode.com/system" />
				<property name="password" value="http://www.mamicode.com/java" />
			</dataSource>
		</environment>
	</environments>
	
	<mappers>	
		<mapper resource="com/mapper/StudentMapper.xml" />
	</mappers>
	
</configuration>

 然后运行就出错了。。。。
望有大神给出有效建议!!

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/mapper/StudentMapper.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: 32; columnNumber: 10; 元素类型为 "mapper" 的内容必须匹配 "(cache-ref|cache|resultMap*|parameterMap*|sql*|insert*|update*|delete*|select*)+"。
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:82)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:66)
    at com.test.TastAdd.main(TastAdd.java:24)
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: 32; columnNumber: 10; 元素类型为 "mapper" 的内容必须匹配 "(cache-ref|cache|resultMap*|parameterMap*|sql*|insert*|update*|delete*|select*)+"。
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:115)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:95)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
    ... 2 more
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 32; columnNumber: 10; 元素类型为 "mapper" 的内容必须匹配 "(cache-ref|cache|resultMap*|parameterMap*|sql*|insert*|update*|delete*|select*)+"。
    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:339)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:113)
    ... 4 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 32; columnNumber: 10; 元素类型为 "mapper" 的内容必须匹配 "(cache-ref|cache|resultMap*|parameterMap*|sql*|insert*|update*|delete*|select*)+"。
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(XMLDTDValidator.java:1994)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(XMLDTDValidator.java:879)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1789)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2965)
    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:257)
    ... 8 more

初次使用Mybatis配置出现错误待解决