数据库:
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 findAll();
}
StudentMapper.xml:
insert into student values(seqstu.nextval,#{sname},#{sex},#{address},#{birthday})
update student set sname=#{sname},sex=#{sex},address=#{address},birthday=#{birthday} where stuid=#{stuid}
delete from student where stuid=#{sid}
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:
然后运行就出错了。。。。
望有大神给出有效建议!!
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.
at org.apache.ibatis.builder.xml.XMLMapperBuilder.
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