经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
Swoft2.x 小白学习笔记 (二) --- mysql、redis
来源:cnblogs  作者:Foto_CShow  时间:2019/9/30 9:10:29  对本文有异议

介绍swoft中

  1、mysql、

  2、Redis

 

一、mysql使用:

  1、配置,在 app\bean.php文件中

  1. 'db' => [
  2. 'class' => Database::class,
  3. 'dsn' => 'mysql:dbname=webdemo;host=localhost',
  4. 'username' => 'root',
  5. 'password' => 'foto_cj1',
  6. ],
    //链接池配置
  7. 'db2.pool' => [
  8. 'class' => Pool::class,
  9. 'database' => bean('db'),
  10. 'minActive' => 10,
  11. 'maxActive' => 20,
  12. 'maxWait' => 0,
  13. 'maxWaitTime' => 0,
  14. 'maxIdleTime' => 60,
  15. ],

  2、生成Model,一个Model对应一张表。在 /App/Model/Entity/ 文件夹下新建文件

  1. <?php declare(strict_types=1);
  2. namespace App\Model\Entity;
  3. use Swoft\Db\Annotation\Mapping\Column;
  4. use Swoft\Db\Annotation\Mapping\Entity;
  5. use Swoft\Db\Annotation\Mapping\Id;
  6. use Swoft\Db\Eloquent\Model;
  7. /**
  8. *
  9. * Class Demo
  10. *
  11. * @since 2.0
  12. *
  13. * @Entity(table="demo",pool="db2.pool") //定义Model,参数是对应的表和连接池(选填)
  14. */
  15. class Demo extends Model
  16. {
  17. /**
  18. *默认自动添加 created_at 和 updated_at,不需要时设置为false
  19. * @var bool
  20. */
  21. public $modelTimestamps = false;
  22. /**
  23. *
  24. * @Id(incrementing=false)
  25. * @Column(name="id") //定义列
  26. *
  27. * @var int
  28. */
  29. private $id;
  30. /**
  31. * @Column(name="name")
  32. *
  33. * @var string|null
  34. */
  35. private $name;
  36. /**
  37. * @param int $id
  38. *
  39. * @return void
  40. */
  41. public function setId(int $id): void
  42. {
  43. $this->id = $id;
  44. }
  45. /**
  46. * @param string|null $name
  47. *
  48. * @return void
  49. */
  50. public function setName(?string $name): void
  51. {
  52. $this->name = $name;
  53. }
  54. /**
  55. * @return int
  56. */
  57. public function getId(): ?int
  58. {
  59. return $this->id;
  60. }
  61. /**
  62. * @return string|null
  63. */
  64. public function getName(): ?string
  65. {
  66. return $this->name;
  67. }
  68. }
View Code

 

  3、使用(Model)    

  1. //查询
  2. $user = Demo::find($)->toArray();
  3. //修改
  4. $sdbuser = Demo::find($id);
  5. $sdbuser->setName("cjcjcjccj");
  6. //或者
  7. $sdbuser->update(['name' => "dddddd"]);
  8. //删除
  9. $result = Demo::where('id', 1)->delete();
  10. $user = Demo::find($data["uid"]);
  11. $result = $user->delete();
  12. //插入
  13. $count = new Demo();
  14. $count->setId(mt_rand(1, 100));
  15. $count->setName('attr');
  16. $result = $count->save();
  17. $nId = $count->getId();
  18. //批量插入
  19. $insArr = [
  20. [
  21. 'id' => random_int(1, 100),
  22. 'name' => md5(uniqid())
  23. ],
  24. [
  25. 'id' => random_int(1, 100),
  26. 'name' => md5(uniqid())
  27. ]
  28. ];
  29. $result = Demo::insert($insArr);
View Code

   4、DB原生使用:https://www.swoft.org/docs/2.x/zh-CN/db/builder.html

 

二:Redis简单使用:

1、配置:在 app\bean.php文件中

  1. 'redis' => [
  2. 'class' => RedisDb::class,
  3. 'host' => '127.0.0.1',
  4. 'port' => 6379,
  5. 'database' => 0,
  6. 'option' => [
  7. 'prefix' => 'swoft:'
  8. ]
  9. ],
  10. 'redis.pool' => [
  11. 'class' => \Swoft\Redis\Pool::class,
  12. 'redisDb' => \bean('redis'),
  13. 'minActive' => 10,
  14. 'maxActive' => 20,
  15. 'maxWait' => 0,
  16. 'maxWaitTime' => 0,
  17. 'maxIdleTime' => 40,
  18. ]
View Code

2、使用

  (1)、直接使用

  1. Redis::set($key, $setData$time);
  2. Redis::get($key);

      (2)、通过 @Inject注入连接池方式使用

    在/App/Http/Controller/中新建文件 

  1. /**
  2. * Class RedisController
  3. *
  4. * @since 2.0
  5. * @Controller("redis")
  6. */
  7. class RedisController
  8. {
  9. /**
  10. * @Inject("redis.pool")
  11. *
  12. * @var Pool
  13. */
  14. private $redis;
  15. /**
  16. * @return array
  17. * @RequestMapping("find") //访问路由: /redis/find
  18. * @throws \Throwable
  19. */
  20. public function find()
  21. {
  22. $us = $this->redis->get('user');
  23. if($us)
  24. $this->redis->set('user', ["name" => "gimi", "age" => "18"],120);
  25. return $us;
  26. }
  27. }

 

查看文档: 

    https://www.swoft.org/docs/2.x/zh-CN/db/index.html

    https://www.swoft.org/docs/2.x/zh-CN/redis/index.html  

    

 

原文链接:http://www.cnblogs.com/cj8988/p/11607555.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号