typeorm 关联查询

关联查询

Article 实体类

// 文件位置: /src/entities/article.entity.ts
import { Column, Entity, PrimaryColumn } from "typeorm";

@Entity('article')
export class Article {
  @PrimaryColumn()
  id: string;

  @Column()
  title: string;

  @Column()
  link: string;

  @Column()
  fileId: string;

  @Column('text')
  content: string;

  @Column()
  categoryId: string;

  @Column()
  formatId: number;

  @Column()
  originId: number;

  @Column()
  createBy: string;
}

User 实体类

// 文件位置: /src/entities/user.entity.ts
import { Column, Entity, PrimaryColumn } from "typeorm";

@Entity('user')
export class User {
  @PrimaryColumn()
  id: string;

  @Column()
  nickname: string;

  @Column()
  username: string;

  @Column()
  password: string;

  @Column()
  avator: string;

  @Column()
  email: string;
}

表关联关系: article.createBy === user.id

ArticleService 服务代码

import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Article } from "src/entities/article.entity";
import { User } from "src/entities/user.entity";
import { Repository } from "typeorm";

@Injectable()
export class ArticleService {
  @InjectRepository(Article)
  private readonly articleRespository: Repository
; getAllUsers () { return this.articleRespository.createQueryBuilder() .leftJoinAndSelect(User, 'user', 'user.id = article.createBy') // .select(` // article.id as id, // article.title as title, // article.content as content, // user.id as userId, // user.nickname as nickname, // user.username as usernmae, // user.avator as avator // `) .getRawMany(); } } // 对应生成的sql: /* SELECT `Article`.`id` AS `Article_id`, `Article`.`title` AS `Article_title`, `Article`.`link` AS `Article_link`, `Article`.`fileId` AS `Article_fileId`, `Article`.`content` AS `Article_content`, `Article`.`categoryId` AS `Article_categoryId`, `Article`.`formatId` AS `Article_formatId`, `Article`.`originId` AS `Article_originId`, `Article`.`createBy` AS `Article_createBy`, `user`.`id` AS `user_id`, `user`.`nickname` AS `user_nickname`, `user`.`username` AS `user_username`, `user`.`password` AS `user_password`, `user`.`avator` AS `user_avator`, `user`.`email` AS `user_email` FROM `article` `Article` LEFT JOIN `user` `user` ON `user`.`id` = article.createBy */ // 返回结果: [ { "Article_id": "0fcb8310-9c4a-11ea-9427-017d0539b705", "Article_title": "fe'f", "Article_link": null, "Article_fileId": null, "Article_content": "

微任务

", "Article_categoryId": "4b958080-9a33-11ea-9abc-1d8c64f552b3", "Article_formatId": 2, "Article_originId": 1, "Article_createBy": "16ffe4f0-98d0-11ea-adcb-cd4aa44d4464", "user_id": "16ffe4f0-98d0-11ea-adcb-cd4aa44d4464", "user_nickname": "李云龙", "user_username": "wanghailong", "user_password": "1bbd886460827015e5d605ed44252251", "user_avator": "http://192.168.1.101:8765/avator/f360e610-9d80-11ea-9008-019523360f5b.jpg", "user_email": "[email protected]" } ] // 放开上面注掉的 "select" 代码 // 对应生成的sql: /* SELECT article.id as id, article.title as title, article.content as content, `user`.`id` as userId, `user`.`nickname` as nickname, `user`.`username` as usernmae, `user`.`avator` as avator FROM `article` `Article` LEFT JOIN `user` `user` ON `user`.`id` = article.createBy */ // 进行查询,可以得出如下的查询结果: [ { "id": "0fcb8310-9c4a-11ea-9427-017d0539b705", "title": "fe'f", "content": "

微任务

", "userId": "16ffe4f0-98d0-11ea-adcb-cd4aa44d4464", "nickname": "李云龙", "usernmae": "wanghailong", "avator": "http://192.168.1.101:8765/avator/f360e610-9d80-11ea-9008-019523360f5b.jpg" } ]

结论:

  1. typeorm 有一套默认生成查询列的方式,其生成出来的查询列的格式是:"表名_列名"
  2. typeorm 生成的列是根据实体类中定义的字段进行生成的,也就是实体类中有多少个字段就生成多少个查询列
  3. 默认的生成列的方式可以通过 .select() 方法进行重置,并且同时根据自定义的查询列只返回我们想要的列,同时还能重命名列

相关方法说明

  1. createQueryBuilder() 方法的调用可以传递一个实体类名称,如果是通过 Respository 的子类调用的,那么就可以不传递实体类名称
  2. leftJoinAndSelect() 方法有多个重载方法,第一个参数可以是一个实体类名称,还可以是一个实体类型
  3. getRawMany() 方法根据生成器的生成的 sql 进行查询并返回查询结果

你可能感兴趣的:(typeorm 关联查询)