添加注释

This commit is contained in:
myh 2024-01-04 15:56:33 +08:00
parent 1d99f46099
commit bae2a8d26b
4 changed files with 17 additions and 1 deletions

View File

@ -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({

View File

@ -1,7 +1,9 @@
import { Entity, Column, PrimaryGeneratedColumn, Table } from 'typeorm';
// 实体层
@Entity()
export class Users {
// 以下是数据库基本字段
@PrimaryGeneratedColumn()
id: number;

View File

@ -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<Users[]> {
return await this.userService.findAll();
}
// 根据id查找用户
@Get("/findById/:id")
async findById(@Param('id') id: number): Promise<Users> {
return await this.userService.findOne(id);
}
// 根据id删除用户
@Delete("/remove/:id")
async remove(@Param('id') id: number): Promise<void> {
await this.userService.remove(id);
}
// 新建用户
@Post("/save")
async save(@Body() user: Users): Promise<Users> {
return await this.userService.save(user)
}
// 根据id修改用户
@Put("/update")
async update(@Param('id') id: number, @Body() user: Users): Promise<void> {
this.userService.update(id, user);

View File

@ -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<Users>) { }
// 查找所有用户
async findAll(): Promise<Users[]> {
return await this.userRepository.find();
}
// 根据id查找用户
async findOne(id: number): Promise<Users> {
return await this.userRepository.findOne(id)
}
// 根据id删除用户
async remove(id: number): Promise<void> {
await this.userRepository.delete(id)
}
// 新建用户
async save(user: Users): Promise<Users> {
return await this.userRepository.save(user)
}
// 根据id修改用户
async update(id: number, user: Users): Promise<void> {
await this.userRepository.update(id, user);
}