我是靠谱客的博主 清脆机器猫,这篇文章主要介绍MyBatis-generator+SpringBoot+Gradle配置参考Gradle配置配置设置生成代码的配置文件,现在分享给大家,希望可以做个参考。

参考

在 Gradle 中使用 MyBatis Generator

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

Gradle配置

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

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 配置

复制代码
1
2
3
4
5
6
7
8
9
10
11
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 中配置数据库表信息

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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:这点很重要啊,数据库的设计和后端的开发往往不是一个人,又不能保证两个地方的命名格式到位…

最后

以上就是清脆机器猫最近收集整理的关于MyBatis-generator+SpringBoot+Gradle配置参考Gradle配置配置设置生成代码的配置文件的全部内容,更多相关MyBatis-generator+SpringBoot+Gradle配置参考Gradle配置配置设置生成代码内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部