typeorm 查询_TypeORM-查询操作
时间: 2024-01-05 17:02:23 浏览: 30
TypeORM 是一个基于 TypeScript 和 JavaScript 的 ORM 框架,可以与多种数据库进行交互。下面是一些 TypeORM 查询操作的示例:
1. 基本查询
```typescript
import {getRepository} from "typeorm";
import {User} from "./entity/User";
const userRepository = getRepository(User);
const users = await userRepository.find();
```
2. 条件查询
```typescript
const userRepository = getRepository(User);
const users = await userRepository.find({ where: { name: "John" } });
```
3. 模糊查询
```typescript
const userRepository = getRepository(User);
const users = await userRepository.createQueryBuilder("user")
.where("user.name LIKE :name", { name: "%John%" })
.getMany();
```
4. 分页查询
```typescript
const userRepository = getRepository(User);
const users = await userRepository.find({
skip: 0,
take: 10,
});
```
5. 联表查询
```typescript
const userRepository = getRepository(User);
const users = await userRepository.createQueryBuilder("user")
.leftJoinAndSelect("user.photos", "photo")
.getMany();
```
以上是一些常见的 TypeORM 查询操作,更多的查询方式可以参考 TypeORM 官方文档。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)