BUG—— Mybatis
Posted JohnnyLin00
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BUG—— Mybatis相关的知识,希望对你有一定的参考价值。
ClassNotFoundException
### Error building SqlSession.
### The error may exist in com/johnny/entity/personMapper.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 'com/johnny/entity/personMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Person'. Cause: java.lang.ClassNotFoundException: Cannot find class: Person
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
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.johnny.personMapper">
<select id="selectPersonById" resultType="Person">
select * from person where id = #id
</select>
</mapper>
正解
<?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.johnny.personMapper">
<select id="selectPersonById" resultType="com.johnny.entity.Person">
select * from person where id = #id
</select>
</mapper>
xxxMapper is not known to the MapperRegistry.
Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com.johnny.mapper.PersonMapper is not known to the MapperRegistry.
at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:779)
at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:291)
at com.johnny.test.TestMybatis.selectAllPerson(TestMybatis.java:36)
at com.johnny.test.TestMybatis.main(TestMybatis.java:87)
将personMapper.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.johnny.PersonMapper">
<select id="selectPersonById" resultType="com.johnny.entity.Person">
select * from person where id = #id
</select>
<select id="selectAllPerson" resultType="com.johnny.entity.Person">
select * from person
</select>
<insert id="addPerson" parameterType="com.johnny.entity.Person">
insert into person(id, age, name) values( #id, #age, #name)
</insert>
<update id="updatePerson" parameterType ="com.johnny.entity.Person">
update person set name = #name, age =#age where id = #id
</update>
<delete id="deletePersonById" parameterType ="int">
delete from person where id=#id
</delete>
</mapper>
正解
<?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.johnny.mapper.PersonMapper"> <!-- 该mapper的命名空间要为对应接口的全类名 -->
<select id="selectPersonById" resultType="com.johnny.entity.Person">
select * from person where id = #id
</select>
<select id="selectAllPerson" resultType="com.johnny.entity.Person">
select * from person
</select>
<insert id="addPerson" parameterType="com.johnny.entity.Person">
insert into person(id, age, name) values( #id, #age, #name)
</insert>
<update id="updatePerson" parameterType ="com.johnny.entity.Person">
update person set name = #name, age =#age where id = #id
</update>
<delete id="deletePersonById" parameterType ="int">
delete from person where id=#id
</delete>
</mapper>
“configuration” 的内容必须匹配 “(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)”。
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 58; columnNumber: 17; 元素类型为 "configuration" 的内容必须匹配 "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)"。
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.johnny.test.TestMybatis.queryNationByIdWithPerson(TestMybatis.java:72)
at com.johnny.test.TestMybatis.main(TestMybatis.java:89)
按照提示
configuration标签下的子标签也有顺序的,必须按照以下顺序出现:
(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)"
开启了log4j日志, 控制台依然看不到日志输出
原因是:日志级别调为了ERRO ,只看得到ERRO及以上的错误信息。改成DEBUG即可
日志级别: DEBUG < INFO< WARN < ERRO
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
以上是关于BUG—— Mybatis的主要内容,如果未能解决你的问题,请参考以下文章