# 第一个CRUD

本章节主要讲解如何在平台中创建第一个CRUD

本平台内置通用 Mapper、通用 Service、通用controller,可实现单表大部分 CRUD 操作,性能基本无损耗,直接面向对象操作,具体介绍见 base模块详解

# 初始化数据库

1.以Mysql为例,执行创建数据库demo脚本(如果已经创建数据库请忽略该步骤)。

CREATE DATABASE IF NOT EXISTS demo 
DEFAULT CHARACTER SET utf8mb4
DEFAULT COLLATE utf8mb4_general_ci;

create_database_1

2.执行创建表脚本

CREATE TABLE `demo`.`staff` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `name` varchar(60) DEFAULT NULL COMMENT '姓名',
  `gender` varchar(2) DEFAULT NULL COMMENT '性别',
  `age` int(2) DEFAULT NULL COMMENT '年龄',
  `email` varchar(60) DEFAULT NULL COMMENT '邮箱',
  `phone` varchar(60) DEFAULT NULL COMMENT '手机号',
  `org_id` varchar(32) DEFAULT NULL COMMENT '部门id',
  `description` varchar(255) DEFAULT NULL COMMENT '描述',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工信息表';

create_database_2

3.对应application-dev.yml上添加数据库相关配置

数据库相关信息请根据实际情况修改,本配置仅作为示例

spring:
  application:
    name: hos-app
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      url: jdbc:mysql://192.16.18.196:10015/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
      username: xxxx
      password: xxxx
      driver-class-name: com.mysql.jdbc.Driver

# entity

1.在模块oa-user-modelcom.mediway.oa.user.model.entity包下创建员工信息实体类,注意需要继承com.mediway.hos.database.model.BaseEntity;在类上添加@TableName注解,value值为对应的表名; 在实体字段上添加@TableField注解,具体注解使用参考https://baomidou.com/pages/223848/ (opens new window)

package com.mediway.oa.user.model.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.mediway.hos.database.model.BaseEntity;
import lombok.Data;

@TableName(value = "`staff`")
@Data
public class Staff extends BaseEntity {

    /**
     * 姓名
     */
    @TableField(value = "`name`")
    private String name;

    /**
     * 性别
     */
    @TableField(value = "`gender`")
    private String gender;

    /**
     * 年龄
     */
    @TableField(value = "`age`")
    private Integer age;

    /**
     * 部门id
     */
    @TableField(value = "`org_id`")
    private String orgId;

    /**
     * 邮箱
     */
    @TableField(value = "`email`")
    private String email;

    /**
     * 手机号
     */
    @TableField(value = "`phone`")
    private String phone;


    /**
     * 描述
     */
    @TableField(value = "`description`")
    private String description;

}

# mapper

1.在模块oa-user-servicecom.mediway.oa.user.mapper包下创建员工信息mapper接口,注意需要继承com.baomidou.mybatisplus.core.mapper.BaseMapper接口,BaseMapper接口封装了基础的CRUD方法。

package com.mediway.oa.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mediway.oa.user.model.entity.Staff;

public interface StaffMapper extends BaseMapper<Staff> {

}

2.在模块oa-user-serviceresources/mapper/user包下创建员工信息mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mediway.oa.user.mapper.StaffMapper">
    <resultMap id="BaseResultMap" type="com.mediway.oa.user.model.entity.Staff">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="VARCHAR" property="id" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="phone" jdbcType="VARCHAR" property="phone" />
        <result column="age" jdbcType="INTEGER" property="age" />
        <result column="gender" jdbcType="VARCHAR" property="gender" />
        <result column="description" jdbcType="VARCHAR" property="description" />
        <result column="email" jdbcType="VARCHAR" property="email" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="org_id" jdbcType="VARCHAR" property="orgId" />
    </resultMap>

</mapper>

# service

1.在模块oa-user-servicecom.mediway.oa.user.service包下创建员工信息service接口,注意需要继承com.mediway.hos.database.service.BaseService接口,BaseService接口封装了基础的CRUD方法。

package com.mediway.oa.user.service;

import com.mediway.hos.database.service.BaseService;
import com.mediway.oa.user.model.entity.Staff;
import org.springframework.stereotype.Service;

@Service
public interface StaffService extends BaseService<Staff> {

}

2.在模块oa-user-servicecom.mediway.oa.user.service.impl包下创建员工信息serviceImpl类,记住需要继承com.mediway.hos.database.service.impl.BaseServiceImpl,并且实现StaffService接口。

package com.mediway.oa.user.service.impl;

import com.mediway.hos.base.service.impl.BaseServiceImpl;
import com.mediway.oa.user.mapper.StaffMapper;
import com.mediway.oa.user.model.entity.Staff;
import com.mediway.oa.user.service.StaffService;
import org.springframework.stereotype.Service;

@Service
public class StaffServiceImpl extends BaseServiceImpl<StaffMapper, Staff> implements StaffService {

}

# api

在模块oa-user-apicom.mediway.oa.user.api包下创建员工模块交互接口api。 @FeignClient的注解value值为该模块以微服务启动的服务名,path对应controller的注解@RequestMapping路径

package com.mediway.oa.user.api;

import org.springframework.cloud.openfeign.FeignClient;

@FeignClient(value = "oa-user-server", path = "/staff")
public interface StaffApi {

}

# controller

在模块oa-user-controllercom.mediway.oa.user.controller包下创建员工信息controller, 注意需要继承com.mediway.hos.database.controller.BaseController, 并且实现StaffApi接口。 BaseController中封装了基础的CRUD方法,子类可直接使用,无需编码。

package com.mediway.oa.user.controller;

import com.mediway.hos.database.controller.BaseController;
import com.mediway.oa.user.model.entity.Staff;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user/staff")
public class StaffController extends BaseController<Staff> implements StaffApi {

}

@RequestMapping("/user/staff") 中/user为oa-user模块中所有url的统一前缀, 每个模块需要单独定义唯一的编码,以便通过网关进行路由转发,二者需要保持一致

本平台提供了代码生成器可以快速生成后端代码,具体使用教程见代码生成器