# 开始使用

提示:如果您是diboot新用户,需要体验diboot及devtools,建议您 从playground项目开始,避免手动配置的麻烦。

# 1. 创建新的Spring Boot web项目

推荐使用 Spring Initializer (opens new window),快速初始化一个 Spring Boot 工程。 如果您使用IntelliJ IDEA,可以直接通过菜单: File -> New -> Project -> Spring Initializr 创建。

如果需要生成前端,需根据相关版本下载对应的前端项目: diboot-element-admin (opens new window)diboot-antd-admin (opens new window)

# 2. 引入依赖

  • Maven项目引入依赖
<dependency>
    <groupId>com.diboot</groupId>
    <artifactId>diboot-core-spring-boot-starter</artifactId>
    <version>{latestVersion}</version>
</dependency>
<dependency>
    <groupId>com.diboot</groupId>
    <artifactId>diboot-devtools-spring-boot-starter</artifactId>
    <version>{latestVersion}</version>
    <scope>provided</scope><!-- 打war包时剔除,注: 某些IDEA版本存在provided的支持问题,可通过需要生成时手动启用,打包或不需要时注释掉的方式剔除devtools依赖 -->
</dependency>
1
2
3
4
5
6
7
8
9
10
11
  • Gradle项目引入依赖
compile("com.diboot:diboot-core-spring-boot-starter:{latestVersion}")
providedCompile("com.diboot:diboot-devtools-spring-boot-starter:{latestVersion}")
1
2

如果spring boot项目需要打jar包运行,则需要参考如下配置确保打包时剔除devtools:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <!-- 打jar包时剔除 -->
            <excludes>
                <exclude>
                    <groupId>com.diboot</groupId>
                    <artifactId>diboot-devtools-spring-boot-starter</artifactId>
                </exclude>
            </excludes>
        </configuration>
    </plugin>
</plugins>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

WARNING

  • diboot-devtools-spring-boot-starter 是用于开发过程的代码生成工具,须声明 exclude剔除devtools jar,以免打包至非开发环境。
  • 某些IDEA版本存在provided声明支持情况,可通过手动需要时启用,打包不需要时注释掉的方式剔除devtools依赖。

# 3. 添加配置信息

  • diboot-core starter依赖的数据源配置(已配置可忽略),Mysql示例:
#datasource config
spring.datasource.url=jdbc:mysql://localhost:3306/diboot_example?characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=diboot
spring.datasource.password=123456
spring.datasource.hikari.maximum-pool-size=5
spring.datasource.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
1
2
3
4
5
6
  • diboot-devtools 配置示例:
# Spring 相关配置
spring.main.allow-bean-definition-overriding=true

# diboot-devtools 相关配置
# 作者等注释信息
diboot.devtools.codes-version=1.0
diboot.devtools.codes-copyright=MyCompany
diboot.devtools.codes-author=MyName
# 代码生成路径及偏好
diboot.devtools.output-path=example/src/main/java/com/diboot/example/
diboot.devtools.output-path-sql=example/src/main/resources/
diboot.devtools.enable-lombok=true
#diboot.devtools.enable-swagger=false
#diboot.devtools.generate-mapper-xml=false
# 前端项目路径(diboot-element-admin 或 diboot-antd-admin项目,此处如果与后端项目为同一项目下不同模块,可只配置模块名,否则需要配置绝对路径,即全路径)
diboot.devtools.output-path-admin-ui=diboot-antd-admin/
# 移动端前端项目路径
#diboot.devtools.output-path-mobile-ui=diboot-mobile-ui/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  • diboot-devtools 配置参数说明:

    • spring.main.allow-bean-definition-overriding=true:遇到同样名字的Bean时,允许覆盖。

    以下diboot-devtools 相关配置均可根据实际情况填写

    • codes-version:当前使用diboot-devtools的版本号。
    • codes-copyright:生成代码的版权归属,显示在每个类或接口的注释中。
    • codes-author:生成代码的作者,显示在每个类或接口的注释中。
    • output-path:当前项目代码的默认起始路径
    • output-path-*:分别定义当前项目中EntityVOService及其实现类Mapper及映射文件Controller、等文件所在的路径。
    • output-path-sql:devtools生成的数据结构变更SQL存储路径,便于同步至非开发环境。
    • generate-mapper-xml:是否生成Mapper.xml文件,默认true
    • enable-lombok:是否引入Lombok注解,若设置true,请注意添加Lombok依赖。
    • enable-swagger:是否引入Swagger注解,若设置true,请注意添加Swagger依赖。
    • output-path-admin-ui:PC端管理后台前端Vue项目路径,用于前端代码生成(此处如果与后端项目为同一项目下不同模块,可只配置模块名,否则需要配置绝对路径,即全路径)。前端项目下载: diboot-element-admin (opens new window)diboot-antd-admin (opens new window)
    • output-path-mobile-ui:移动端前端项目路径,用于移动端端代码生成(此处如果与后端项目为同一项目下不同模块,可只配置模块名,否则需要配置绝对路径,即全路径)。前端项目下载: diboot-mobile-ui (opens new window)

如果您使用的是PostgreSQL或人大金仓数据库,还需要额外添加两行配置,以此来适配boolean类型字段所对应的数据库的boolean类型,需添加的配置如下:

mybatis-plus.global-config.db-config.logic-delete-value=true
mybatis-plus.global-config.db-config.logic-not-delete-value=false
1
2

# 4. 启动项目

以 Spring Boot 项目在IntelliJ IDEA中的一种启动方式为例:

在项目入口文件 Application 上点击右键,在弹出的菜单上点击 RUN 'Application'。 当出现类似下面提示时,表示启动成功:

: Started Application in 4.223 seconds (JVM running for 16.693)
1

diboot-devtools在初次运行中,会自动检测安装以diboot_前缀的数据库表。

# 5. 打开管理页面

在控制台(Console)上的启动日志最后,找到如下入口:

: Diboot devtools v2.x 初始化完成
: ...
: URL: http://localhost:8080/example/diboot/index.html
: ...
1
2
3
4

其中的URL即是devtools页面链接,点击即可打开Devtools操作界面。

个人用户初次使用需要扫码登录。

# 6. 生成依赖组件的初始化代码

进入devtools操作页面,初次进入会提示生成基础组件的初始化代码(将组件的controller等代码生成到本地方便修改)。 根据依赖的组件不同,可各自生成对应的初始化代码。

组件基础代码生成后,需要重启应用,以便后台接口生效。

如连接后端后发现验证码无法显示,请检查是否生成了IAM组件的基础代码并重启了应用

WARNING

  • Devtools是用于开发过程的代码生成工具,切勿将其打包至非开发环境。

实践准则:

  • 方式一(非开发环境取消devtools依赖):

    • Maven配置
      <!-- dependency节点 -->
      <dependency>
          <groupId>com.diboot</groupId>
          <artifactId>diboot-devtools-spring-boot-starter</artifactId>
          <scope>provided</scope><!-- 打war包时剔除,注: IDEA社区版请勿添加此行 -->
      </dependency>
      
      <!-- plugins节点配置 -->
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
              <!-- 打jar包时剔除 -->
              <excludes>
                  <exclude>
                      <groupId>com.diboot</groupId>
                      <artifactId>diboot-devtools-spring-boot-starter</artifactId>
                  </exclude>
              </excludes>
          </configuration>
      </plugin>
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      • Gradle项目引入依赖
        providedCompile("com.diboot:diboot-devtools-spring-boot-starter:$dibootDevtoolsVersion")
        
        1
  • 方式二(为devtools创建专属项目,与代码项目隔离开):

    单独为devtools建立一个专属项目,生成代码路径指向你的项目代码,需要使用devtools时启动这个devtools项目。

使用过程中遇到问题,可加群交流。