介绍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" => ""],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  

最新文章

  1. C程序范例(2)——学生管理系统”链表“实现
  2. MySQL主从同步延迟
  3. gulp详细入门教程-gulp demo download
  4. php ffmpeg
  5. CentOS7 安装MySQL
  6. 经验分享:Xcode 创建.a和framework静态库【转】
  7. Gnome创建桌面快捷方式
  8. array,vertor,arraylist,hashable,hashmap等几个易混淆概念的区别
  9. 2016年10月21日 星期五 --出埃及记 Exodus 19:5
  10. jsp的一些基本语法
  11. 试着开发chrome插件
  12. cf C Milking cows
  13. Swift - 43 - 继承, 多态, 析构函数
  14. asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料
  15. logify与theos的二三事
  16. http中的get和post(二)
  17. git应用套路
  18. User-Agent-Switcher和fiddler
  19. 手机Web 开发中图片img 如何等比例缩放
  20. sed用法说明

热门文章

  1. P1986 元旦晚会——贪心或差分约束系统
  2. 4)抽象方法不能为private,final或者static,为什么?
  3. Vue中的动画效果
  4. 4.linux vim 编辑器的基本操作
  5. 记录一次腾讯X5内核64位手机初始化失败
  6. Debian/Ubuntu/CentOS开机启动
  7. IIS/VS IIS Express 添加MIME映射 svg、woff、woff2、json
  8. pytest 基本用法
  9. python socketpool:通用连接池
  10. python笔记9 线程进程 threading多线程模块 GIL锁 multiprocessing多进程模块 同步锁Lock 队列queue IO模型