Springboot+mybatis+gradle+MyBatis-generator

汪庆
2023-12-01

参考文章Springboot+gradle+Mybatis-Generator 代码自动生成器

gradle配置

buildscript {
    ext {
        springBootVersion = '1.5.10.RELEASE'
    }
    repositories {
        mavenCentral()
        //添加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 {
    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'
    compile 'mysql:mysql-connector-java:6.0.6'
    //配置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
    configFile = '/Users/murasakiseifu/mybatisGenerator/src/main/resources/generator.xml'
}

generator.xml配置

<?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>
    <context id="my" targetRuntime="MyBatis3">

        <!--自动实现Serializable接口-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>

        <!-- 去除自动生成的注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--数据库基本信息-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/takin_write"
                        userId="murasakiseifu"
                        password="123456">
        </jdbcConnection>

        <!--生成实体类的位置以及包的名字-->
        <!--同样Mac用户:targetProject 为全路径-->
        <javaModelGenerator targetPackage="com.example.entity" targetProject="/Users/murasakiseifu/mybatisGenerator/src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--生成映射文件存放位置-->
        <!--同样Mac用户:targetProject 为全路径-->
        <sqlMapGenerator targetPackage="mapper" targetProject="/Users/murasakiseifu/mybatisGenerator/src/main/resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!--生成Dao类存放位置,mapper接口生成的位置-->
        <!--同样Mac用户:targetProject 为全路径-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="/Users/murasakiseifu/mybatisGenerator/src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!-- 配置表信息 -->
        <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
            是否生成 example类 -->
        <table schema="takin_write" tableName="c_student"
               domainObjectName="Student" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false">
        </table>

    </context>
</generatorConfiguration>

application.properties配置

spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.15.216:1520/helowin11
spring.datasource.username=mon
spring.datasource.password=1234
spring.jpa.database=ORACLE
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

#配置文件所在地址
mybatis.config-location  = classpath:mybatis/config/mybatis-config.xml

#mxl所在地址
mybatis.mapper-locations = classpath:mybatis/*Mapper.xml

#实体类所做包
mybatis.type-aliases-package = com.demo.entity

  

server.port=8086
spring.aop.auto=true
spring.aop.proxy-target-class=true

spring.thymeleaf.cache=false

#spring.profiles.active=default
spring.jersey.type=filter

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 类似资料: