Getting Started with MyBatis
Overcoming Pain Points
MyBatis, a semi-automatic mapping framework, addresses the challenges faced by large-scale projects that require high performance and simple maintenance. Unlike Hibernate or Spring, which are more suitable for small-scale projects, MyBatis provides a more robust solution for complex applications. In this article, we will explore the key components of MyBatis and provide a step-by-step guide on how to get started.
Key Components of MyBatis
MyBatis consists of four core components:
- SqlSessionFactoryBuilder: This component generates the SqlSessionFactory based on configuration information.
- SqlSessionFactory: This component relies on the SqlSessionFactoryBuilder to generate the SqlSession session.
- SqlSession: This component is responsible for either sending an SQL execution or getting a Mapper interface.
- Mapper: This component consists of a Java interface and XML (or annotations) and is responsible for sending SQL to perform and returning the result.
Using MyBatis with Spring Boot
To integrate MyBatis with the popular Spring Boot framework, add the following Maven configuration:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
Configuring MyBatis
Add the following configuration to the application.properties file:
# db
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user
spring.datasource.characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# mybatis
mybatis.mapper-locations=classpath:mappers/*xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.auto-mapping-unknown-column-behavior=NONE
Pain Points and Solutions
MyBatis query is very clear and effective, but the availability of generic components for update, insert, delete, and selectByPK is limited. This can be addressed primarily by using the mybatis-generator. Check the corresponding configuration, which is similar to the following:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<configuration>
<generatorConfiguration>
<properties>
<property name="databaseType" value="mysql"/>
</properties>
<javaClientGenerator targetProject="." type="ANNOTATION"/>
<tableCatalogs>
<catalog name="mybatis" tableNamePattern=".*" domainObjectNamePattern=".*" enableCountByExample="true" enableDeleteByExample="true" enableInsertByExample="true" enableSelectByExample="true" enableUpdateByExample="true" />
</tableCatalogs>
</generatorConfiguration>
</configuration>
<executions>
<execution>
<id>generator</id>
<goals>
<goal>generator</goal>
</goals>
</execution>
</executions>
</plugin>
Integrating MyBatis with Spring Boot
To integrate MyBatis with Spring Boot, add the following configuration to the application.yml file:
mybatis:
mapper-locations: classpath*:com/bjork/ws/dao/mapper/*xml
configuration:
map-underscore-to-camel-case: true
auto-mapping-unknown-column-behavior: NONE
Troubleshooting
When using MyBatis, you may encounter the following error:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
This error is caused by a mismatch between the general definition of the Mapper interface and the XML files. To resolve this issue, follow these steps:
- Check the package name of the XML file and ensure it corresponds to the package name of the Mapper interface.
- Check the namespace of the XML file and ensure it corresponds to the package name of the Mapper interface.
- Check the function name of the XML file and ensure it corresponds to the function name of the Mapper interface.
- Remove any Chinese notes from the XML file.
- Free up space in the XML file or remove any empty lines.
By following these steps, you should be able to resolve the issue and get started with MyBatis.