mybatis遇到问题解决方法---- Error building SqlSession.

在学完spring后(“春”,当然这不是真的春啊),会发现好多东西都不用亲手去写了,在框架之中开发的确是省了不少事,当然代码方面省事了,配置文件就需要稍微费点事,相比之下呢,我更喜欢后者。

说到这里呢,就不得不提一下当下SSM主流框架(spring,spring-mvc,mybatis),当然,这个大家都知道,所以就不再赘述。今天说一下常遇到的一个问题:Error building SqlSession.(创建会话出错)

闲话不多说,大家看一下事故现场,下面的场面太大了,怕你没耐心看,所以在这里说明问题出在哪里,很容易我们可以看到Error building SqlSession.字样,那么为什么会出现这样的情况呢?

### Error building SqlSession.

### The error may exist in config/mapper/stuMapper.xml

### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause:……

我们接着看下一行,The error may exist in config/mapper/stuMapper.xml,这就说明我们的映射文件出错了,而且这种错误八成是配置的包名+类名+类出现了问题。以我的工程为例,大家请看,是不是发现了,改过来之后就可以正常运行了。

出错代码如下图:

正确代码如下图:

这个时候再运行程序就ok了。

 下面附上事故现场:

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in config/mapper/stuMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'config/mapper/stuMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.zzxtit.boot.mybatis.hello.Student'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.zzxtit.boot.mybatis.hello.Student
    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 com.zzxtit.boot.mybatis.first.FirstMain.init(FirstMain.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'config/mapper/stuMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.zzxtit.boot.mybatis.hello.Student'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.zzxtit.boot.mybatis.hello.Student
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:98)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
    ... 25 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'config/mapper/stuMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.zzxtit.boot.mybatis.hello.Student'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.zzxtit.boot.mybatis.hello.Student
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:122)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:94)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:373)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
    ... 27 more
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.zzxtit.boot.mybatis.hello.Student'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.zzxtit.boot.mybatis.hello.Student
    at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:118)
    at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:74)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:137)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:130)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:120)
    ... 30 more
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.zzxtit.boot.mybatis.hello.Student'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.zzxtit.boot.mybatis.hello.Student
    at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
    at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149)
    at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116)
    ... 34 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: com.zzxtit.boot.mybatis.hello.Student
    at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
    at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
    at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
    at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116)
    ... 36 more

展开阅读全文

### Error building SqlSession. ### The error may exist in SQL Mapper Configurati

03-07

mybatis运行抛出异常:rn[code=text]rnException in thread "main" org.apache.ibatis.exceptions.PersistenceException: rn### Error building SqlSession.rn### The error may exist in SQL Mapper Configurationrn### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。rn at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)rn at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:50)rn at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:29)rn at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:17)rn at chapter1.Tabdemo2Test.main(Tabdemo2Test.java:23)rnCaused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。rn at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:217)rn at org.apache.ibatis.parsing.XPathParser.(XPathParser.java:86)rn at org.apache.ibatis.builder.xml.XMLConfigBuilder.(XMLConfigBuilder.java:61)rn at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:46)rn ... 3 morernCaused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。rn at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:701)rn at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:435)rn at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1895)rn at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanData(XMLEntityScanner.java:1375)rn at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanComment(XMLScanner.java:801)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanComment(XMLDocumentFragmentScannerImpl.java:1034)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2982)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)rn at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)rn at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)rn at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)rn at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:215)rn ... 6 morernrn[/code]rnrn代码结构:rn[img=https://img-bbs.csdn.net/upload/201803/07/1520431823_513485.png][/img]rn具体代码如下:rnmybatis-config.xml文件rn[code=text]rnrnrnrnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rnrn[/code]rntabdemo2Mapper.xml文件:rn[code=text]rnrnrnrn rn select * from tabdemo2rn rnrn[/code]rnItabdemo2Mapper.java rn[code=text]rnpackage chapter1;rnrnimport java.util.List;rnrnpublic interface Itabdemo2Mapper rn rn public abstract List selectAll ();rnrnrnrn[/code]rnTabdemo2Model.javarn[code=text]rnpackage chapter1;rnrnpublic class Tabdemo2Model rn private Integer t2num;rn private String t2name;rn private Integer t2age;rn rn rn public Tabdemo2Model() rn super();rn rn public Tabdemo2Model(Integer t2num, String t2name, Integer t2age) rn super();rn this.t2num = t2num;rn this.t2name = t2name;rn this.t2age = t2age;rn rn // get和set方法rn // toString方法rnrnrn[/code]rntabdemo2表结构rn[img=https://img-bbs.csdn.net/upload/201803/07/1520431809_433508.png][/img]rnTabdemo2Test.java rn[code=text]rnpackage chapter1;rnrnimport java.io.IOException;rnimport java.util.List;rnrnimport org.apache.ibatis.io.Resources;rnimport org.apache.ibatis.session.SqlSession;rnimport org.apache.ibatis.session.SqlSessionFactoryBuilder;rnrnpublic class Tabdemo2Test rnrn public static void main(String[] args) throws IOException rn rn SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();rn SqlSession ss = ssfb.build(Resources.getResourceAsReader("mybatis-config.xml")).openSession();rn rn Itabdemo2Mapper tabdemo2 = ss.getMapper(Itabdemo2Mapper.class);rn List ll = tabdemo2.selectAll();rn rn for(int i = 0; i < ll.size(); i++)rn System.out.println(ll.get(i));rn rn rn ss.close();rn rn rnrnrn[/code]rnrn 论坛

没有更多推荐了,返回首页