安装

yarn add egg-mysql -s

配置

plugin.js

  1. exports.mysql = {
  2. enable: true,
  3. package: 'egg-mysql',
  4. };

config.default.js

  1. config.mysql = {
  2. app: true, // 是否将mysql挂载到app下
  3. agent: false, // 是否挂载在代理下面
  4. client: {
  5. host: '127.0.0.1',
  6. port: '3306',
  7. user: 'root',
  8. password: '0000mysql',
  9. database: 'egg',
  10. },
  11. };

使用

问题

遇到的问题:
报错 nodejs.AppWorkerDiedError 怎么解决?


增删改查

service/user.js

  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class UserService extends Service {
  4. async lists() {
  5. try {
  6. const { app } = this;
  7. // const res = await app.mysql.query(
  8. // 'SELECT * FROM user',
  9. // ''
  10. // );
  11. //查 user 表中全部
  12. const res = await app.mysql.select('user');
  13. // console.log('SELECT', res);
  14. return res;
  15. } catch (error) {
  16. console.log('service/user.js/lists', error);
  17. return null;
  18. }
  19. }
  20. async detail(id) {
  21. return {
  22. id,
  23. name: 'zhou',
  24. age: 20,
  25. };
  26. }
  27. async detail2(id) {
  28. try {
  29. const { app } = this;
  30. // 查user 表中 的id
  31. const res = await app.mysql.get('user', { id });
  32. return res;
  33. } catch (error) {
  34. console.log('service/user.js/lists', error);
  35. return null;
  36. }
  37. }
  38. async add(params) {
  39. try {
  40. const { app } = this;
  41. // 接收参数,给user 表中添加 数据
  42. const res = await app.mysql.insert('user', params);
  43. return res;
  44. } catch (error) {
  45. console.log('service/user.js/lists', error);
  46. return null;
  47. }
  48. }
  49. async edit(params) {
  50. try {
  51. const { app } = this;
  52. // 接收参数,修改user 表中一对属性
  53. const res = await app.mysql.update('user', params);
  54. return res;
  55. } catch (error) {
  56. console.log('service/user.js/lists', error);
  57. return null;
  58. }
  59. }
  60. async delete(id) {
  61. try {
  62. const { app } = this;
  63. // 删除
  64. const res = await app.mysql.delete('user', { id });
  65. return res;
  66. } catch (error) {
  67. console.log('service/user.js/lists', error);
  68. return null;
  69. }
  70. }
  71. }
  72. module.exports = UserService;

controller 中调用
controller/user.js

  1. async lists() {
  2. // eslint-disable-next-line no-unused-vars
  3. const { ctx, app } = this;
  4. // console.log('app.mysql.query--------', app.mysql.query);
  5. // await new Promise(resolve => {
  6. // setTimeout(() => {
  7. // resolve();
  8. // }, 1500);
  9. // });
  10. const res = await ctx.service.user.lists();
  11. // console.log('res0----', res0);
  12. // const res = 'await ctx.service.user.lists';
  13. ctx.body = res;
  14. }
  15. async detail2() {
  16. const { ctx } = this;
  17. const res = await ctx.service.user.detail2(ctx.params.id);
  18. ctx.body = res;
  19. }
  20. async add() {
  21. const { ctx } = this;
  22. // const rule = {
  23. // name: { type: 'string' },
  24. // age: { type: 'number' },
  25. // };
  26. // ctx.validate(rule);
  27. const res = await ctx.service.user.add(ctx.request.body);
  28. // ctx.body = { status: 200, data: ctx.request.body };
  29. ctx.body = { status: 200, data: res };
  30. }
  31. async edit() {
  32. const { ctx } = this;
  33. const res = await ctx.service.user.edit(ctx.request.body);
  34. ctx.body = { status: 200, data: res };
  35. }
  36. async del() {
  37. const { ctx } = this;
  38. const res = ctx.service.user.delete(ctx.request.body.id);
  39. ctx.body = { status: 200, data: res };
  40. }

get 请求之外的操作测试:
添加
image.png
修改
image.png

image.png