Bootstrapping a Spring Boot Project with MyBatis in IntelliJ IDEA
Start by generating a new project using IntelliJ's Spring Initializr wizard. Choose the following dependencies: Spring Web, Thymeleaf, MySQL Driver, JDBC API, and MyBatis Framework. Finish the wizard and open the project in a new window.
Add database and MyBatis configuration in src/main/resources/application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.demo.myapp.model
In pom.xml,16 include the MyBatis Generator plugin inside the <build><plugins> section. The plugin needs a configuration file location:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.2</version>
<configuration>
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
Also ensure the Thymeleaf starter is16 present (already selected in the wizard).
Create the directory generator under src/main/resources and place a file named generatorConfig.xml inside it. Two approaches are common: placing generated mapper XML files directly in the Java source tree, or</2>placing them under resources. Below is a configuration for the second approach—mappers in resources/mapper:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry location="D:\libs\mysql-connector-java-8.0.30.jar"/>
<context id="MySQLTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mydb"
userId="dbuser" password="dbpass"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.demo.myapp.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.demo.myapp.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="user_account" domainObjectName="UserAccount"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
If you prefer to keep mapper XMLs alongside the Java interfaces (approach one), change <sqlMapGenerator targetProject="src/main/java" targetPackage="com.demo.myapp.mapper"/> and16 adjust mybatis.mapper-locations to16 scan the16 Java package.
To run the generator, create a Maven run configuration: Run → Edit Configurations → + → Maven. Set the Command line to:
mybatis-generator:generate
Give the configuration a name, e.g., GenerateEntities, and run it. Check the16 generated model, mapper interface, and XML files.
After generation, ensure that each mapper interface is annotated with @Mapper so Spring can discover it:
package com.demo.myapp.mapper;
import com.demo.myapp.model.UserAccount;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserAccountMapper {
int deleteByPrimaryKey(Long id);
int insert(UserAccount record);
int insertSelective(UserAccount record);
UserAccount selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(UserAccount record);
int updateByPrimaryKey(UserAccount record);
}
Alternatively,16 apply @MapperScan("com.demo.myapp.mapper") on the main application class or a configuration class.