Mybatis官方生成器是一个强大的工具,主要用于自动生成Mapper接口、XML映射文件和实体类等代码,以减少手动编写这些代码的工作量。通过配置XML文件,可以灵活地生成符合项目需求的代码,并支持多种数据库类型。本文将详细介绍Mybatis官方生成器的学习过程,包括安装与配置、生成代码的基本步骤以及常见问题的解决方法,帮助读者快速掌握Mybatis官方生成器的使用技巧。Mybatis官方生成器学习涵盖了从安装配置到生成代码的全过程。
Mybatis基本概念介绍 Mybatis是什么Mybatis是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。Mybatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。Mybatis可以使用简单的XML或注解进行配置和原始映射,将接口和Java的POJOs(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
Mybatis的主要特点- 动态SQL支持:Mybatis提供了强大的动态SQL构建能力,可以灵活地根据条件生成不同的SQL语句。
 - 基于接口的编程:Mybatis通过接口及其输入参数类型进行查询、更新等操作。
 - 支持自定义SQL:允许在XML配置文件中编写自定义SQL,也可以使用注解来配置。
 - 使用灵活:Mybatis提供了各种配置方式,可以根据项目的需要进行配置,提供了丰富的API接口。
 - 与JDBC相比:Mybatis的SQL语句需要手动编写,但是提供了强大的动态SQL和配置灵活性,更适合复杂应用。
 - 插件扩展性:Mybatis提供了插件机制,可以通过编写插件来扩展其功能。
 - 缓存支持:Mybatis内置了一级缓存(SqlSession级别的缓存)和二级缓存(mapper级别的缓存),可以提高查询性能。
 
示例代码
动态SQL支持:
<select id="getUserByNameAndId" resultType="User">
    SELECT * FROM users WHERE name = #{name} AND id = #{id}
</select>
 Mybatis的工作原理
工作流程
- 配置解析:Mybatis通过读取XML配置文件来解析配置信息,包括数据库连接信息,映射文件的位置等。
 - 获取SqlSession对象:SqlSession是Mybatis的执行器,用来发送执行SQL语句的命令并返回结果。
 - 执行SQL:通过SqlSession获取Mapper对象,通过Mapper对象调用映射文件中定义的方法,完成与数据库的交互。
 - 返回结果:将数据库返回的结果转换成Java对象,封装成集合返回。
 
配置详解
- Mybatis配置文件:
mybatis-config.xml,包括数据库连接、环境配置、映射文件路径等配置信息。 - 映射文件:
*.Mapper.xml,包含SQL语句、映射规则等信息。 - Java对象:POJO对象,与数据库表结构相对应。
 - SqlSession:Mybatis执行器,可以用来执行SQL语句,返回结果等。
 - Executor:执行器,内部封装了JDBC对数据库的增删改查操作。
 - Mapped Statement:映射语句的封装,包括了传入参数类型、传出结果类型等。
 - ParameterHandler:参数处理器,负责处理传入的参数,将其封装成参数对象。
 - ResultSetHandler:结果集处理器,负责处理数据库返回的结果集,将其转换成List。
 - Statement:JDBC的Statement接口。
 
示例代码
<!-- mybatis-config.xml -->
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/mybatis/UserMapper.xml"/>
    </mappers>
</configuration>
<!-- UserMapper.xml -->
<mapper namespace="com.mybatis.mapper.UserMapper">
    <select id="getUserById" resultType="User">
        SELECT * FROM users WHERE id = #{id}
    </select>
</mapper>
// User实体类
public class User {
    private int id;
    private String name;
    private String email;
    // getter和setter方法
}
// UserMapper接口
public interface UserMapper {
    User getUserById(int id);
}
// 测试代码
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
SqlSession session = factory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user.getName());
session.close();
 Mybatis官方生成器简介
 官方生成器的作用
Mybatis官方生成器(Mybatis Generator)是一个开源工具,主要用于生成Mybatis所需的Mapper接口、XML映射文件、实体类等。通过配置XML文件,可以自动生成符合项目需求的代码,避免手动编写这些代码的繁琐过程。
官方生成器的优势- 自动生成代码:减少手动编写Mapper接口、XML映射文件等代码的工作量。
 - 支持多种数据库:支持多种数据库类型,如MySQL、Oracle、SQL Server等。
 - 灵活的配置选项:可以自定义生成代码的格式、命名规则等,满足项目需求。
 - 减少维护成本:当数据库表结构发生变化时,可以重新生成代码,保持代码与数据库的一致性。
 - 提升开发效率:减少重复劳动,集中精力在业务逻辑上。
 
示例代码
自动生成Mapper接口:
public interface UserMapper {
    User selectUserById(int id);
    int insertUser(User user);
    int updateUser(User user);
    int deleteUser(int id);
}
 官方生成器的安装与配置
安装步骤
- 下载Mybatis Generator插件:可以从官网下载Mybatis Generator的jar包。
 - 配置文件:编写配置文件,定义数据库连接信息和生成代码的配置。
 - 执行生成器:通过命令行或IDE插件运行Mybatis Generator,生成所需代码。
 
配置文件示例
<!-- generatorConfig.xml -->
<generatorConfiguration>
    <context id="TestTables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                       connectionURL="jdbc:mysql://localhost:3306/mybatis"
                       userId="root"
                       password="root">
        </jdbcConnection>
        <javaModelGenerator targetPackage="com.mybatis.model" targetProject="src/main/java"/>
        <sqlMapGenerator targetPackage="com.mybatis.mapper" targetProject="src/main/resources"/>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.mapper" targetProject="src/main/java"/>
        <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>
执行生成器
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
 使用Mybatis官方生成器生成代码
 生成代码的基本步骤
- 编写数据库连接信息:在配置文件中定义数据库连接信息。
 - 配置生成器参数:定义生成代码的格式、命名规则等。
 - 指定生成代码的目标位置:定义生成的代码保存位置。
 - 指定需要生成的表名:定义需要生成代码的数据库表名。
 - 执行生成器:运行Mybatis Generator生成代码。
 
示例代码
生成器配置文件:
<!-- generatorConfig.xml -->
<generatorConfiguration>
    <context id="TestTables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                       connectionURL="jdbc:mysql://localhost:3306/mybatis"
                       userId="root"
                       password="root">
        </jdbcConnection>
        <javaModelGenerator targetPackage="com.mybatis.model" targetProject="src/main/java"/>
        <sqlMapGenerator targetPackage="com.mybatis.mapper" targetProject="src/main/resources"/>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.mapper" targetProject="src/main/java"/>
        <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>
执行生成器:
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
 配置生成器的XML文件
配置文件详解
<generatorConfiguration>:根标签,包含其他配置。<context>:生成器上下文,定义了生成代码的环境。<jdbcConnection>:数据库连接参数,定义了连接数据库所需的驱动类、连接字符串等。<javaModelGenerator>:生成Java模型类。<sqlMapGenerator>:生成XML映射文件。<javaClientGenerator>:生成Mapper接口。<table>:定义需要生成代码的数据库表。
示例代码
<!-- generatorConfig.xml -->
<generatorConfiguration>
    <context id="TestTables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                       connectionURL="jdbc:mysql://localhost:3306/mybatis"
                       userId="root"
                       password="root">
        </jdbcConnection>
        <javaModelGenerator targetPackage="com.mybatis.model" targetProject="src/main/java"/>
        <sqlMapGenerator targetPackage="com.mybatis.mapper" targetProject="src/main/resources"/>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.mapper" targetProject="src/main/java"/>
        <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>
 生成Mapper接口和XML文件
生成Mapper接口
假设数据库表users的结构如下:
| 列名 | 类型 | 
|---|---|
| id | int | 
| name | string | 
| string | 
生成的Mapper接口如下:
public interface UserMapper {
    User selectUserById(int id);
    int insertUser(User user);
    int updateUser(User user);
    int deleteUser(int id);
}
生成XML映射文件
生成的XML映射文件内容如下:
<mapper namespace="com.mybatis.mapper.UserMapper">
    <select id="selectUserById" resultType="User">
        SELECT * FROM users WHERE id = #{id}
    </select>
    <insert id="insertUser" parameterType="User">
        INSERT INTO users (name, email) VALUES (#{name}, #{email})
    </insert>
    <update id="updateUser" parameterType="User">
        UPDATE users SET name = #{name}, email = #{email} WHERE id = #{id}
    </update>
    <delete id="deleteUser" parameterType="int">
        DELETE FROM users WHERE id = #{id}
    </delete>
</mapper>
 生成实体类和DAO接口
生成实体类
生成的实体类如下:
public class User {
    private int id;
    private String name;
    private String email;
    // getter和setter方法
}
生成DAO接口
生成的DAO接口如下:
public interface UserMapper {
    User selectUserById(int id);
    int insertUser(User user);
    int updateUser(User user);
    int deleteUser(int id);
}
 Mybatis官方生成器的自定义配置
 修改生成器模板
可以通过修改生成器模板来自定义生成的代码。
示例配置文件
<!-- generatorConfig.xml -->
<generatorConfiguration>
    <context id="TestTables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                       connectionURL="jdbc:mysql://localhost:3306/mybatis"
                       userId="root"
                       password="root">
        </jdbcConnection>
        <javaModelGenerator targetPackage="com.mybatis.model" targetProject="src/main/java">
            <property name="enableInterface" value="true"/>
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="com.mybatis.mapper" targetProject="src/main/resources"/>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.mapper" targetProject="src/main/java"/>
        <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
    </context>
</generatorConfiguration>
示例模板文件
<!-- User.java -->
public class User {
    private int id;
    private String name;
    private String email;
    // getter和setter方法
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}
 设置不同的生成规则
可以通过配置不同的生成规则来自定义生成的代码。
示例配置文件
<!-- generatorConfig.xml -->
<generatorConfiguration>
    <context id="TestTables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                       connectionURL="jdbc:mysql://localhost:3306/mybatis"
                       userId="root"
                       password="root">
        </jdbcConnection>
        <javaModelGenerator targetPackage="com.mybatis.model" targetProject="src/main/java">
            <property name="enableInterface" value="true"/>
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="com.mybatis.mapper" targetProject="src/main/resources"/>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.mapper" targetProject="src/main/java"/>
        <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
    </context>
</generatorConfiguration>
示例模板文件
<!-- User.java -->
public class User {
    private int id;
    private String name;
    private String email;
    // getter和setter方法
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}
 自定义实体类属性
可以通过配置自定义实体类属性来自定义生成的代码。
示例配置文件
<!-- generatorConfig.xml -->
<generatorConfiguration>
    <context id="TestTables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                       connectionURL="jdbc:mysql://localhost:3306/mybatis"
                       userId="root"
                       password="root">
        </jdbcConnection>
        <javaModelGenerator targetPackage="com.mybatis.model" targetProject="src/main/java">
            <property name="enableInterface" value="true"/>
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="com.mybatis.mapper" targetProject="src/main/resources"/>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.mapper" targetProject="src/main/java"/>
        <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <columnOverride column="id" javaType="int" jdbcType="INTEGER" />
            <columnOverride column="name" javaType="String" jdbcType="VARCHAR" />
            <columnOverride column="email" javaType="String" jdbcType="VARCHAR" />
        </table>
    </context>
</generatorConfiguration>
示例模板文件
<!-- User.java -->
public class User {
    private int id;
    private String name;
    private String email;
    // getter和setter方法
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}
 使用生成的代码进行数据库操作
 连接数据库
示例代码
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
SqlSession session = factory.openSession();
配置文件示例
<!-- mybatis-config.xml -->
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/mybatis/UserMapper.xml"/>
    </mappers>
</configuration>
 执行增删改查操作
示例代码
// 查询操作
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user.getName());
// 插入操作
User newUser = new User();
newUser.setName("John Doe");
newUser.setEmail("john.doe@example.com");
mapper.insertUser(newUser);
// 更新操作
User updateUser = new User();
updateUser.setId(1);
updateUser.setName("Jane Doe");
updateUser.setEmail("jane.doe@example.com");
mapper.updateUser(updateUser);
// 删除操作
mapper.deleteUser(1);
 处理异常和事务管理
示例代码
try {
    UserMapper mapper = session.getMapper(UserMapper.class);
    User user = mapper.getUserById(1);
    System.out.println(user.getName());
    User newUser = new User();
    newUser.setName("John Doe");
    newUser.setEmail("john.doe@example.com");
    mapper.insertUser(newUser);
    User updateUser = new User();
    updateUser.setId(1);
    updateUser.setName("Jane Doe");
    updateUser.setEmail("jane.doe@example.com");
    mapper.updateUser(updateUser);
    mapper.deleteUser(1);
    session.commit();
} catch (Exception e) {
    session.rollback();
    e.printStackTrace();
} finally {
    session.close();
}
 Mybatis官方生成器的常见问题及解决方法
 常见错误及解决办法
错误1:找不到数据库表
- 原因:配置文件中的表名错误或数据库中不存在该表。
 - 解决方法:检查配置文件中的表名是否正确,确认数据库中是否存在该表。
 - 示例代码
<!-- generatorConfig.xml --> <generatorConfiguration> <context id="TestTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="root"> </jdbcConnection> <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration> 
错误2:生成的代码不符合预期
- 原因:生成器配置文件中的参数设置错误。
 - 解决方法:检查配置文件中的参数设置,确保它们符合预期。
 - 示例代码
<!-- generatorConfig.xml --> <generatorConfiguration> <context id="TestTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="root"> </jdbcConnection> <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration> 
错误3:生成的代码存在编译错误
- 原因:生成的代码与项目中的其他代码存在冲突。
 - 解决方法:确保生成的代码与项目中的其他代码兼容,可以修改生成器配置文件中的包名或类名。
 - 示例代码
<!-- generatorConfig.xml --> <generatorConfiguration> <context id="TestTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="root"> </jdbcConnection> <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration> 
问题1:如何生成多个表的代码?
- 解答:在配置文件中添加多个
<table>标签,每个标签对应一个数据库表。 - 示例代码
<!-- generatorConfig.xml --> <generatorConfiguration> <context id="TestTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="root"> </jdbcConnection> <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> <table tableName="orders" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration> 
问题2:如何生成特定的数据库表代码?
- 解答:在配置文件中的
<table>标签中指定需要生成的数据库表名。 - 示例代码
<!-- generatorConfig.xml --> <generatorConfiguration> <context id="TestTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="root"> </jdbcConnection> <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration> 
问题3:如何生成特定的Java类?
- 解答:在配置文件中的
<table>标签中指定domainObjectName参数,定义生成的Java类名。 - 示例代码
<!-- generatorConfig.xml --> <generatorConfiguration> <context id="TestTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="root"> </jdbcConnection> <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration> 
问题4:如何生成特定的Mapper接口?
- 解答:在配置文件中的
<table>标签中指定domainObjectName参数,定义生成的Mapper接口名。 - 示例代码
<!-- generatorConfig.xml --> <generatorConfiguration> <context id="TestTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="root"> </jdbcConnection> <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration> 
与Spring集成
- 步骤1:在Spring配置文件中配置SqlSessionFactory。
 - 步骤2:使用
@Autowired注解注入SqlSessionFactory。 - 步骤3:通过SqlSessionFactory获取SqlSession,执行数据库操作。
 
与Spring Boot集成
- 步骤1:在Spring Boot应用中引入Mybatis的依赖。
 - 步骤2:配置Mybatis的SqlSessionFactory。
 - 步骤3:使用
@MapperScan注解扫描Mapper接口。 - 步骤4:通过
@Autowired注解注入Mapper接口,执行数据库操作。 
示例代码
Spring配置类示例
@Configuration
public class MyBatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        return factoryBean;
    }
    @Bean
    public DataSource dataSource() {
        return new DriverManagerDataSource("jdbc:mysql://localhost:3306/mybatis", "root", "root");
    }
}
Service类示例
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public void addUser(User user) {
        userMapper.insertUser(user);
    }
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }
    public void deleteUser(int id) {
        userMapper.deleteUser(id);
    }
    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }
}
Controller类示例
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/")
    public void addUser(@RequestBody User user) {
        userService.addUser(user);
    }
    @PutMapping("/")
    public void updateUser(@RequestBody User user) {
        userService.updateUser(user);
    }
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable int id) {
        userService.deleteUser(id);
    }
    @GetMapping("/{id}")
    public User getUserById(@PathVariable int id) {
        return userService.getUserById(id);
    }
}
通过以上配置和代码,可以方便地将Mybatis Generator生成的代码与Spring或Spring Boot集成,实现数据库操作。