ThingsBoard3.9.1源码分析-CRUD操作客户customer

        最近在整理课程,基于最新ThingsBoard版本3.9.1/4.0,欢迎大家点赞,收藏、关注我,提问,在评论区留言,我争取把最新、最准、最好的课程,呈现在各位亦师亦友的csdn广大读者面前。

目录

对实体''客户''的增删改查操作

查询客户列表

查询单个客户信息

增加

删除

修改


对实体''客户''的增删改查操作

查询客户列表

(1)从web发起http请求

从前端发起,可以查询所有客户列表信息,也可以查询某个客户详情信息。

打开ui-ngx前端代码所在文件夹,在entity.servie.ts文件里,可以找到:

按名称过滤,在getEntitiesByNameFilter方法中实现,调用如下:

return this.getEntitiesByPageLink(entityType, pageLink, subType, config).pipe()

继续跟进getEntitiesByPageLink,

按分页查询列表,在getEntitiesByPageLink方法中实现,调用如下:

return this.getEntitiesByPageLinkObservable(entityType, pageLink, subType, config);

继续跟进getEntitiesByPageLinkObservable

查询实体分页可观察对象,在getEntitiesByPageLinkObservable方法中,调用如下:

case EntityType.CUSTOMER:
        pageLink.sortOrder.property = 'title';
        if (authUser.authority === Authority.CUSTOMER_USER) {
          entitiesObservable = this.getSingleCustomerByPageLinkObservable(pageLink, config);
        } else {
          entitiesObservable = this.customerService.getCustomers(pageLink, config);
        }
        break;

        说明:如上图getEntitiesByPageLinkObservable方法的核心作用是根据传入的实体类型(EntityType)和实体ID(entityId),动态选择对应的服务(Service),发送http请求去获取该实体的详细信息,并返回一个可观察对象(Observable)。

说明:

http请求的例子如下       

http://localhost:8088/api/customers?pageSize=10&page=0&sortProperty=createdTime&sortOrder=DESC

(2)触发CustomerController

(3)CustomerSerivce的实现类CustomerServiceImpl,如下

(4)CustomerDao的实现类JpaCustomerDao,如下

(5)CustomerRepository类的findByTenantId方法,如下

(6) 响应

Page<CustomerEntity> 包含分页元数据(如页码、总条数)和当前页的客户实体列表,如下:

{
    "data": [
        {
            "id": {
                "entityType": "CUSTOMER",
                "id": "639e8850-1a82-11f0-a30e-7b3d821fd0f9"
            },
            "createdTime": 1744780776789,
            "country": "China",
            "state": "jiangsu",
            "city": "nanjing",
            "address": "software road 108",
            "address2": null,
            "zip": "210012",
            "phone": "+8613500010001",
            "email": "13500010001@qeeking.cn",
            "title": "Customer D",
            "tenantId": {
                "entityType": "TENANT",
                "id": "bcfe4580-f899-11ef-9d5b-df9893071920"
            },
            "externalId": null,
            "version": 1,
            "name": "Customer D",
            "additionalInfo": {
                "description": "add by jingwd",
                "homeDashboardId": null,
                "homeDashboardHideToolbar": true
            }
        },
        {
            "id": {
                "entityType": "CUSTOMER",
                "id": "bd907360-f899-11ef-9d5b-df9893071920"
            },
            "createdTime": 1741052466582,
            "country": null,
            "state": null,
            "city": null,
            "address": null,
            "address2": null,
            "zip": null,
            "phone": null,
            "email": null,
            "title": "Customer B",
            "tenantId": {
                "entityType": "TENANT",
                "id": "bcfe4580-f899-11ef-9d5b-df9893071920"
            },
            "externalId": null,
            "version": 1,
            "name": "Customer B",
            "additionalInfo": null
        },
        {
           "id": {
                "entityType": "CUSTOMER",
                "id": "bd907361-f899-11ef-9d5b-df9893071920"
            },
            "createdTime": 1741052466582,
            "country": null,
            "state": null,
            "city": null,
            "address": null,
            "address2": null,
            "zip": null,
            "phone": null,
            "email": null,
            "title": "Customer C",
            "tenantId": {
                "entityType": "TENANT",
                "id": "bcfe4580-f899-11ef-9d5b-df9893071920"
            },
            "externalId": null,
            "version": 1,
            "name": "Customer C",
            "additionalInfo": null
        },
        {
            "id": {
                "entityType": "CUSTOMER",
                "id": "bd7aa170-f899-11ef-9d5b-df9893071920"
            },
            "createdTime": 1741052466439,
            "country": null,
            "state": null,
            "city": null,
            "address": null,
            "address2": null,
            "zip": null,
            "phone": null,
            "email": null,
            "title": "Customer A",
            "tenantId": {
                "entityType": "TENANT",
                "id": "bcfe4580-f899-11ef-9d5b-df9893071920"
            },
            "externalId": null,
            "version": 1,
            "name": "Customer A",
            "additionalInfo": null
        }
    ],
    "totalPages": 1,
    "totalElements": 4,
    "hasNext": false
}

查询单个客户信息

前端请求:

后台CustomerController:

增加

(1)从web发起http请求

POST http://localhost:8088/api/customer

{
  "title": "1",
  "additionalInfo": {
    "description": "1",
    "homeDashboardId": null,
    "homeDashboardHideToolbar": true
  },
  "country": null,
  "city": null,
  "state": null,
  "zip": null,
  "address": null,
  "address2": null,
  "phone": null,
  "email": null
}

(2)触发CustomerController

(3)DefaultTbCustomerService类的save方法,如下

(4)CustomerService接口的实现类CustomerServiceImpl,saveCustomer方法,如下

调用saveCustomer方法,如下:

customer CustomerDao;

public interface CustomerDao extends

Dao<Customer>, TenantEntityDao, ExportableEntityDao<CustomerId, Customer> {}

调用Dao的saveAndFlush方法。

实际调用的saveAndFlush方法,在JpaAbstractDao类中,如下:

删除

删除操作:

DELETE http://localhost:8088/api/customer/5b68eb00-36d2-11f0-a721-df5b2f1fe558

查询操作:

http://localhost:8088/api/customers?pageSize=10&page=0&sortProperty=createdTime&sortOrder=DESC

修改

http请求:

POST http://localhost:8088/api/customer

{
  "id": {
    "entityType": "CUSTOMER",
    "id": "4513e750-36dd-11f0-aacc-273881eb978b"
  },
  "createdTime": 1747898442309,
  "country": null,
  "state": null,
  "city": null,
  "address": null,
  "address2": null,
  "zip": null,
  "phone": null,
  "email": null,
  "title": "22",  //修改的新值
  "tenantId": {
    "entityType": "TENANT",
    "id": "bcfe4580-f899-11ef-9d5b-df9893071920"
  },
  "externalId": null,
  "version": 1,
  "name": "2",
  "additionalInfo": {
  "description": "22", //修改的新值
  "homeDashboardId": null,
  "homeDashboardHideToolbar": true

  }

}

http响应:

{
    "id": {
        "entityType": "CUSTOMER",
        "id": "4513e750-36dd-11f0-aacc-273881eb978b"
    },
    "createdTime": 1747898442309,
    "country": null,
    "state": null,
    "city": null,
    "address": null,
    "address2": null,
    "zip": null,
    "phone": null,
    "email": null,
    "title": "22",
    "tenantId": {
        "entityType": "TENANT",
        "id": "bcfe4580-f899-11ef-9d5b-df9893071920"
    },
    "externalId": null,
    "version": 2,
    "name": "22",
    "additionalInfo": {
        "description": "22",
        "homeDashboardId": null,
        "homeDashboardHideToolbar": true
    }
}

结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值