新建数据库
CREATE DATABASE `tp6stu01` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin';
创建表和数据
DROP TABLE IF EXISTS `tp_user`;
CREATE TABLE `tp_user` (`id` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自动编号',`username` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`password` char(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`gender` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '男',`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`price` decimal(8, 2) UNSIGNED NOT NULL DEFAULT 0.00,`details` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`uid` smallint(6) NULL DEFAULT NULL,`status` tinyint(3) NOT NULL DEFAULT 0 COMMENT '状态',`list` json NULL,`delete_time` datetime(0) NULL DEFAULT NULL,`create_time` datetime(0) NOT NULL DEFAULT '1997-01-01 01:01:01' COMMENT '创建时间',`update_time` datetime(0) NOT NULL DEFAULT '1997-01-01 01:01:01' COMMENT '修改时间',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 302 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
插入数据
INSERT INTO `tp_user` VALUES (1, '蜡笔小新', '123', '男', 'xiaoxin@163.com', 60.00, '123', 1001, -1, NULL, NULL, '2016-06-27 16:45:26', '1997-01-01 01:01:01');
INSERT INTO `tp_user` VALUES (2, '路飞', '123', '男', 'lufei@163.com', 70.00, '123', 1002, 0, NULL, NULL, '2016-06-27 16:55:56', '1997-01-01 01:01:01');
INSERT INTO `tp_user` VALUES (3, '黑崎一护', '456', '男', 'yihu@163.com', 80.00, '123', 1003, 1, NULL, NULL, '2016-07-27 17:22:16', '1997-01-01 01:01:01');
INSERT INTO `tp_user` VALUES (4, '小明', '123', '男', 'xiaoming@163.com', 90.00, '123', 1004, 2, NULL, NULL, '2016-08-27 23:50:52', '1997-01-01 01:01:01');
INSERT INTO `tp_user` VALUES (5, '孙悟饭', '123', '男', 'wufan@163.com', 100.00, '123', 1005, -1, NULL, NULL, '2016-08-28 18:02:53', '1997-01-01 01:01:01');
INSERT INTO `tp_user` VALUES (6, '孙悟天', '123', '男', 'wutian@163.com', 110.00, '123', NULL, 0, NULL, NULL, '2016-09-28 22:07:38', '1997-01-01 01:01:01');
INSERT INTO `tp_user` VALUES (7, '樱桃小丸子', '123', '女', 'yingtao@163.com', 77.00, '123', 1007, 1, NULL, NULL, '2016-10-29 10:53:58', '1997-01-01 01:01:01');
INSERT INTO `tp_user` VALUES (8, '孙悟空', '123', '男', 'wukong@163.com', 100.00, '123', 1008, 2, NULL, NULL, '2018-12-11 10:09:36', '1997-01-01 01:01:01');
INSERT INTO `tp_user` VALUES (9, '李白', '123', '男', 'UPPER(EMAIL)', 52.00, '123', 1011, 0, NULL, NULL, '2019-10-28 10:00:39', '2019-12-25 13:28:04');
INSERT INTO `tp_user` VALUES (10, '辉夜', '123', '女', 'HUIYE@163.COM', 91.00, '123', 1009, -3, NULL, NULL, '2019-09-20 22:01:42', '1997-01-01 01:01:01');
INSERT INTO `tp_user` VALUES (11, '李黑', '123', '男', 'LIBAI@163.COM', 102.00, '123', 2022, 3, NULL, NULL, '1997-01-01 01:01:01', '2020-01-16 17:19:08');
修改.env数据库配置
APP_DEBUG = true[APP]
DEFAULT_TIMEZONE = Asia/Shanghai[DATABASE]
TYPE = mysql
HOSTNAME = 10.10.90.139
DATABASE = tp6stu01
USERNAME = root
PASSWORD = password
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true
PREFIX = tp_[LANG]
default_lang = zh-cn
测试环境下是可以使用的,但是到了线上建议配置到config下database.php
// 数据库类型
'type' => env('database.type', 'mysql'),
// 服务器地址
'hostname' => env('database.hostname', '10.10.90.139'),
// 数据库名
'database' => env('database.database', 'tp6stu01'),
// 用户名
'username' => env('database.username', 'root'),
// 密码
'password' => env('database.password', 'password'),
// 端口
'hostport' => env('database.hostport', '3306'),
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => env('database.charset', 'utf8'),
// 数据库表前缀
'prefix' => env('database.prefix', 'tp_'),
可以通过删除改变.env 的配置,或删除.env 来验证 database 的执行优先级,经过验证可知:.env>database.php
//default 表示设置默认的数据库连接,这里用的是connections下mysql的配置
'default' => env('database.driver', 'mysql'),
新建DataTest.php
<?phpnamespace app\controller;use think\facade\Db;class DataTest
{public function index(){$user = Db::table('tp_user')->select();//也可以切换为name就不用写前缀了//$user = Db::name('user')->select();return json($user);}
}
connections 配置数据库连接信息,可以是多个数据库,便于切换,新建demo配置
'demo' => [// 数据库类型'type' => env('database.type', 'mysql'),// 服务器地址'hostname' => env('database.hostname', '10.10.90.139'),// 数据库名'database' => env('database.database', 'tp6stu02'),// 用户名'username' => env('database.username', 'root'),// 密码'password' => env('database.password', 'password'),// 端口'hostport' => env('database.hostport', '3306'),// 数据库连接参数'params' => [],// 数据库编码默认采用utf8'charset' => env('database.charset', 'utf8'),// 数据库表前缀'prefix' => env('database.prefix', 'tp_'),// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)'deploy' => 0,// 数据库读写是否分离 主从式有效'rw_separate' => false,// 读写分离后 主服务器数量'master_num' => 1,// 指定从服务器序号'slave_no' => '',// 是否严格检查字段是否存在'fields_strict' => true,// 是否需要断线重连'break_reconnect' => false,// 监听SQL'trigger_sql' => env('app_debug', true),// 开启字段缓存'fields_cache' => false,
],
新建demo方法
public function demo()
{$demo = Db::connect('demo')->table('tp_user')->select();return json($demo);
}
模型初探
在 app 目录下创建一个 model 目录,并创建 User.php 的模型类
<?phpnamespace app\model;use think\Model;class User extends Model
{
}
datatest.php添加getUser方法
use app\model\User;public function getUser()
{$user = User::select();return json($user);
}
数据库默认链接的default下的配置就是mysql,现在想切换为demo,需要给User添加一个属性
<?phpnamespace app\model;use think\Model;class User extends Model
{protected $connection = 'demo';
}