MyBatis-generator+SpringBoot+Gradle配置

参考

在 Gradle 中使用 MyBatis Generator

Springboot+gradle+Mybatis-Generator 代码自动生成器

Gradle配置

直接从现在的项目修改贴下来的,里面有不少的非必须项…

可以看看上面的参考里的文章。

buildscript {
   ext {
      springBootVersion = '2.0.0.RELEASE'
   }
   repositories {
      mavenCentral()
      //添加maven仓库 mybatis-generetor
      maven {
         url "https://plugins.gradle.org/m2/"
      }
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

      // mybatis-generator 插件路径mybatis-generetor
      classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4"
   }
}
//配置从阿里云源下载依赖
allprojects {
   repositories {
      maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
      maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
   }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
//引入 mybatis-generator 插件mybatis-generetor
apply plugin: "com.arenagod.gradle.MybatisGenerator"

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

repositories {
   mavenCentral()
}

configurations {
   //这里需要使用 MyBatis Generator,MySQL 驱动,以及 MyBatis Mapper.
   //由于代码生成单独运行即可,不需要参与到整个项目的编译,因此在 build.gradle 中添加配置:
   mybatisGenerator
}

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

   // mybatis
   compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1')

   // https://mvnrepository.com/artifact/mysql/mysql-connector-java
   compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.45'

   // https://mvnrepository.com/artifact/com.alibaba/fastjson
   compile group: 'com.alibaba', name: 'fastjson', version: '1.2.47'

   //mybatis-geerator
   // https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core
   compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version: '1.3.6'

}

// mybatis-generator.xml 配置路径
mybatisGenerator {
   verbose = true
   configFile = 'src/main/resources/mybatis/generator.xml'
}

配置

在项目配置文件中配置基本信息

我用的 yml 配置

spring:
  datasource:
    url: jdbc:mysql://118.126.xxx.xxx:3306/test?serverTimezone=UTC
    username: xxxx
    password: xxxxx
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  #实体类所做包
  type-aliases-package: learning.model2
  #mapper.xml所在位置
  mapper-locations: classpath:mappers/*.xml

设置生成代码的配置文件

在 generatorConfig.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://118.xxx.xxx.xxx:3306/test"
                        userId="root"
                        password="xxxxxx">
        </jdbcConnection>

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

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

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

        <!-- 配置表信息 -->
        <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
            是否生成 example类 -->

        <table schema="test" tableName="place_salary"
               domainObjectName="PlaceSalary" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false">
            <!-- 生成的bean的属性不采用驼峰-->
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>
</generatorConfiguration>

更详细的配置情况还得更深入的学习。

基本上这样只是可以拿来用…

对”为什么“完全不了解,里面有多少东西是没必要的也不知道…

有一点需要拿出来讲一讲。

生成的bean类的属性不采用驼峰式命名

自动生成的 bean 对象的属性默认是驼峰式的,这样当然很好,不过 Spring 在自动装配对象需要数据库里的字段,和 bean 类的属性一致,否则是装配不了的。

上面添加的注释下的属性就是使之在自动生成 bean 时,属性值和数据库字段一致。

PS:这点很重要啊,数据库的设计和后端的开发往往不是一个人,又不能保证两个地方的命名格式到位…

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值