文档

下插件

λ yarn add @nestjs/typeorm typeorm mysql

创建 cats模块, 控制器,service

λ nest g mo cats

λ nest g co cats

λ nest g s cats cats/

链接mysql 链接出现下面的错误

Client does not support authentication protocol requested by server; consider u pgrading MySQL client 如果出现这个错误 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的数据库密码';

// app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm'; #
import { Connection } from 'typeorm'; #
import { CatsModule } from './cats/cats.module'; @Module({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'mysql登录密码',
database: 'nest',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}), #
CatsModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {
constructor(private readonly connection: Connection) {} #
}

编辑 entity

// cats.entity.ts

import { PrimaryGeneratedColumn, Column, Entity } from "typeorm";

@Entity()
export class Cats {
@PrimaryGeneratedColumn()
id: number; @Column({length: 45})
name: string; @Column('int')
age: number;
}

在cats.module.ts 中使用

import { TypeOrmModule } from '@nestjs/typeorm';
import { Cats } from './cats.entity';
@Module({
imports: [TypeOrmModule.forFeature([Cats])],
})
export class CatsModule {}

注入 cats.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Cats } from './cats.entity';
import { Repository } from 'typeorm'; @Injectable()
export class CatsService {
constructor( @InjectRepository(Cats)
private readonly catsRepository: Repository<Cats> ) { } find(){
return this.catsRepository.find()
} getCat(name:string): Promise<Cats[]> {
return this.catsRepository.find({ name });
}
}

Nestjs 链接mysql的更多相关文章

  1. PDO链接mysql学习笔记

    <?php //PDO链接mysql//dsn三种写法: //dsn01 $dsn = 'mysql:host=localhost;dbname=mysql'; //$dsn = 'mysql: ...

  2. Mssql链接mysql数据库

    最近在做mysql数据库实时同步到mssql数据库的方案,花了一周时间,测试通过了,在实际机器上测试出现了若干问题.第一个问题就是在mssql上链接mysql的问题. 第一步,安装 Mysql ODB ...

  3. 如何用DOS 链接mysql

    1.Ctrl+R 打开DOS窗口 2.键入 cd\ 回车进入C盘根目录 3.进入mysql bin目录下 操作mysql命令 4.输入连接数据库命令 mysql -hlocalhost -uroot ...

  4. SQL Server链接MySQL实践

    最近在访问多数据库的时候进行了SQLServer链接MySQL数据的实践,现总结如下: 一.  安装mysql-connector-odbc驱动: 1. 在SQL Server服务器的机器上安装mys ...

  5. Python学习笔记9-Python 链接MySql数据库

    Python 链接MySql数据库,方法很简单: 首先需要先 安装一个MySql链接插件:MySQL-python-1.2.3.win-amd64-py2.7.exe 下载地址:http://dev. ...

  6. Tomcat通过JNDI方式链接MySql数据库

    原文:Tomcat通过JNDI方式链接MySql数据库 拷贝MySQL的JDBC驱动到Tomcat的lib路径下 配置全局数据源或者单个Web应用的局部数据源 局部数据源 在Tomcat的conf/C ...

  7. visual studio 2012 链接Mysql 5.1

    首先在nuGet 下载MySql.Data.Entity 安装 mysql for visual studio http://www.mysql.com/why-mysql/windows/visua ...

  8. C# 链接MySql数据库

    C# 链接MySql数据库只得注意的几点: 1.C#链接MySql数据库要在网上下载一个mysql-connector-net-6.0.4-noinstall.rar  这里面放的都是一堆dll .将 ...

  9. python链接mysql

    1.安装MySQLdb MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的. 下载地址: ht ...

随机推荐

  1. Outlook Error: The Delegates settings were not saved correctly. Cannot activate send-on-behalf-of list.

    When user want to set Delegates for a user of a mailbox, the user meet below error. Step 1: Step 2: ...

  2. 浪院长 | spark streaming的使用心得

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/rlnLo2pNEfx9c/article/details/82505159 今天.主要想聊聊spar ...

  3. zookeeper三节点集群安装记录

    以下是3个节点的zk安装记录. 下载 下载地址: http://mirrors.shu.edu.cn/apache/zookeeper/ cd /data/opt/zk wget http://mir ...

  4. SpringBoot2.0针对请求参数@RequestBody验证统一拦截

    title: "SpringBoot2.0针对请求参数@RequestBody验证的统一拦截"categories: SpringBoot2.0 Shirotags: Spring ...

  5. 如何卸载EXCEL中的插件?

    1.问题:每一次启动,excel都会弹出一个提示框,是因为将treeplan给删除了.找不到. 2.解决办法: 1)在加载项界面,点击转到.就进入自己加载的工具界面 2)将相应项前面的勾取消--但某插 ...

  6. 使用 Nginx 为 Linux 实例绑定多个域名

    KB: 41467 · 更新时间:2018-11-16 20:26:51     Nginx 是一款广泛应用的 Web 服务器,常用于反向代理.负载均衡器以及 HTTP 缓存等.本文以 CentOS ...

  7. [转]Redis内部数据结构详解-sds

    本文是<Redis内部数据结构详解>系列的第二篇,讲述Redis中使用最多的一个基础数据结构:sds. 不管在哪门编程语言当中,字符串都几乎是使用最多的数据结构.sds正是在Redis中被 ...

  8. SQL2008R2的 遍历所有表更新统计信息 和 索引重建

    [2.以下是更新统计信息] DECLARE UpdateStatisticsTables CURSOR READ_ONLY FOR SELECT sst.name, Schema_name(sst.s ...

  9. 【翻译】Nginx的反向代理

    本文为翻译文,原文地址:https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ 本文描述代理服务器的基本配置.你能学到如何 ...

  10. Mac笔记本中是用Idea开发工具在Java项目中调用python脚本遇到的环境变量问题解决

    问题描述: mac笔记本本身会自带几个python版本,比如python2.7版本,我没有改动mac默认的python版本,只是安装了python3.7版本. 使用Pycharm开发Python项目没 ...