An error occurred while registering the MyBtis annotation method.
I recently learned about MyBatis and learned how to add, delete, modify, and query in Annotation mode. I am confused for a long time and finally find a solution to the problem!
Use this method when registering with the configuration file after writing the annotation Interface
Method 1
<mapper class="com.liuyunlong.mybatis.annotation.UserMapper" />
The problem arises. An exception is thrown as follows:
Org. apache. ibatis. exceptions. persistenceException: ### Error building SqlSession. ### Cause: org. apache. ibatis. builder. builderException: Error creating document instance. cause: org. xml. sax. SAXParseException; lineNumber: 26; columnNumber: 66; must be of the element type "mapper" declared attribute "class ". At org. apache. ibatis. exceptions. exceptionFactory. wrapException (ExceptionFactory. java: 8) at org. apache. ibatis. session. sqlSessionFactoryBuilder. build (SqlSessionFactoryBuilder. java: 60) at org. apache. ibatis. session. sqlSessionFactoryBuilder. build (SqlSessionFactoryBuilder. java: 44) at com. liuyunlong. mybatis. utils. myBatisUtil. getSession (MyBatisUtil. java: 22) at com. liuyunlong. mybatis. test. lesson3.add (Lesson3.ja Va: 20) 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: 47) at org. junit. internal. runners. model. reflectiveCallable. run (Reflecti VeCallable. java: 12) at org. junit. runners. model. frameworkMethod. invokeExplosively (FrameworkMethod. java: 44) at org. junit. internal. runners. statements. invokeMethod. evaluate (InvokeMethod. java: 17) at org. junit. runners. parentRunner. runLeaf (ParentRunner. java: 271) at org. junit. runners. blockJUnit4ClassRunner. runChild (BlockJUnit4ClassRunner. java: 70) at org. junit. runners. blockJUnit4ClassRunner. runChild (BlockJUnit4 ClassRunner. java: 50) at org. junit. runners. parentRunner $ 3.run( ParentRunner. java: 238) at org. junit. runners. parentRunner $1. schedule (ParentRunner. java: 63) at org. junit. runners. parentRunner. runChildren (ParentRunner. java: 236) at org. junit. runners. parentRunner. access $000 (ParentRunner. java: 53) at org. junit. runners. parentRunner $2. evaluate (ParentRunner. java: 229) at org. junit. runners. parentRunner. run (ParentRunner. Java: 309) at org. eclipse. jdt. internal. junit4.runner. JUnit4TestReference. run (JUnit4TestReference. java: 50) at org. eclipse. jdt. internal. junit. runner. testExecution. run (TestExecution. java: 38) at org. eclipse. jdt. internal. junit. runner. remoteTestRunner. runTests (RemoteTestRunner. java: 459) at org. eclipse. jdt. internal. junit. runner. remoteTestRunner. runTests (RemoteTestRunner. java: 675) at org. eclipse. jdt. internal. ju Nit. runner. remoteTestRunner. run (RemoteTestRunner. java: 382) at org. eclipse. jdt. internal. junit. runner. remoteTestRunner. main (RemoteTestRunner. java: 192) Caused by: org. apache. ibatis. builder. builderException: Error creating document instance. cause: org. xml. sax. SAXParseException; lineNumber: 26; columnNumber: 66; must be of the element type "mapper" declared attribute "class ". At org. apache. ibatis. parsing. XPathParser. createDocument (XPathParser. java: 238) at org. apache. ibatis. parsing. XPathParser.
(XPathParser. java: 107) at org. apache. ibatis. builder. xml. XMLConfigBuilder.
(XMLConfigBuilder. java: 52) at org. apache. ibatis. session. SqlSessionFactoryBuilder. build (SqlSessionFactoryBuilder. java: 57)... 26 more
I use another registration method. With the package name annotation, all the ER er files under the package can be automatically registered to the configuration file.
<package name="com.liuyunlong.mybatis.annotation"/>
The problem arises again. An exception is thrown as follows:
Org. apache. ibatis. exceptions. persistenceException: ### Error building SqlSession. ### Cause: org. apache. ibatis. builder. builderException: Error creating document instance. cause: org. xml. sax. SAXParseException; lineNumber: 26; columnNumber: 54; the element type "package" must be declared ". At org. apache. ibatis. exceptions. exceptionFactory. wrapException (ExceptionFactory. java: 8) at org. apache. ibatis. session. sqlSessionFactoryBuilder. build (SqlSessionFactoryBuilder. java: 60) at org. apache. ibatis. session. sqlSessionFactoryBuilder. build (SqlSessionFactoryBuilder. java: 44) at com. liuyunlong. mybatis. utils. myBatisUtil. getSession (MyBatisUtil. java: 22) at com. liuyunlong. mybatis. test. lesson3.add (Lesson3.ja Va: 20) 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: 47) at org. junit. internal. runners. model. reflectiveCallable. run (Reflecti VeCallable. java: 12) at org. junit. runners. model. frameworkMethod. invokeExplosively (FrameworkMethod. java: 44) at org. junit. internal. runners. statements. invokeMethod. evaluate (InvokeMethod. java: 17) at org. junit. runners. parentRunner. runLeaf (ParentRunner. java: 271) at org. junit. runners. blockJUnit4ClassRunner. runChild (BlockJUnit4ClassRunner. java: 70) at org. junit. runners. blockJUnit4ClassRunner. runChild (BlockJUnit4 ClassRunner. java: 50) at org. junit. runners. parentRunner $ 3.run( ParentRunner. java: 238) at org. junit. runners. parentRunner $1. schedule (ParentRunner. java: 63) at org. junit. runners. parentRunner. runChildren (ParentRunner. java: 236) at org. junit. runners. parentRunner. access $000 (ParentRunner. java: 53) at org. junit. runners. parentRunner $2. evaluate (ParentRunner. java: 229) at org. junit. runners. parentRunner. run (ParentRunner. Java: 309) at org. eclipse. jdt. internal. junit4.runner. JUnit4TestReference. run (JUnit4TestReference. java: 50) at org. eclipse. jdt. internal. junit. runner. testExecution. run (TestExecution. java: 38) at org. eclipse. jdt. internal. junit. runner. remoteTestRunner. runTests (RemoteTestRunner. java: 459) at org. eclipse. jdt. internal. junit. runner. remoteTestRunner. runTests (RemoteTestRunner. java: 675) at org. eclipse. jdt. internal. ju Nit. runner. remoteTestRunner. run (RemoteTestRunner. java: 382) at org. eclipse. jdt. internal. junit. runner. remoteTestRunner. main (RemoteTestRunner. java: 192) Caused by: org. apache. ibatis. builder. builderException: Error creating document instance. cause: org. xml. sax. SAXParseException; lineNumber: 26; columnNumber: 54; the element type "package" must be declared ". At org. apache. ibatis. parsing. XPathParser. createDocument (XPathParser. java: 238) at org. apache. ibatis. parsing. XPathParser.
(XPathParser. java: 107) at org. apache. ibatis. builder. xml. XMLConfigBuilder.
(XMLConfigBuilder. java: 52) at org. apache. ibatis. session. SqlSessionFactoryBuilder. build (SqlSessionFactoryBuilder. java: 57)... 26 more
One user pointed out that MyBtis was only supported after 3.2. In previous versions, you can create a mapper. xml file and specify namespace as the interface file, as shown below:
<?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.liuyunlong.mybatis.annotation.UserMapper"> </mapper>
Then register the mapper. xml file in the configuration file.
<mappers> <mapper resource="com/liuyunlong/mybatis/map/mapper.xml" /> </mappers>
Solve the problem!
So is it true that Versions later than 3.2 support the above two methods, update the jar file of MyBatis for verification, I have verified version 3.2.4, the above two methods can be used.