如果使用rocket框架,那么就不需要往下看了。因为rocket框架已经内置了ORM
配置diesel遇到了N个坑!在这里记录一下!我的电脑是win10 64位。diesel是数据库持久层。
开始操作
- 访问 https://downloads.mysql.com/archives/c-c/ 下载
mysql-connector-c-6.1.11-winx64
安装 - 找到mysql-connector-c安装路径, 我的路径如下
C:\Program Files\MySQL\MySQL Connector C 6.1\lib\vs14
- 以管理员权限启动CMD, 执行如下命令
setx MYSQLCLIENT_LIB_DIR "C:\Program Files\MySQL\MySQL Connector C 6.1\lib\vs14"
- 配置config文件,文件路径为
C:\Users\用户名\.cargo\config
,如果不存在那就创建config
文件,内容如下。
#配置使用中科大源
[http]
check-revoke = false
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"
#配置mysql编译
[build]
rustflags = ["-L", "C:\\Program Files\\MySQL\\MySQL Connector C 6.1\\lib\\vs14"]
在上面的配置文件中有
[build]
配置内容,这一步非常重要,如果在这里不进行配置的话,即使是diesel安装成功了,但是项目启动时无法正常启动,原因是在编译、测试时还是要这个环境变量,因为其中的依赖diesel = { version = "1.4.5", features = ["mysql"] }
需要它
- 安装diesel
cargo install diesel_cli --no-default-features --features mysql
- 安装完成后执行
diesel
,结果如下。
C:\WINDOWS\system32>diesel
diesel 1.4.1
USAGE:
diesel [FLAGS] [OPTIONS] <SUBCOMMAND>
FLAGS:
--locked-schema Require that the schema file is up to date
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
--config-file <CONFIG_FILE> The location of the configuration file to use. Falls back to the
`DIESEL_CONFIG_FILE` environment variable if unspecified. Defaults to
`diesel.toml` in your project root. See diesel.rs/guides/configuring-diesel-cli
for documentation on this file.
--database-url <DATABASE_URL> Specifies the database URL to connect to. Falls back to the DATABASE_URL
environment variable if unspecified.
SUBCOMMANDS:
bash-completion DEPRECATED: Generate bash completion script for the diesel command.
completions Generate shell completion scripts for the diesel command.
database A group of commands for setting up and resetting your database.
help Prints this message or the help of the given subcommand(s)
migration A group of commands for generating, running, and reverting migrations.
print-schema Print table definitions for database schema.
setup Creates the migrations directory, creates the database specified in your DATABASE_URL, and
runs existing migrations.
You can also run `diesel SUBCOMMAND -h` to get more information about that subcommand.