Hello everyone, after learning the Spring Framework, I also got a book called “Playing with MyBatis: In-depth Analysis and Customization”. This article is based on the problems encountered in running configuration in this small volume. Therefore, if you feel a little confused when watching, there is probably no scene!

PS: This is not a brochure, but the questions can be referred to.

background

The author constructed the project following the booklet “Basic Review – Overview and Environment Building”, followed the steps of the author to the last step. Pom.xml, log4j.properties, mapper.xml are configured and depart.java is created as the class to package the records queried from MySQL.

“Then hit” Run “and, sadly, did not succeed one time along the way. But let’s take a look at what stands in the way of remaining successful.

The following error message is displayed:

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in mapper/department.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource mapper/department.xml
	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 app.MyBatisApplication1.main(MyBatisApplication1.java:15)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource mapper/department.xml
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:122)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)...2 more
Caused by: java.io.IOException: Could not find resource mapper/department.xml
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:374)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120)...4 more

Process finished with exit code 1
Copy the code

The error message is long, but take a look. What is the problem?

The solution

Read the following error message carefully, there is a familiar shadow:

Cause: java.io.IOException:

Could not find resource mapper/department.xml

See this, can not help but touch his head, ah wrong ah, I are written in accordance with the author, zha say can not find it? No way, since said can not find, that I will artificially look for, to see if there is really no this file. Well, no!

People are stupid, is it I read wrong? As it turns out, the author made a mistake and named the file mapper.xml. Instead of “I”, I didn’t realize it and just followed it.

Now that you see the problem, correct it by pressing Shift +F6 and rename it todepartment.xmlAfter, run it again.

It turned out to be an error. So what’s the problem this time?

Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.linkedbear.mybatis.entity.Department'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.linkedbear.mybatis.entity.Department
Copy the code

Error message is too long, cut out the problem to show you.

Report wrong information to express, did not find the kind that wants to pack, you how fat matter, how old forget three fall four! In fact, the solution is very simple, because the author’s package is different from the one created by himself in many cases, so when running, the code running on the author’s computer is OK, which is error to you, and there is a high probability that the package is different!

According to the error information, locate the package as follows. Remove the package name, that is, change it to entity.department.

Click Run again, and you’ll see the result. (of course, the output is not the same as the small book, rewrite the toString function).

conclusion

In fact, the whole process of investigation is still very simple, that is, to correct the files or classes that cannot be found, but THE author still wrote this article. For beginners, when they come into contact with a new field (the author has used MyBatis before, but the code is generated by MyBatis Generator plug-in), they will inevitably panic when they encounter difficulties, copy and paste the error information on the Internet to find answers without looking at it, or even give up learning directly.

But now just under review the whole process, in fact, learning is the problem, read the error message, and then locate information, and try to solve the problem becomes longer, the author is to break their own timidity, face the problem to grow, and also hope can through this article will give beginners like the confidence to continue to keep it up!

Finally, can you share your experience or experience in the comments section?

This article is part of the “Gold Nuggets For Free!” Event, click to view details of the event