current position:Home>Blog project (4, initialization)

Blog project (4, initialization)

2022-08-06 17:29:55Ning Xuan

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第5天,点击查看活动详情

一、前言

上一章中,I designed the user module for it、文章模块、Modules such as classification labels have carried out database design work,The beginning of this chapter is to develop the code for the designed personal blog

in several processes of project development,需求分析,原型设计,The database design has been completed,This chapter mainly explains the initialization of the project,包括了mybatis-plus的代码自动生成器,swagger-ui的api文档功能,使用lombok注解

二、使用gitee仓库

Since it is a blog made by rookies to practice hands,In the case of serious lack of depth,There must be breadth,所以采用了gitee作为代码仓库,进行代码管理(Although useless,都是使用idea内置的git插件)

具体giteeCreate information as shown,Because I developed it myself,Branch model onlymaster,没有其他分支

image.png

About the stuff about open source licenses,我也不太懂,I found an explanation about the open source license on the Internet,如下

协议简述
Apache允许他人修改源代码后再闭源,但是必须对每个修改过的文件做版权说明
GPL3无论以何种方式修改或者使用代码,都需要开源
MIT允许他人修改源代码后再闭源,不用对修改过的文件做说明,且二次开发的软件可以使用原作者的名字做营销
BSD2/BSD3和上面一条类似,but without prior written permission,The name of the copyright owner or its contributors may not be used for promotion
BSL和GPL类似,但不需要复制版权信息
CCZ放弃创作的作品版权权益,并将其奉献给大众,不对代码做任何担保
EPL与GPL类似,有权使用、修改、复制与发布软件原始版本和修改后版本,但在某些情况下则必须将修改内容一并释出
AGPLGPL拓展,使用在线网络服务的也需要开源
GPL2和GPL3相比,如果使用代码作为服务提供,而不分发软件,则不需要开源
LGPL和GPL相比,LGPL允许商业软件通过类库引用(link)方式使用LGPL类库而不需要开源商业软件的代码
Mozilla与LGPL类似,但是需要对修改过的源码内容做说明
Unlicense与CCZ相似,且开放商标和所用的专利授权

gitee项目地址

三、初始化项目

将项目拉到本地,idea打开,新增文件夹(src/main/....)、pom文件等

The structure of the initialization end project is as follows

image.png

pom文件添加依赖如下:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- aop-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <!-- mysql驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- 数据源-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.13</version>
    </dependency>
    <!-- lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <!-- mybatis-plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.1</version>
    </dependency>
    <!-- mybatis-plus代码生成器-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.5.1</version>
    </dependency>
        <!-- 模板引擎-->
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.28</version>
    </dependency>
    <!-- swagger -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
    <!-- swagger前端页面 -->
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
        <version>2.0.7</version>
    </dependency>

</dependencies>
复制代码

Everything works fine when starting the project,over

image.png

四、使用mybatis-plusCode code generator

The database table name is given byblogs_ip改为了access_ip,The prefix is ​​ignored when using the code generatorblogs之后,类名变成了IP, 感觉不太好

mybatis-plus官网

mybatis-plus官方代码生成器

FastAutoGenerator.create("url", "username", "password")
    .globalConfig(builder -> {
        builder.author("baomidou") // 设置作者
            .enableSwagger() // 开启 swagger 模式
            .fileOverride() // 覆盖已生成文件
            .outputDir("D://"); // 指定输出目录
    })
    .packageConfig(builder -> {
        builder.parent("com.baomidou.mybatisplus.samples.generator") // 设置父包名
            .moduleName("system") // 设置父包模块名
            .pathInfo(Collections.singletonMap(OutputFile.xml, "D://")); // 设置mapperXml生成路径
    })
    .strategyConfig(builder -> {
        builder.addInclude("t_simple") // 设置需要生成的表名
            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
    })
    .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
    .execute();
复制代码

I changed it on the official code generator,如controller的@RestController注解,实体类上的@Lombok注解,具体如下所示

 public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1/blog?useSSL=false";
        String username = "root";
        String password = "123456";
        String authorName = "NingXuan";
        String packName = "com.ningxuan.blog";


        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(authorName) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .commentDate("yyyy-MM-dd") // 注释日期
                            .outputDir("C:\Users\My\Desktop\learn\blog\src\main\java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder
                            .parent(packName) // 设置父包名
// .moduleName("blog") // 设置父包模块名
                            .entity("model")    // 设置实体类包名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "C:\Users\My\Desktop\learn\blog\blog\src\main\resources\mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("blogs_archive", "blogs_article","blogs_article_comment","blogs_article_info","blogs_category","blogs_file",
                            "access_ip","blogs_label","blogs_message","city","friend_link","sys_user") // 设置需要生成的表名
                            .addTablePrefix("blogs") // 设置过滤表前缀
                    ;
                    builder.entityBuilder()     // 设置实体类
                            .enableLombok()     // 开启lombok模型
                            .enableTableFieldAnnotation()   // 开启生成实体时生成字段注解
                            .idType(IdType.ASSIGN_ID)       // id自动生成,使用雪花算法
                    ;
                    builder.controllerBuilder()
                            .enableRestStyle(); // 开启生成@RestController 控制器
                    builder.mapperBuilder()
                            .enableMapperAnnotation();  // 开启mapper注解
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .execute();

    }
复制代码

mybatis-plus代码生成器配置

The directory structure after generation

image.png

But it's me who suffers more

五、补充

别的不说,The item that I just pulled down is this virtue and I am speechless,Somewhat overwhelmed,It looked very unpleasant,A lot is missing, 我的src呢

image.png

copyright notice
author[Ning Xuan],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/218/202208061702144511.html

Random recommended