JDBC and Mybatis common bugs

Problem description

Because spring boot usually simplifies the configuration and feels a little handmade, I handwritten JDBC and Mybatis in Mybatis class today. At the same time, there were several problems, which are recorded here.

0x01 JDBC insert Chinese garbled code

The code is as follows

The characters are still normal when the console prints the results

However, the Chinese characters just inserted in the database are garbled.

Thus, it can be judged that the character encoding is different when the character is back-end to the database.

Check the configuration file as follows

After checking, it is found that there is a writing problem with UseUnicode and CharacterEncoding behind the url. The correct writing method should be lowercase, as shown below

jdbc.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false

Problem solving

0x02 Mybatis mapper file not found

pass: Mybatis here does not use Spring integration

Errors are reported as follows:

java.lang.ExceptionInInitializerError

Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com.mapper.StuMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com.mapper.StuMapper.xml
······
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com.mapper.StuMapper.xml
······
Caused by: java.io.IOException: Could not find resource com.mapper.StuMapper.xml
······

This problem indicates mapper XML not found

There are two situations:

  • 1, The interface is different from the xml file path
  • 2, xml has problems

After analysis, the interface is the same as the xml path. The case is excluded

At this point, check mybatis config mapper configuration of XML file

Here's the problem It refers to the division between packages under the source package and packages. What we are looking for here is the static resource package. We should use / division, so the correct writing is as follows:

Problem solving

0x03 JUnit test problem when spring integrates Mybaits

After using Spring to integrate Mybatis, the following problems occur in unit testing

java.lang.ExceptionInInitializerError
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
······
Caused by: java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
	at org.springframework.util.Assert.state(Assert.java:73)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:104)
	... 15 more

Spring junit4classrunner requires JUnit 4.12 or higher It can be seen that the JUnit version is too low, but I configured the JUnit version to meet the requirements. The result is still wrong.

After research, the problem was found.

When I created the project, I used maven's dependency management for convenience. I put all the required dependencies in the parent project, and then the child project inherits the dependencies of the parent project. The structure is as follows:

The problem is that in the project of Spring integration Mybatis, I used maven's scaffold webapp when creating this sub project, so he brought a JUnit dependency to replace the JUnit dependency of the parent project.

It happens that the dependent version of the subproject is lower than the version to be used, so there is a problem.

The comment will be refreshed and the. POM will be updated After XML, run the unit test again.

The query is successful and the problem is solved.

summary

Reviewing the basics again, I can't write code for a while without using Springboot, especially the various configuration files of spring and Mybatis and the constraints of dtd, but it also makes me more proficient in browsing official documents. It's still necessary to review the basics from time to time. You need to know where to match what, and you don't need to memorize by rote. Most configurations are written on the official documents.

He who by reviewing the old can gain knowledge of the new and is fit to be a teacher

——The Analects of Confucius

Keywords: Java MySQL JDBC Mybatis Spring

Added by devstudio on Mon, 28 Feb 2022 17:40:00 +0200