Swoft2.x 小白学习笔记 (二) --- mysql、redis
程序员文章站
2022-12-22 16:49:22
介绍swoft中 1、mysql、 2、Redis 一、mysql使用: 1、配置,在 app\bean.php文件中 2、生成Model,一个Model对应一张表。在 /App/Model/Entity/ 文件夹下新建文件
介绍swoft中
1、mysql、
2、redis
一、mysql使用:
1、配置,在 app\bean.php
文件中
'db' => [ 'class' => database::class, 'dsn' => 'mysql:dbname=webdemo;host=localhost', 'username' => 'root', 'password' => 'foto_cj1', ],
//链接池配置 'db2.pool' => [ 'class' => pool::class, 'database' => bean('db'), 'minactive' => 10, 'maxactive' => 20, 'maxwait' => 0, 'maxwaittime' => 0, 'maxidletime' => 60, ],
2、生成model,一个model对应一张表。在 /app/model/entity/ 文件夹下新建文件
<?php declare(strict_types=1); namespace app\model\entity; use swoft\db\annotation\mapping\column; use swoft\db\annotation\mapping\entity; use swoft\db\annotation\mapping\id; use swoft\db\eloquent\model; /** * * class demo * * @since 2.0 * * @entity(table="demo",pool="db2.pool") //定义model,参数是对应的表和连接池(选填) */ class demo extends model { /** *默认自动添加 created_at 和 updated_at,不需要时设置为false * @var bool */ public $modeltimestamps = false; /** * * @id(incrementing=false) * @column(name="id") //定义列 * * @var int */ private $id; /** * @column(name="name") * * @var string|null */ private $name; /** * @param int $id * * @return void */ public function setid(int $id): void { $this->id = $id; } /** * @param string|null $name * * @return void */ public function setname(?string $name): void { $this->name = $name; } /** * @return int */ public function getid(): ?int { return $this->id; } /** * @return string|null */ public function getname(): ?string { return $this->name; } }
3、使用(model)
//查询 $user = demo::find($)->toarray(); //修改 $sdbuser = demo::find($id); $sdbuser->setname("cjcjcjccj"); //或者 $sdbuser->update(['name' => "dddddd"]); //删除 $result = demo::where('id', 1)->delete(); $user = demo::find($data["uid"]); $result = $user->delete(); //插入 $count = new demo(); $count->setid(mt_rand(1, 100)); $count->setname('attr'); $result = $count->save(); $nid = $count->getid(); //批量插入 $insarr = [ [ 'id' => random_int(1, 100), 'name' => md5(uniqid()) ], [ 'id' => random_int(1, 100), 'name' => md5(uniqid()) ] ]; $result = demo::insert($insarr);
4、db原生使用:https://www.swoft.org/docs/2.x/zh-cn/db/builder.html
二:redis简单使用:
1、配置:在 app\bean.php
文件中
'redis' => [ 'class' => redisdb::class, 'host' => '127.0.0.1', 'port' => 6379, 'database' => 0, 'option' => [ 'prefix' => 'swoft:' ] ], 'redis.pool' => [ 'class' => \swoft\redis\pool::class, 'redisdb' => \bean('redis'), 'minactive' => 10, 'maxactive' => 20, 'maxwait' => 0, 'maxwaittime' => 0, 'maxidletime' => 40, ]
2、使用
(1)、直接使用
redis::set($key, $setdata,$time); redis::get($key);
(2)、通过 @inject注入连接池方式使用
在/app/http/controller/中新建文件
/** * class rediscontroller * * @since 2.0 * @controller("redis") */ class rediscontroller { /** * @inject("redis.pool") * * @var pool */ private $redis; /** * @return array * @requestmapping("find") //访问路由: /redis/find * @throws \throwable */ public function find() { $us = $this->redis->get('user'); if($us) $this->redis->set('user', ["name" => "gimi", "age" => "18"],120); return $us; } }
查看文档:
https://www.swoft.org/docs/2.x/zh-cn/db/index.html
https://www.swoft.org/docs/2.x/zh-cn/redis/index.html