首页 > 作文

Swoft2.x 小白学习笔记 (二) — mysql、redis

更新时间:2023-04-07 17:30:37 阅读: 评论:0

介绍swoft中

  1、mysql、

  2、redis

一、mysql使用:

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

   'db' => [        'class'    => databa::class,        'dsn'      => 'mysql:dbname=webdemo;host=localhost',        'urname' => 'root',        'password' => 'foto_cj1',    ],
//链接池配置 'db2.pool' => [ 'class' => pool::class, 'databa' => 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;u swoft\db\annotation\mapping\column;u swoft\db\annotation\mapping\entity;u swoft\db\annotation\mapping\id;u swoft\db\eloquent\model;/** * * class demo * * @since 2.0 * * @entity(table="demo",pool="db2.pool")  //定义model,参数是对应的表和连接池(选填) */class demo extends model{    /**     *默认自动添加 created_at 和 updated_at,不需要时设置为fal     * @var bool     */    public $modeltimestamps = fal;    /**     *     * @id(incrementing=fal)     * @column(name="id")    //定义列     *     * @var int     */    private $id;    /**     * @column(name="name")     *     * @var string|null     */    private $name;    /**     * @param int $id     *     * @return void     */    public 必修一英语function tid(int $id): void    {        $this->id = $id;    }  贺新春  /**     * @param string|null $name     *     * @return void     */    public function tname(?string $name): void    {        $this->name = $name;    }    /**     * @return int     */    public function getid(): ?int    {        return $this->id;    }    /**     * @return string|null     */    public function getname(): ?str血手幽灵ing    {        return $this->name;    }}

 3、使用(model)

//查询    $ur = demo::find($)->toarray();//修改    $sdbur = demo::find($id);    $sdbur->tname("cjcjcjccj");//或者    $sdbur->update(['name' => "dddddd"]);  //删除        $result = demo::where('id', 1)->delete();        $ur = demo::find($data["uid"]);        $result = $ur报到证补办流程->delete();//插入        $count = new demo();        $count->tid(mt_rand(1, 100));        $count->tname('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::inrt($insarr);

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

纪念抗战70周年阅兵

二:redis简单使用:

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

'redis'             => [        'class'    => redisdb::class,        'host'     => '127.0.0.1',        'port'     => 6379,        'databa' => 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::t($key, $tdata,$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('ur');        if($us)            $this->redis->t('ur', ["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  

本文发布于:2023-04-07 17:30:32,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/69f4e012c4bf380deeb4c63425f517bc.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Swoft2.x 小白学习笔记 (二) — mysql、redis.doc

本文 PDF 下载地址:Swoft2.x 小白学习笔记 (二) — mysql、redis.pdf

标签:文件   定义   连接池   不需要
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图