From bae2a8d26b33060e15bad047aa41b0813367c099 Mon Sep 17 00:00:00 2001 From: myh Date: Thu, 4 Jan 2024 15:56:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.module.ts | 1 + src/entity/user.entity.ts | 2 ++ src/users/users.controller.ts | 6 ++++++ src/users/users.service.ts | 9 ++++++++- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/app.module.ts b/src/app.module.ts index a970861..612f6ff 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -3,6 +3,7 @@ import { UserModule } from './users/users.module'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Users } from './entity/user.entity'; +// 数据库连接池配置 @Module({ imports: [ TypeOrmModule.forRoot({ diff --git a/src/entity/user.entity.ts b/src/entity/user.entity.ts index 5762636..4493a04 100644 --- a/src/entity/user.entity.ts +++ b/src/entity/user.entity.ts @@ -1,7 +1,9 @@ import { Entity, Column, PrimaryGeneratedColumn, Table } from 'typeorm'; +// 实体层 @Entity() export class Users { + // 以下是数据库基本字段 @PrimaryGeneratedColumn() id: number; diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 8e894cc..753da76 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -2,30 +2,36 @@ import { Body, Controller, Delete, Get, Injectable, Param, Patch, Post, Put } fr import { UserService } from 'src/users/users.service'; import { Users } from '../entity/user.entity'; +// @Injectable()注入器 @Controller('users') export class UserController { constructor(private readonly userService: UserService) { } + // 查找所有用户 @Get("/findAll") async findAll(): Promise { return await this.userService.findAll(); } + // 根据id查找用户 @Get("/findById/:id") async findById(@Param('id') id: number): Promise { return await this.userService.findOne(id); } + // 根据id删除用户 @Delete("/remove/:id") async remove(@Param('id') id: number): Promise { await this.userService.remove(id); } + // 新建用户 @Post("/save") async save(@Body() user: Users): Promise { return await this.userService.save(user) } + // 根据id修改用户 @Put("/update") async update(@Param('id') id: number, @Body() user: Users): Promise { this.userService.update(id, user); diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 3cd78fd..389f489 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -1,30 +1,37 @@ import { Users } from "src/entity/user.entity"; import { Injectable } from "@nestjs/common"; import { InjectRepository } from "@nestjs/typeorm"; -import { FindOneOptions, Repository } from "typeorm"; +import { Repository } from "typeorm"; +// service层 @Injectable() export class UserService { + // 注入器 constructor( @InjectRepository(Users) private userRepository: Repository) { } + // 查找所有用户 async findAll(): Promise { return await this.userRepository.find(); } + // 根据id查找用户 async findOne(id: number): Promise { return await this.userRepository.findOne(id) } + // 根据id删除用户 async remove(id: number): Promise { await this.userRepository.delete(id) } + // 新建用户 async save(user: Users): Promise { return await this.userRepository.save(user) } + // 根据id修改用户 async update(id: number, user: Users): Promise { await this.userRepository.update(id, user); }