头图
A management system often requires back-end + front-end to be implemented together. Single-table CRUD operations are often similar, we can use a code generator to achieve. Sometimes our management system only needs some simple CRUD pages. Is there any framework that can achieve a pure Java management system without writing front-end code? I recommend a full-stack framework Erupt here, I hope it will be helpful to everyone!

SpringBoot actual combat e-commerce project mall (40k+star) address: https://github.com/macrozheng/mall

Introduction to Erupt

Erupt is a low-code full-stack class framework, which uses Java annotations to dynamically generate pages and add, delete, modify, check, permission control and other background functions. Zero front-end code, zero CURD, automatic table , only one class file of 160ac5a31abec6 + concise annotation configuration, rapid development of enterprise-level background management system.

Basic use

Let's first come to the actual battle in Poland, taking product brand management as an example, to get familiar with the basic use of Erupt combined with SpringBoot!

SpringBoot integrates Erupt

Since Erupt natively supports SpringBoot, integration is still very convenient!
  • In order to facilitate the management of the Erupt version, we first add the version attribute of pom.xml
<properties>
    <erupt.version>1.6.13</erupt.version>
</properties>
  • Then add Erupt's permission management, data security, background WEB interface and MySQL driver dependency pom.xml
<dependencies>
    <!--用户权限管理-->
    <dependency>
        <groupId>xyz.erupt</groupId>
        <artifactId>erupt-upms</artifactId>
        <version>${erupt.version}</version>
    </dependency>
    <!--接口数据安全-->
    <dependency>
        <groupId>xyz.erupt</groupId>
        <artifactId>erupt-security</artifactId>
        <version>${erupt.version}</version>
    </dependency>
    <!--后台WEB界面-->
    <dependency>
        <groupId>xyz.erupt</groupId>
        <artifactId>erupt-web</artifactId>
        <version>${erupt.version}</version>
    </dependency>
    <!--Mysql数据库驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>
</dependencies>
  • Modify the application.yml file of the project, add data source and JPA configuration;
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/erupt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
  jpa:
    show-sql: true
    generate-ddl: true
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    database: mysql
  • Create the following configuration files in the resources mall-tiny-erupt in 060ac5a31ac121);

  • Add Erupt's Java configuration class EruptConfig , based on MallTinyApplication , configure the package scan code path;
/**
 * Created by macro on 2021/4/13.
 */
@Configuration
@ComponentScan({"xyz.erupt","com.macro.mall.tiny"})
@EntityScan({"xyz.erupt","com.macro.mall.tiny"})
@EruptScan({"xyz.erupt","com.macro.mall.tiny"})
public class EruptConfig {
}
  • erupt database in MySQL, and then use the startup class to run the project. The following table will be automatically created erupt

  • After the project is successfully launched, you can directly visit the login page, the default account password is erupt:erupt , and the project access address: http://localhost :8080/

  • After the login is successful, it will jump to the project homepage. We can find that we have not written a line of front-end code, but have complete authority management and dictionary management functions. Isn't it great!

Implement single-table CRUD

Use the core annotations @Erupt and @EruptField define an entity class to quickly complete the CRUD operation. Let's take the product brand management as an example.
  • No Controller, Service, Dao are needed, only one entity class can complete CRUD. First, we create the entity class PmsBrand ;
@Erupt(name = "商品品牌")
@Table(name = "pms_brand")
@Entity
public class PmsBrand {

    @Id
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "native")
    @Column(name = "id")
    @EruptField
    private Long id;

    @EruptField(
            views = @View(title = "品牌名称"),
            edit = @Edit(title = "品牌名称",notNull=true,search = @Search(vague = true))
    )
    private String name;

    @EruptField(
            views = @View(title = "品牌首字母"),
            edit = @Edit(title = "品牌首字母",notNull=true)
    )
    private String firstLetter;

    @EruptField(
            views = @View(title = "品牌LOGO"),
            edit = @Edit(title = "品牌LOGO", type = EditType.ATTACHMENT,
                    attachmentType = @AttachmentType(type = AttachmentType.Type.IMAGE))
    )
    private String logo;

    @EruptField(
            views = @View(title = "品牌专区大图"),
            edit = @Edit(title = "品牌专区大图", type = EditType.ATTACHMENT,
                    attachmentType = @AttachmentType(type = AttachmentType.Type.IMAGE))
    )
    private String bigPic;

    @EruptField(
            views = @View(title = "品牌故事"),
            edit = @Edit(title = "品牌故事")
    )
    private String brandStory;

    @EruptField(
            views = @View(title = "排序"),
            edit = @Edit(title = "排序")
    )
    private Integer sort;

    @EruptField(
            views = @View(title = "是否显示"),
            edit = @Edit(title = "是否显示")
    )
    private Boolean showStatus;

    @EruptField(
            views = @View(title = "品牌制造商"),
            edit = @Edit(title = "品牌制造商")
    )
    private Boolean factoryStatus;

    private Integer productCount;

    private Integer productCommentCount;

}
  • After the creation is successful, restart the project, and add a first-level menu menu maintenance;

  • Then add a call brand management of secondary menu, pay attention to choose a good menu and type previous menu, enter type value for the class name of the entity class PmsBrand ;

  • After the menu is added successfully, refresh the page and the complete brand management function will appear, let’s try to add it;

  • @Edit at the query list page again, we can find that we use the 060ac5a31ac618 annotation to convert the entity type fields into different input controls, such as text boxes, image upload boxes, radio buttons, and value boxes.

Core annotation description

A few core notes of Erupt, just compare the code in PmsBrand to learn!

@Erupt

  • name: function name
  • desc: functional description

@EruptField

  • views: table display configuration
  • edit: edit item configuration
  • sort: front-end display order, the smaller the number, the higher the front

@View

  • title: table column name
  • desc: table column description
  • type: data display format, the default is AUTO, which can be inferred according to the attribute type
  • show: Whether to display

@Edit

  • title: table column name
  • desc: table column description
  • type: edit type, the default is AUTO, which can be inferred according to the attribute type
  • show: Whether to display
  • notNull: Is it required?
  • search: Whether to support search, search = @Search(vague = true) will enable advanced query strategy

Expansion module

Of course, the function of Erupt is much more than that. It also integrates many practical system functions, including timing tasks, code generators, system monitoring, and NoSQL support.

Timed task erupt-job

Through the timing task function, we can define the timing task in the code, and then operate the task in the graphical interface, a bit like the PowerJob before!
  • First, we need to add erupt-job related dependencies pom.xml
<!--定时任务erupt-job-->
<dependency>
    <groupId>xyz.erupt</groupId>
    <artifactId>erupt-job</artifactId>
    <version>${erupt.version}</version>
</dependency>
  • Then application.yml (otherwise an error will be reported when starting);
spring:
  mail:
    username: xxxxxx@qq.com
    password: 123456
    host: smtp.exmail.qq.com
    port: 465
    properties:
      mail.smtp.ssl.auth: true
      mail.smtp.ssl.enable: true
      mail.smtp.ssl.required: true
  • Then create a timed task implementation class JobHandlerImpl , add the timed task execution code in the exec
/**
 * Created by macro on 2021/4/13.
 */
@Service
@Slf4j
public class JobHandlerImpl implements EruptJobHandler {
    @Override
    public String exec(String code, String param) throws Exception {
        log.info("定时任务已经执行,code:{},param:{}",code,param);
        return "success";
    }
}
  • Then restart the application and task maintenance, which will be executed every 5 seconds;

  • After successfully added, the timing of the task started, click on the task list Log button to view the execution log.

Code generator erupt-generator

If you think handwriting entity classes are more troublesome, you can also use the code generator in Erupt.
  • Add erupt-generator related dependencies in pom.xml
<!-- 代码生成器 erupt-generator -->
<dependency>
    <groupId>xyz.erupt</groupId>
    <artifactId>erupt-generator</artifactId>
    <version>${erupt.version}</version>
</dependency>
  • In the code generation menu, we can directly add tables and fields just like in Navicat to generate entity code;

  • In the process of adding, we can find that there are quite a lot of editing types 30 ;

  • After the addition is successful, click the code preview button of the list item to directly generate the code and copy it to your own project.

System monitoring erupt-monitor

By using Erupt's system monitoring function, we can view server configuration, Redis cache usage, and online user information.
  • Add erupt-monitor related dependencies in pom.xml
<!--服务器监控 erupt-monitor-->
<dependency>
    <groupId>xyz.erupt</groupId>
    <artifactId>erupt-monitor</artifactId>
    <version>${erupt.version}</version>
</dependency>
  • Because Redis needs to be used, application.yml , and the Redis storage function of Session should be turned on;
spring:
  redis:
    host: localhost # Redis服务器地址
    database: 1 # Redis数据库索引(默认为0)
    port: 6379 # Redis服务器连接端口
    password: 123456 # Redis服务器连接密码(默认为空)
    timeout: 3000ms # 连接超时时间
erupt:
  # 开启redis方式存储session,默认false,开启后需在配置文件中添加redis配置
  redisSession: true
  • Through the service monitoring menu, you can view the server's CPU, memory and Java virtual machine information;

  • Through the cache monitoring menu, you can view Redis information, command statistics and Redis Key statistics;

  • Through the online user menu, you can view the online user information, and you can also force the user to log out!

NoSQL data source erupt-mongodb

Erupt supports a variety of data sources, including: MySQL, Oracle, PostgreSQL, H2, and even MongoDB. Let's experience the support functions of MongoDB.
  • Add erupt-mongodb related dependencies in pom.xml
<!--NoSQL数据源 erupt-mongodb-->
<dependency>
    <groupId>xyz.erupt</groupId>
    <artifactId>erupt-mongodb</artifactId>
    <version>${erupt.version}</version>
</dependency>
  • Since MongoDB needs to be used, the MongoDB configuration application.yml
spring:
  data:
    mongodb:
      host: localhost # mongodb的连接地址
      port: 27017 # mongodb的连接端口号
      database: erupt # mongodb的连接的数据库
  • Take a simplified version of commodity management as an example, or a familiar routine, add a PmsProduct entity class;
/**
 * Created by macro on 2021/4/13.
 */
@EruptDataProcessor(EruptMongodbImpl.MONGODB_PROCESS)  //此注解表示使用MongoDB来存储数据
@Document(collection = "product")
@Erupt(
        name = "商品管理",
        orderBy = "sort"
)
public class PmsProduct {
    @Id
    @EruptField
    private String id;

    @EruptField(
            views = @View(title = "商品名称", sortable = true),
            edit = @Edit(title = "商品名称", search = @Search(vague = true))
    )
    private String name;

    @EruptField(
            views = @View(title = "副标题", sortable = true),
            edit = @Edit(title = "副标题", search = @Search(vague = true))
    )
    private String subTitle;

    @EruptField(
            views = @View(title = "价格", sortable = true),
            edit = @Edit(title = "价格")
    )
    private Double price;

    @EruptField(
            views = @View(title = "商品图片"),
            edit = @Edit(title = "商品图片", type = EditType.ATTACHMENT,
                    attachmentType = @AttachmentType(type = AttachmentType.Type.IMAGE))
    )
    private String pic;

    @EruptField(
            views = @View(title = "状态", sortable = true),
            edit = @Edit(title = "状态",
                    boolType = @BoolType(trueText = "上架", falseText = "下架"),
                    search = @Search)
    )
    private Boolean publishStatus;

    @EruptField(
            views = @View(title = "创建时间", sortable = true),
            edit = @Edit(title = "创建时间", search = @Search(vague = true))
    )
    private Date createTime;
}
  • The difference from the previous operation of MySQL is that MongoDB is specified to store data @EruptDataProcessor @Table annotation is changed to use the @Document annotation;
@EruptDataProcessor(EruptMongodbImpl.MONGODB_PROCESS)  //此注解表示使用MongoDB来存储数据
@Document(collection = "product")
@Erupt(
        name = "商品管理",
        orderBy = "sort"
)
public class PmsProduct {
    //...省略若干代码
}
  • The next step is to add a commodity management menu maintenance, and you can see this function after refreshing it.

Online interface development erupt-magic-api

Finally, I will introduce a magical function. You can develop the interface directly through the UI interface without defining Java objects such as Controller, Service, Dao, Mapper, XML, VO, etc.!
  • Add erupt-magic-api related dependencies in pom.xml
<!--在线接口开发 erupt-magic-api-->
<dependency>
    <groupId>xyz.erupt</groupId>
    <artifactId>erupt-magic-api</artifactId>
    <version>${erupt.version}</version>
</dependency>
  • Add magic-api related configuration in application.yml
erupt:
  # 设置具体哪些包被jackson消息转化而不是gson
  jacksonHttpMessageConvertersPackages:
    - org.ssssssss

magic-api:
  web: /magic/web
  # 接口配置文件存放路径
  resource.location: D:/erupt/magic-script
  • We can directly magic-api , such as the following script, which is used to query all brands;
var sql = "select * from pms_brand";    
return db.select(sql);
  • Add this script directly in the interface configuration menu to realize the brand list query interface without writing additional code;

  • Visit the interface directly in the browser and find that the interface has been automatically generated, isn't it great!

to sum up

If your need is to build a back-end management system that does not have a complicated business, Erupt is a good choice! It allows you not to write front-end code! But if your demand side has a lot of requirements for the interface, and your business logic is more complicated, then you have to implement the front end yourself!

Reference

Official document: https://www.yuque.com/erupts/erupt

Project source address

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-erupt

This article GitHub https://github.com/macrozheng/mall-learning has been included, welcome to Star!

macrozheng
1.1k 声望1.3k 粉丝