// 安装脚手架(只需要安装一次,因为这个是全局的)
npm i -g @nestjs/cli
// 创建项目
nest new project-name
// (该过程有个选择包管理工具的,我选的yarn)
yarn run start:dev
// 可以在浏览器访问localhost:3000 输出helloWorld
// 安装依赖
yarn add --save @nestjs/typeorm typeorm mysql2
// @nestjs/typeorm这个本人运行上面命令一直安装不上,只好单独安装一下就成功了(yarn add @nestjs/typeorm)
nest g res user2
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User2Module } from './user2/user2.module';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'admin',
database: 'shop',
autoLoadEntities: true,
synchronize: true,
}),
User2Module,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class User2 {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column({ default: true })
isActive: boolean;
}
import { Module } from '@nestjs/common';
import { User2Service } from './user2.service';
import { User2Controller } from './user2.controller';
// 引入实体
import { User2 } from './entities/user2.entity';
// 引入orm框架
import {TypeOrmModule} from '@nestjs/typeorm'
@Module({
imports:[TypeOrmModule.forFeature([User2])],
controllers: [User2Controller],
providers: [User2Service],
})
export class User2Module {}
到这一步,运行项目会发现数据库多出来一张user2的表,表字段和user2.entity.ts一一对应
接下来三个文件内都是逻辑代码
// 这里在添加数据用上了
export class CreateUser2Dto {
firstName: string;
lastName: string;
isActive: boolean;
}
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
Query,
DefaultValuePipe,
ParseIntPipe,
} from '@nestjs/common';
import { User2Service } from './user2.service';
import { CreateUser2Dto } from './dto/create-user2.dto';
import { UpdateUser2Dto } from './dto/update-user2.dto';
@Controller('user2')
export class User2Controller {
constructor(private readonly user2Service: User2Service) {}
@Post() // 这里添加数据使用了apifox,调至post接口,Body-json类型,将添加的字段依次输入即可,直接发请求 http://localhost:3000/user2
async create(@Body() createUser2Dto: CreateUser2Dto) {
const res = await this.user2Service.create(createUser2Dto);
if (res.generatedMaps.length > 0) {
return {
success: '添加成功',
};
} else {
error: '添加失败';
}
return this.user2Service.create(createUser2Dto);
}
// 查询所有
@Get()
findAll(
@Query('current', new DefaultValuePipe(1), ParseIntPipe) current: number, // 页数,默认值为1,int类型
@Query('pagesize', new DefaultValuePipe(15), ParseIntPipe) pagesize: number, // 每页条数,默认值15
) {
// 传入当前页数,number类型的
return this.user2Service.findAll(current, pagesize);
}
// 查询单条信息
@Get(':id')
findOne(@Param('id', ParseIntPipe) id: number) {
// 将id类型转换为int类型限制为number类型
return this.user2Service.findOne(+id);
}
// 更新(修改数据) 这里修改数据使用了apifox,调至post接口,Body-json类型,将添加的字段依次输入即可,直接发请求 http://localhost:3000/user2/3
@Patch(':id')
async update(
@Param('id') id: string,
@Body() updateUser2Dto: UpdateUser2Dto,
) {
const user = await this.user2Service.findOne(+id);
if (user === null) {
return {
error: '没有此用户,暂时无法修改',
};
}
const res = await this.user2Service.update(+id, updateUser2Dto);
if (res.affected > 0) {
return {
success: '修改成功',
};
} else {
error: '修改失败';
}
}
// 根据id删除单个用户
@Delete(':id')
async remove(@Param('id') id: number) {
// 先查询到这个用户(这里直接调用根据id查询用户的方法)
const user = await this.user2Service.findOne(id); // 在findOne这个方法里面的findOne是使用了promise,因此这里需要异步一下
if (user === null) {
return { error: '删除的用户不存在' };
}
const res = this.user2Service.remove(+id);
if ((await res).affected > 0) {
return {
success: '删除成功',
};
} else {
error: '删除失败';
}
}
}
import { Injectable } from '@nestjs/common';
import { CreateUser2Dto } from './dto/create-user2.dto';
import { UpdateUser2Dto } from './dto/update-user2.dto';
import { InjectRepository } from '@nestjs/typeorm';
// 1.导入实体
import { User2 } from './entities/user2.entity';
// 2.导入依赖注入
import { Repository } from 'typeorm';
@Injectable()
export class User2Service {
// 3.需要对数据库进行操作,这里需要进行依赖注入
constructor(
@InjectRepository(User2)
private user2Repoitory: Repository<User2>,
) {}
// 新增数据(这里需要用到dto层进行逻辑处理)
create(createUser2Dto: CreateUser2Dto) {
return this.user2Repoitory.insert(createUser2Dto);
}
// 查询数据
findAll(current = 1, pagesize = 15) {
// 查询所有的逻辑代码()(访问localhost:3000/user2)
// return this.user2Repoitory.findAndCount();
// 分页查询的代码
return this.user2Repoitory.findAndCount({
skip: (current - 1) * pagesize, // 跳过多少页
take: pagesize, // 当前页数
});
}
// 根据id查询
findOne(id: number) {
// 根据id查询用户信息
// return this.user2Repoitory.findOneBy({ id });
// 也可以使用findone查询,这个查询毕竟灵活
return this.user2Repoitory.findOne({ where: { id } }); // 里面放查询条件
}
// 更新(修改数据)
update(id: number, updateUser2Dto: UpdateUser2Dto) {
// 根据id修改数据
return this.user2Repoitory.update(id,updateUser2Dto);
}
// 根据id删除用户
remove(id: number) {
return this.user2Repoitory.delete(id);
}
}
// 创建项目
nest new project-name
// (该过程有个选择包管理工具的,我选的yarn)
yarn run start:dev
// 可以在浏览器访问localhost:3000 输出helloWorld
// 安装依赖
yarn add --save @nestjs/typeorm typeorm mysql2
// @nestjs/typeorm这个本人运行上面命令一直安装不上,只好单独安装一下就成功了(yarn add @nestjs/typeorm)
nest g res user2
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserModule } from './user/user.module';
import { User2Module } from './user2/user2.module';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'admin',
database: 'shop',
autoLoadEntities: true,
synchronize: true,
}),
UserModule,
User2Module,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import { Entity, Column, PrimaryGeneratedColumn, OneToOne } from 'typeorm';
import { Idcard } from './idcard.entity';
@Entity()
export class User2 {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column()
age: number;
@Column({ default: true })
isActive: boolean;
// 需要第二个参数 , 外键是在idcard这个表的user2字段
// 在incard是通过JoinColumn()做的关联 , 下面这个第二个参数指定了外键名称(这样的话,如果外键存在则是删除不掉这个user2表的)
@OneToOne(()=>Idcard,(idcard)=>idcard.user2)
idCard:Idcard
}
import { User2 } from 'src/user2/entities/user2.entity';
import {
Column,
Entity,
JoinColumn,
OneToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
@Entity()
export class Idcard {
@PrimaryGeneratedColumn()
id: number;
@Column({ length: 50, comment: '身份证号' })
cardname: string;
// 指定一对一的关系
@JoinColumn() // 这里必须要连接下,否则外键加不上
@OneToOne(() => User2, { // 这里代表的是关联关系
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
cascade: true, // 关联关系保存
})
user2: User2; // 绑定的外键
}
import { Module } from '@nestjs/common';
import { User2Service } from './user2.service';
import { User2Controller } from './user2.controller';
// 引入实体
import { User2 } from './entities/user2.entity';
import { Idcard } from './entities/idcard.entity';
// 引入orm框架
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [TypeOrmModule.forFeature([User2,Idcard])],
controllers: [User2Controller],
providers: [User2Service],
})
export class User2Module {}
到这一步,运行项目会发现数据库多出来一张user2和idcard的表,表字段和entity.ts一一对应
接下来三个文件内都是逻辑代码
// 这里在添加数据用上了
export class CreateUser2Dto {
firstName: string;
lastName: string;
isActive: boolean;
}
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { User2Service } from './user2.service';
import { CreateUser2Dto } from './dto/create-user2.dto';
import { UpdateUser2Dto } from './dto/update-user2.dto';
@Controller('user2')
export class User2Controller {
constructor(private readonly user2Service: User2Service) {}
@Post()
create(@Body() createUser2Dto: CreateUser2Dto) {
return this.user2Service.create(createUser2Dto);
}
@Get()
findAll() {
return this.user2Service.findAll();
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.user2Service.findOne(+id);
}
@Patch(':id')
update(@Param('id') id: string, @Body() updateUser2Dto: UpdateUser2Dto) {
return this.user2Service.update(+id, updateUser2Dto);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.user2Service.remove(+id);
}
}
import { Injectable } from '@nestjs/common';
import { CreateUser2Dto } from './dto/create-user2.dto';
import { UpdateUser2Dto } from './dto/update-user2.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User2 } from './entities/user2.entity';
import { Idcard } from './entities/idcard.entity';
@Injectable()
export class User2Service {
constructor(
@InjectRepository(User2) private user2: Repository<User2>,
@InjectRepository(Idcard) private idcard: Repository<Idcard>,
) {}
// 插入数据
create(createUser2Dto: CreateUser2Dto) {
const user2 = new User2();
user2.firstName = '我是';
user2.lastName = '扎代';
user2.age = 20;
// 接下来是第二张表要添加的数据
const idcard = new Idcard();
idcard.cardname = '123456';
idcard.user2 = user2; // 进行绑定关联关系
// 最后进行保存,这里只要Idcard配置了cascade:true就可以自动进行关联关系的保存,即这里只需要保存idcard就可以自动保存user了,不用保存两次
return this.idcard.save(idcard);
}
findAll() {
return `This action returns a user2`;
}
// 关联查询
findOne(id: number) {
return this.user2.find({
relations: ['idCard'], // 这个是关联表的外表列名(当查找到对应的数据时,user2也会打印出关联表信息),如果不写的话,关联表信息不会被查出来
where: {
id: id,
},
});
}
// 修改数据
update(id: number, updateUser2Dto: UpdateUser2Dto) {
const user2 = new User2();
user2.id = 1; // 这里需要指定一下id(因为id是自增的,不指定一下id会相当于是添加数据)
user2.firstName = '我是';
user2.lastName = '扎代';
user2.age = 20;
// 接下来是第二张表要添加的数据
const idcard = new Idcard();
idcard.id = 1; // 这里需要指定一下id(因为id是自增的,不指定一下id会相当于是添加数据)
idcard.cardname = '123456';
idcard.user2 = user2; // 进行绑定关联关系
// 最后进行保存,这里只要Idcard配置了cascade:true就可以自动进行关联关系的保存,即这里只需要保存idcard就可以自动保存user了,不用保存两次
return this.idcard.save(idcard);
}
// 删除数据
async remove(id: number) {
let res = await this.idcard.delete(1);
let res2 = await this.user2.delete(1);
if (res.affected > 0 && res2.affected > 0) {
return { success: '删除成功' };
} else {
return { error: '删除失败' };
}
}
}
多个员工只有一个部门,但是一个部门可以有很多员工,这样就对应了一对多的关系
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
import { Department } from './Department.eneity';
// 员工表
@Entity()
export class Employee {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
// 多的一方(员工),很多个员工对应一个部门
// 为什么不需要JoinColumn()这个装饰器,因为这种多对一的关系,外键是知道的(外键在多的一方)
@ManyToOne(() => Department, {
cascade: true,
})
department: Department;
}
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import { Employee } from './Employee.eneity';
// 部门表
@Entity()
export class Department {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
// 一个部门有很多个员工,第二个参数代表外键在哪里维护
@OneToMany(() => Employee, (emp) => emp.department)
employee: Employee[];
}
import { Module } from '@nestjs/common';
import { User2Service } from './user2.service';
import { User2Controller } from './user2.controller';
// 引入实体
import { User2 } from './entities/user2.entity';
import { Idcard } from './entities/idcard.entity';
import { Employee } from './entities/Employee.eneity';
import { Department } from './entities/Department.eneity';
// 引入orm框架
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [TypeOrmModule.forFeature([User2,Idcard,Employee,Department])],
controllers: [User2Controller],
providers: [User2Service],
})
export class User2Module {}
此时启动项目会创建两个新表,可以查看外键看关系,一对多和多对一这种关系外键都在多的一方
下面就只需要修改/src/user2/user2.servics.ts进行增删改查就行了
import { Injectable } from '@nestjs/common';
import { CreateUser2Dto } from './dto/create-user2.dto';
import { UpdateUser2Dto } from './dto/update-user2.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User2 } from './entities/user2.entity';
import { Idcard } from './entities/idcard.entity';
import { Department } from './entities/Department.eneity';
import { Employee } from './entities/Employee.eneity';
@Injectable()
export class User2Service {
constructor(
@InjectRepository(User2) private user2: Repository<User2>,
@InjectRepository(Idcard) private idcard: Repository<Idcard>,
@InjectRepository(Department) private department: Repository<Department>,
@InjectRepository(Employee) private employee: Repository<Employee>,
) {}
// 插入数据
create(createUser2Dto: CreateUser2Dto) {
// 这是一对多的写法(会打印出多的一方每个人的数据)
// const d1 = new Department();
// d1.name = '技术部';
// const e1 = new Employee();
// e1.name = '张三';
// e1.department = d1; // 关联关系绑定
// const e2 = new Employee();
// e2.name = '李四';
// e2.department = d1; // 关联关系绑定
// const e3 = new Employee();
// e3.name = '王五';
// e3.department = d1; // 关联关系绑定
// 最后进行保存,这里只要Idcard配置了cascade:true就可以自动进行关联关系的保存,即这里只需要保存idcard就可以自动保存user了,不用保存两次
// return this.employee.save([e1, e2, e3]);
// 这是多对一的写法,打印出的也是最外层是多的一方,内部是每一个员工对象
const e1 = new Employee();
e1.name = '张三';
const e2 = new Employee();
e2.name = '李四';
const e3 = new Employee();
e3.name = '王五';
const d1 = new Department();
d1.name = '技术部';
d1.employee = [e1, e2, e3];
return this.department.save([e1, e2, e3]);
}
findAll() {
return `This action returns a user2`;
}
// 关联查询
findOne(id: number) {
console.log('id', id);
return this.department.find({
// department查询的表名
relations: ['employee'], // 这个是关联表的外表列名(当查找到对应的数据时,user2也会打印出关联表信息)
where: {
id: id,
},
});
}
// 修改数据
update(id: number, updateUser2Dto: UpdateUser2Dto) {
// 这里和一对一是一致的,只需要修改id即可
}
// 删除数据
async remove(id: number) {
// 删除的话,需要将关联的employee删了,再删除department,找到当前id项
const department = await this.department.findOne({
relations: ['employee'],
where: { id },
});
// 如果找到这个数据了,把他关联的员工找到
if (department) {
const employeesToDelete = department.employee;
// 先清空员工
await this.employee.remove(employeesToDelete);
// 再删除数据
await this.department.delete(id);
return 1;
} else {
// Handle the case when the department with the given ID is not found
return 0;
}
}
}
文章和文章标签存在多对多关系
import {
Entity,
PrimaryGeneratedColumn,
Column,
ManyToMany,
JoinTable,
} from 'typeorm';
import { Tag } from './Tag.entity';
@Entity()
export class Article {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
content: string;
// 建立多对多的关系,需要个中间表
@JoinTable() // 指定中间表,(这个是自动创建的,运行项目就等创建完数据库就会发现的,中间表绑定的是另外两张表的id)
// 多对多关系的所有表都要指定第二个参数,找到外键位置
@ManyToMany(() => Tag, (a) => a.articles, {
cascade: true,
onDelete: 'CASCADE', // 设置级联删除,这样无论这样只要任意一方被删除,与之相关的数据都会同步删除
onUpdate: 'CASCADE',
})
tag: Tag[];
}
import { Entity, PrimaryGeneratedColumn, Column, ManyToMany } from 'typeorm';
import { Article } from './Article.entity';
@Entity()
export class Tag {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
// 删除这里,比如我想删除一篇文章,也会删除标签tag,但是万一别的文章也有tag引用怎么解决???
// 那就只能给标签加个多对多属性,指定外键列在哪里
// // 多对多关系的所有表都要指定第二个参数,找到外键位置
@ManyToMany(() => Article, (a) => a.tag) // 指定外键列位置
articles: Article[];
}
import { Module } from '@nestjs/common';
import { User2Service } from './user2.service';
import { User2Controller } from './user2.controller';
// 引入实体
import { User2 } from './entities/user2.entity';
import { Idcard } from './entities/idcard.entity';
import { Employee } from './entities/Employee.eneity';
import { Department } from './entities/Department.eneity';
import { Tag } from './entities/Tag.entity';
import { Article } from './entities/Article.entity';
// 引入orm框架
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
TypeOrmModule.forFeature([
User2,
Idcard,
Employee,
Department,
Article,
Tag,
]),
],
controllers: [User2Controller],
providers: [User2Service],
})
export class User2Module {}
/src/user2/user2.service.ts
import { Injectable } from '@nestjs/common';
import { CreateUser2Dto } from './dto/create-user2.dto';
import { UpdateUser2Dto } from './dto/update-user2.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User2 } from './entities/user2.entity';
import { Idcard } from './entities/idcard.entity';
import { Department } from './entities/Department.eneity';
import { Employee } from './entities/Employee.eneity';
import { Article } from './entities/Article.entity';
import { Tag } from './entities/Tag.entity';
@Injectable()
export class User2Service {
constructor(
@InjectRepository(User2) private user2: Repository<User2>,
@InjectRepository(Idcard) private idcard: Repository<Idcard>,
@InjectRepository(Department) private department: Repository<Department>,
@InjectRepository(Employee) private employee: Repository<Employee>,
@InjectRepository(Article) private article: Repository<Article>,
@InjectRepository(Tag) private tag: Repository<Tag>,
) {}
// 插入数据
create(createUser2Dto: CreateUser2Dto) {
// 多对多
const a1 = new Article();
a1.title = 'aaa';
a1.content = 'aaaaaa';
const a2 = new Article();
a2.title = 'bbb';
a2.content = 'bbbbbb';
const t1 = new Tag();
t1.name = '111';
const t2 = new Tag();
t2.name = '222';
const t3 = new Tag();
t3.name = '333';
// 绑定关系
a1.tag = [t1, t2];
a2.tag = [t1, t2, t3];
return this.article.save([a1, a2]); //因为在实体类设置了自动保存,因此这个文章保存了标签是自动关联保存的
// this.tag.save([t1, t2, t3]);
}
findAll() {
return `This action returns a user2`;
}
// 关联查询
findOne(id: number) {
return this.article.find({
// department查询的表名
relations: ['tag'], // 这个是关联表的外表列名(当查找到对应的数据时,user2也会打印出关联表信息)
// where: {
// id: id,
// },
});
}
// 修改数据
async update(id: number, updateUser2Dto: UpdateUser2Dto) {
const a1 = await this.article.findOne({
relations: ['tag'],
where: {
id: id,
},
});
console.log('a1', a1);
a1.title = 'ccccc';
a1.tag = a1.tag.filter((item) => item.name.includes('ttt111'));
// 修改完以后再保存
return this.article.save(a1);
}
// 删除数据
async remove(id: number) {
// 由于article外键设置了级联删除,因此需要先删除article才能删除对应的tag
let res = await this.article.delete(id);
let res2 = await this.tag.delete(id);
if (res.affected > 0 && res2.affected > 0) {
return {
success: '删除成功',
};
}
return {
error: '删除失败',
};
}
}
本次笔记参考此视频学习的