Gradle+mybatis-generator+springboot+postgresql生成entity、mapper、dao

本文介绍了如何在Spring Boot项目中结合Gradle和mybatis-generator插件,配置生成entity、DAO和Mapper文件,重点强调了PostgreSQL的配置区别,以及配置文件中`<configuration>`标签的重要性。项目结构和生成文件的目录需预先设定,执行特定Gradle任务即可完成代码自动生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、版本信息

  • springboot:2.6.15
  • gradle:6.9.4
  • idea:2024.1.1
  • mybatis-generator:使用插件gradle.plugin.com.arenagod.gradle:mybatis-- generator-plugin:1.4
  • os:macos
  • DB:postgresql

网上很多资料都是MySQL的配置方式,我配置DB是postgresql,可苦了我了…

二、配置文件

build.gradle

buildscript {
    ext {
        springBootVersion = '2.6.15'
    }
    repositories {
        mavenCentral()
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
        maven {
            url 'https://maven.aliyun.com/repository/spring/'
        }
        //添加maven仓库
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        // mybatis-generator 插件路径
        classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4"
    }
}

apply plugin: 'java'
//apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
//引入 mybatis-generator 插件
apply plugin: "com.arenagod.gradle.MybatisGenerator"

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    maven {
        url 'https://maven.aliyun.com/repository/public/'
    }
    maven {
        url 'https://maven.aliyun.com/repository/spring/'
    }
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    //数据源
    compile 'com.alibaba:druid-spring-boot-starter:1.1.2'
    implementation 'org.postgresql:postgresql:42.7.3'

    //配置mybatis
    compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1"
    //mybatis-generator core 包
    compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version:'1.3.2'
}

configurations {
    mybatisGenerator
}
// mybatis-generator.xml 配置路径
//这里会遇到个问题:MyBatis Generator 通过xml生成,有日志但是没有生成文件成功的问题,
//原因:mac下是找不到 ./src 路径的,需要全路径,如下配置。windows则为src/main/resources/generator.xml
mybatisGenerator {
    verbose = true
    //我这里是macOS,所以文件的路径一定要用全路径
    configFile = '/Users/xiongzelin/项目/auth/src/main/resources/mybatis/mybatis-generator.xml'
}

mybatis-generator的代码生成配置文件

<?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>
    <!-- generatorConfig.xml配置文件,放在resource目录下即可 -->
    <classPathEntry location="/Users/xiongzelin/项目/auth/src/main/resources/mybatis/postgresql-42.3.1.jar"/>
    <!--数据库驱动个人配置-->
    <context id="MysqlTables" targetRuntime="MyBatis3">
        <property name="autoDelimitKeywords" value="<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘随身带

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值