- Notifications
You must be signed in to change notification settings - Fork159
define driver struct on project config file
zxj edited this pageNov 2, 2023 ·1 revision
Welcome to the rbatis first wiki!today we see some java's database configlook like this:
spring:datasource:username: rootpassword: 123456url: jdbc:mysql://192.168.85.111:3306/demo?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8driver-class-name: com.mysql.jdbc.Driver
oh,We can see that Java can specify the path of the class, which makes it very convenient to replace the database data source and is also very conducive to CI construction. So how can we implement it in Rust?
seeabs_admin
- 0 create your config file
application.json5
{//choose database//"sqlite://target/sqlite.db"//"mysql://root:123456@localhost:3306/test"//"postgres://postgres:123456@localhost:5432/postgres"//"mssql://SA:TestPass!123456@localhost:1433/test"database_url:"sqlite://target/sqlite.db",//choose driver struct(Cargo.toml must add like 'rbdc-*** = { version = "4.4" }')//"rbdc_sqlite::Driver{}"//"rbdc_mysql::Driver{}"//"rbdc_mssql::Driver{}"//"rbdc_pg::Driver{}"database_struct:"rbdc_sqlite::Driver{}",}
- 1 add dev dep(Cargo.toml)
[build-dependencies]json5 ="0.4"serde = {version ="1.0",features = ["derive"] }
- 2 define your build.rs and read config file(this is
application.json5
)
use std::fs::File;use std::io::Write;#[derive(Debug,PartialEq, serde::Serialize, serde::Deserialize)]pubstructApplicationConfig{pubdatabase_struct:String,}fnmain(){println!("start build");let js_data =include_str!("application.json5");let result:ApplicationConfig = json5::from_str(js_data).expect("load config file fail");println!("result={:?}",result);letmut f=File::create("target/driver.rs").unwrap(); f.write_all(result.database_struct.as_ref()).unwrap(); f.flush().unwrap();drop(f);}
- 3 use
include!
import code
//let driver =include!("../../target/driver.rs");