小白兔晒黑了

导航

 

视频地址 https://www.bilibili.com/video/BV14E411t7T4?p=32&spm_id_from=pageDriver&vd_source=4a69745b599dffec877b0fcfe130b092

1 原理

使用注解来完成ORM整合

将Core\init\MyDB的实例注入BeanFactory,

Core\init\MyDB实例里有一个LvDB的实例,装载了ORM

已经装载对象

self::$container->get('Core\init\MyDB') 

-----Core\init\MyDB Object
(
    [lvDB] => Illuminate\Database\Capsule\Manager Object
        (
            [manager:protected] => Illuminate\Database\DatabaseManager Object
                (
                    [app:protected] => Illuminate\Container\Container Object
                        (
                            [resolved:protected] => Array
                                (
                                )

                            [bindings:protected] => Array
                                (
                                )

                            [methodBindings:protected] => Array
                                (
                                )

                            [instances:protected] => Array
                                (
                                    [config] => Illuminate\Support\Fluent Object
                                        (
                                            [attributes:protected] => Array
                                                (
                                                    [database.fetch] => 5
                                                    [database.default] => default
                                                    [database.connections] => Array
                                                        (
                                                            [default] => Array
                                                                (
                                                                    [driver] => mysql
                                                                    [host] => 10.10.10.234
                                                                    [port] => 12345
                                                                    [database] => seckill
                                                                    [username] => root
                                                                    [password] => 123456
                                                                    [charset] => utf8mb4
                                                                    [collation] => utf8mb4_general_ci
                                                                    [prefix] => 
                                                                )

                                                            [test2] => Array
                                                                (
                                                                    [driver] => mysql
                                                                    [host] => localhost
                                                                    [database] => test2
                                                                    [username] => root
                                                                    [password] => 500500
                                                                    [charset] => utf8
                                                                    [collation] => utf8_unicode_ci
                                                                    [prefix] => 
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [scopedInstances:protected] => Array
                                (
                                )

                            [aliases:protected] => Array
                                (
                                )

                            [abstractAliases:protected] => Array
                                (
                                )

                            [extenders:protected] => Array
                                (
                                )

                            [tags:protected] => Array
                                (
                                )

                            [buildStack:protected] => Array
                                (
                                )

                            [with:protected] => Array
                                (
                                )

                            [contextual] => Array
                                (
                                )

                            [reboundCallbacks:protected] => Array
                                (
                                )

                            [globalBeforeResolvingCallbacks:protected] => Array
                                (
                                )

                            [globalResolvingCallbacks:protected] => Array
                                (
                                )

                            [globalAfterResolvingCallbacks:protected] => Array
                                (
                                )

                            [beforeResolvingCallbacks:protected] => Array
                                (
                                )

                            [resolvingCallbacks:protected] => Array
                                (
                                )

                            [afterResolvingCallbacks:protected] => Array
                                (
                                )

                        )

                    [factory:protected] => Illuminate\Database\Connectors\ConnectionFactory Object
                        (
                            [container:protected] => Illuminate\Container\Container Object
                                (
                                    [resolved:protected] => Array
                                        (
                                        )

                                    [bindings:protected] => Array
                                        (
                                        )

                                    [methodBindings:protected] => Array
                                        (
                                        )

                                    [instances:protected] => Array
                                        (
                                            [config] => Illuminate\Support\Fluent Object
                                                (
                                                    [attributes:protected] => Array
                                                        (
                                                            [database.fetch] => 5
                                                            [database.default] => default
                                                            [database.connections] => Array
                                                                (
                                                                    [default] => Array
                                                                        (
                                                                            [driver] => mysql
                                                                            [host] => 10.10.10.234
                                                                            [port] => 12345
                                                                            [database] => seckill
                                                                            [username] => root
                                                                            [password] => 123456
                                                                            [charset] => utf8mb4
                                                                            [collation] => utf8mb4_general_ci
                                                                            [prefix] => 
                                                                        )

                                                                    [test2] => Array
                                                                        (
                                                                            [driver] => mysql
                                                                            [host] => localhost
                                                                            [database] => test2
                                                                            [username] => root
                                                                            [password] => 500500
                                                                            [charset] => utf8
                                                                            [collation] => utf8_unicode_ci
                                                                            [prefix] => 
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                    [scopedInstances:protected] => Array
                                        (
                                        )

                                    [aliases:protected] => Array
                                        (
                                        )

                                    [abstractAliases:protected] => Array
                                        (
                                        )

                                    [extenders:protected] => Array
                                        (
                                        )

                                    [tags:protected] => Array
                                        (
                                        )

                                    [buildStack:protected] => Array
                                        (
                                        )

                                    [with:protected] => Array
                                        (
                                        )

                                    [contextual] => Array
                                        (
                                        )

                                    [reboundCallbacks:protected] => Array
                                        (
                                        )

                                    [globalBeforeResolvingCallbacks:protected] => Array
                                        (
                                        )

                                    [globalResolvingCallbacks:protected] => Array
                                        (
                                        )

                                    [globalAfterResolvingCallbacks:protected] => Array
                                        (
                                        )

                                    [beforeResolvingCallbacks:protected] => Array
                                        (
                                        )

                                    [resolvingCallbacks:protected] => Array
                                        (
                                        )

                                    [afterResolvingCallbacks:protected] => Array
                                        (
                                        )

                                )

                        )

                    [connections:protected] => Array
                        (
                        )

                    [extensions:protected] => Array
                        (
                        )

                    [reconnector:protected] => Closure Object
                        (
                            [this] => Illuminate\Database\DatabaseManager Object
 *RECURSION*
                            [parameter] => Array
                                (
                                    [$connection] => <required>
                                )

                        )

                    [doctrineTypes:protected] => Array
                        (
                        )

                )

            [container:protected] => Illuminate\Container\Container Object
                (
                    [resolved:protected] => Array
                        (
                        )

                    [bindings:protected] => Array
                        (
                        )

                    [methodBindings:protected] => Array
                        (
                        )

                    [instances:protected] => Array
                        (
                            [config] => Illuminate\Support\Fluent Object
                                (
                                    [attributes:protected] => Array
                                        (
                                            [database.fetch] => 5
                                            [database.default] => default
                                            [database.connections] => Array
                                                (
                                                    [default] => Array
                                                        (
                                                            [driver] => mysql
                                                            [host] => 10.10.10.234
                                                            [port] => 12345
                                                            [database] => seckill
                                                            [username] => root
                                                            [password] => 123456
                                                            [charset] => utf8mb4
                                                            [collation] => utf8mb4_general_ci
                                                            [prefix] => 
                                                        )

                                                    [test2] => Array
                                                        (
                                                            [driver] => mysql
                                                            [host] => localhost
                                                            [database] => test2
                                                            [username] => root
                                                            [password] => 500500
                                                            [charset] => utf8
                                                            [collation] => utf8_unicode_ci
                                                            [prefix] => 
                                                        )

                                                )

                                        )

                                )

                        )

                    [scopedInstances:protected] => Array
                        (
                        )

                    [aliases:protected] => Array
                        (
                        )

                    [abstractAliases:protected] => Array
                        (
                        )

                    [extenders:protected] => Array
                        (
                        )

                    [tags:protected] => Array
                        (
                        )

                    [buildStack:protected] => Array
                        (
                        )

                    [with:protected] => Array
                        (
                        )

                    [contextual] => Array
                        (
                        )

                    [reboundCallbacks:protected] => Array
                        (
                        )

                    [globalBeforeResolvingCallbacks:protected] => Array
                        (
                        )

                    [globalResolvingCallbacks:protected] => Array
                        (
                        )

                    [globalAfterResolvingCallbacks:protected] => Array
                        (
                        )

                    [beforeResolvingCallbacks:protected] => Array
                        (
                        )

                    [resolvingCallbacks:protected] => Array
                        (
                        )

                    [afterResolvingCallbacks:protected] => Array
                        (
                        )

                )

        )

    [dbSource:Core\init\MyDB:private] => default
)
View Code

 

2 代码

2.1 \pro\core\annotationhandlers\DBHandler.php

<?php

namespace Core\annotationhandlers;

use Core\annotations\DB;
use Core\BeanFactory;
use Core\init\MyDB;



return [
  DB::class=>function(\ReflectionProperty $prop,$instance,$self) {
    $mydbBean = BeanFactory::getBean(MyDB::class);   //从新获取一个对象
    $mydbBean ->setDbSource($self->source);//新MyDB对象设置数据源
    
    $prop->setAccessible(true);   //
    $prop->setValue($instance,$mydbBean);
    
    return $instance;
  }
];

2.1 \seckill\pro\core\annotations\DB.php

<?php

namespace Core\annotations;
use Doctrine\Common\Annotations\Annotation\Target;

/**
 * @Annotation
 * @Target({"PROPERTY"})
 */
class DB
{
    public $source =  'default';
}

2.3 \seckill\pro\app\config\db.php

<?php

return [
  "default"=>[
    'driver'    =>  'mysql',
    'host'      =>  '10.10.10.234', //我的dicker上的    da9aad4acad8   mysql:5.6
    'port'      =>  '12345',
    'database'  =>  'seckill',
    'username'  =>  'root',
    'password'  =>  '123456',
    'charset'   =>  'utf8mb4',
    'collation' =>  'utf8mb4_general_ci',
    'prefix'    =>  '',
    ] ,
  'test2' =>[
    'driver'    =>  'mysql',
    'host'      =>  'localhost',
    'database'  =>  'test2',
    'username'  =>  'root',
    'password'  =>  '500500',
    'charset'   =>  'utf8',
    'collation' =>  'utf8_unicode_ci',
    'prefix'    =>  '',
  ]
];

2.4 \seckill\pro\app\controllers\UserController.php

use Core\annotations\DB;
use Core\init\MyDB;

加载DB

/**
     * @DB
     * @var MyDB
     */
    private $db;

使用

    /**
     * @RequestMapping(value="/test")
     */
    public function test(Response $response )
    {
        return $this->db->table("users")->get();
    }

3 完整代码

https://github.com/guainttt/seckill/commit/ad3b3f64adc42ca225b951d43f81ab35f3485911#diff-6e54e7c44f70a08915676a86701ef8877d88d6e6799586f48610bbac299ab51c

 

posted on 2022-09-17 02:35  小白兔晒黑了  阅读(28)  评论(0编辑  收藏  举报