Step-by-Step Guide to Configuring MyBatis Generator and Generating Code in a Spring Boot Project
This article provides a detailed tutorial on setting up MyBatis Generator in a Spring Boot application, covering configuration file placement, XML adjustments, Maven dependencies, execution steps, and the resulting model, mapper, and XML files with full code examples.
1. Copy the MyBatis Generator configuration file (GeneratorMapper.xml) to the root directory of your project.
2. Modify
<generatorConfiguration>...</generatorConfiguration>according to your project and database tables, updating the driver class to com.mysql.cj.jdbc.Driver for newer MySQL versions and adding nullCatalogMeansCurrent=true to the JDBC URL to avoid generation issues.
Note: The current MySQL version in the example is 5.7; adjust settings based on your environment.
3. Add the MySQL reverse‑engineering dependency to pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>4. Run the Maven goal to generate the files (e.g., mvn mybatis-generator:generate), then double‑click the generated files to view them.
5. The generation creates the following artifacts: Student.java – the model class with fields id, name, and age. StudentMapper.java – the mapper interface with CRUD methods. StudentMapper.xml – the MyBatis XML mapping file defining result maps and SQL statements.
Example of the generated model class:
package com.md.springboot.model;
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
}Example of the generated mapper interface:
package com.md.springboot.mapper;
import com.md.springboot.model.Student;
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}Example of the generated StudentMapper.xml mapping file (truncated for brevity):
<?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.md.springboot.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.md.springboot.model.Student">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="age" jdbcType="INTEGER" property="age"/>
</resultMap>
...
</mapper>These generated files simplify database operations in the Spring Boot project, allowing you to focus on business logic.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
