千家信息网

SpringBoot集成MyMatis-Generator的使用方法

发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,这篇文章主要讲解了"SpringBoot集成MyMatis-Generator的使用方法",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"SpringBo
千家信息网最后更新 2025年12月03日SpringBoot集成MyMatis-Generator的使用方法

这篇文章主要讲解了"SpringBoot集成MyMatis-Generator的使用方法",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"SpringBoot集成MyMatis-Generator的使用方法"吧!

SpringBoot集成MyMatis-Generator

1.使用Spring Initializr创建SpringBoot项目

POM依赖

                        org.springframework.boot            spring-boot-starter-web                            org.mybatis.spring.boot            mybatis-spring-boot-starter            2.1.3                            mysql            mysql-connector-java            runtime            5.1.40                            org.projectlombok            lombok            true                            org.springframework.boot            spring-boot-starter-test            test                                                org.junit.vintage                    junit-vintage-engine                                        

2. 配置application.yml

根据实际数据源配置

spring:  datasource:    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://localhost:3306/spring_mybatis_dljd?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&useSSL=false    username: root    password: 123456

3. POM中增加插件坐标

                                    org.springframework.boot                spring-boot-maven-plugin                                        org.mybatis.generator                mybatis-generator-maven-plugin                1.4.0                                                            mysql                        mysql-connector-java                        5.1.40                                                                                                            ${project.basedir}/src/main/resources/generatorConfig.xml                    true                    true                                                                                src/main/java                                    **/*.xml                                                        src/main/resources                                    **/*.yml                    **/*.properties                                        

注意:

1. 插件中使用的 mysql-connector-java 与application.yml 中保持一致

2.configurationFile 中指定了生成器配置文件 generatorConfig.xml的位置

3.发布时为了将mapper包中sql映射文件xml 导入资源文件中,加入resources。因此导致重新了默认的资源配置,需要额外导入*.yml,*.properties

4. 创建生成器配置文件

路径:

src\main\resources\generatorConfig.xml

内容:

                                                                                                                                                                                                                            

1. javaModelGenerator 实体和Example文件指定位置

2. sqlMapGenerator SQL映射文件*.Mapper.xml 所在位置

3. javaClientGenerator 接口文件所在位置

4. tableName="%" 为生成所有表

    jdbcConnection driverClass :需与application.yml中保持一致

    5. 测试生成结果

    5.1 点击

    5.2 . 生成

    6. 在启动类中配置扫描接口与映射配置文件

    @SpringBootApplication@MapperScan("com.zhl.springmybatis.mapper")public class SpringMybatisApplication {public static void main(String[] args) {        SpringApplication.run(SpringMybatisApplication.class, args);    }}

    7. 对生成的实体增加toString()方便测试,生成的Mapper接口 Example不用动

    @Data@ToStringpublic class Student {....}

    9.编写Service及ServiceImpl

    StudentService:

    package com.zhl.springmybatis.service;import com.zhl.springmybatis.pojo.Student;import java.util.List;public interface StudentService {    List getList();}

    StudentServiceImpl

    @Servicepublic class StudentServiceImpl implements StudentService {    @Resource    private StudentMapper studentMapper;    @Override    public List getList() {        StudentExample studentExample=new StudentExample();        List list= studentMapper.selectByExample(studentExample);        for (Student s:list) {            System.out.println(s);        }        return list;    }}

    10. 测试类

    这里引用 Mapper接口 使用 @Resource 避免出现警告红线。

    @SpringBootTestclass SpringMybatisApplicationTests {    @Resource    private StudentMapper studentMapper;    @Test    void contextLoads() {    }    @Test    public void GetList(){        StudentExample studentExample=new StudentExample();        List list= studentMapper.selectByExample(studentExample);        for (Student s:list) {            System.out.println(s);        }    }}

    11. 测试结果

    12. 解决dtd文件红标问题

    12.1.根据约束文件地址下载文件到本地

    mybatis-3-mapper.dtd

    mybatis-generator-config_1_0.dtd

    12.2. IDEA中设置

    路径 File | Settings | Languages & Frameworks | Schemas and DTDs

    URI 为 网络地址

    Location 为 本地文件地址

    13. 编译时问题 程序包org.apache.ibatis.annotations不存在

    pom中引入

            org.apache.ibatis        ibatis-core        3.0

    感谢各位的阅读,以上就是"SpringBoot集成MyMatis-Generator的使用方法"的内容了,经过本文的学习后,相信大家对SpringBoot集成MyMatis-Generator的使用方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

    0