WebSite | 简体中文 | Showcase | 案例
Box<dyn Driver>
separation#{arg}
, ${arg}
,?
placeholderlog
crate#![forbid(unsafe_code)]
enabledpy_sql/ html_sql
functions.seeSQLX, mobc, Tiberius, MyBatis,xorm
and so on reference design or code implementation. release of V4.0 is dependent on the support of these frameworks, both directly and indirectly and idea| data structure | is supported |
|---------------------------------------------------------------------------| ------ |
| Option | √ |
| Vec | √ |
| HashMap | √ |
| i32,i64,f32,f64,bool,String...more rust type | √ |
| rbdc::types::Date | √ |
| rbdc::types::FastDateTime | √ |
| rbdc::types::Time | √ |
| rbdc::types::Timestamp | √ |
| rbdc::types::Decimal | √ |
| rbdc::types::Json | √ |
| rbatis::plugin::page::{Page
| database | is supported |
|------------------------|--------------|
| Mysql | √ |
| Postgres | √ |
| Sqlite | √ |
| Mssql | √ |
| Oracle | x(10%) |
| MariaDB(=Mysql) | √ |
| TiDB(=Mysql) | √ |
| CockroachDB(=Postgres) | √ |
| platform | is supported |
| ------ | ------ |
| Linux(unbutu laster*) | √ |
| Apple/MacOS(laster) | √ |
| Windows(latest) | √ |
serde = { version = "1", features = ["derive"] } rbs = "0.1"
log = "0.4" fast_log="1.3"
rbatis = { version = "4.0" }
rbdc={version="0.1"}
rbdc-sqlite={version="0.1"}
```
```rust //#[macro_use] define in 'root crate' or 'mod.rs' or 'main.rs'
extern crate rbatis; extern crate rbdc; use rbatis::{implinsert,implinsert,implupdate,impldelete,implselectpage}; use rbdc::datetime::FastDateTime;
pub struct BizActivity {
pub id: Option
implselect!(BizActivity{selectallbyid(id:&str,name:&str) => "where id = #{id} and name = #{name}
"});
implselect!(BizActivity{selectbyid(id:String) -> Option => "where id = #{id} limit 1
"});
implupdate!(BizActivity{updatebyname(name:&str) => "where id = 1
"});
impldelete!(BizActivity {deletebyname(name:&str) => "where name= '2'
"});
implselectpage!(BizActivity{selectpage(name:&str) => "where name != #{name}
"});
async fn main() {
/// enable log crate to show sql logs
fastlog::init(fastlog::Config::new().console());
/// initialize rbatis. also you can call rb.clone(). this is an Arc point
let rb = Rbatis::new();
/// connect to database
// sqlite
rb.link(SqliteDriver {},"sqlite://target/sqlite.db").await.unwrap();
// mysql
// rb.link(MysqlDriver{},"mysql://root:123456@localhost:3306/test").await.unwrap();
// postgresql
// rb.link(PgDriver{},"postgres://postgres:123456@localhost:5432/postgres").await.unwrap();
// mssql/sqlserver
// rb.link(MssqlDriver{},"jdbc:sqlserver://localhost:1433;User=SA;Password={TestPass!123456};Database=test").await.unwrap();
let activity = BizActivity { id: Some("2".into()), name: Some("2".into()), pclink: Some("2".into()), h5link: Some("2".into()), pcbannerimg: None, h5bannerimg: None, sort: None, status: Some(2), remark: Some("2".into()), createtime: Some(FastDateTime::now()), version: Some(1), deleteflag: Some(1), }; let data = BizActivity::insert(&mut rb, &activity).await; println!("insert = {:?}", data);
let data = BizActivity::selectallbyid(&mut rb, "1", "1").await; println!("selectallbyid = {:?}", data);
let data = BizActivity::selectbyid(&mut rb, "1".tostring()).await; println!("selectby_id = {:?}", data);
let data = BizActivity::updatebycolumn(&mut rb, &activity, "id").await; println!("updatebycolumn = {:?}", data);
let data = BizActivity::updatebyname(&mut rb, &activity, "test").await; println!("updatebyname = {:?}", data);
let data = BizActivity::deletebycolumn(&mut rb, "id", &"2".into()).await; println!("deletebycolumn = {:?}", data);
let data = BizActivity::deletebyname(&mut rb, "2").await; println!("deletebycolumn = {:?}", data);
let data = BizActivity::selectpage(&mut rb, &PageRequest::new(1, 10), "2").await; println!("selectpage = {:?}", data); } ///...more usage,see crud.rs ```
Because of the compile time, the annotations need to declare the database type to be used.
rust
#[py_sql("select * from biz_activity where delete_flag = 0
if name != '':
`and name=#{name}`")]
async fn py_sql_tx(rb: &Rbatis, tx_id: &String, name: &str) -> Vec<BizActivity> { impled!() }
Because of the compile time, the annotations need to declare the database type to be used
html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "https://github.com/rbatis/rbatis_codegen/raw/main/mybatis-3-mapper.dtd">
<mapper>
<select id="select_by_condition">
`select * from biz_activity where `
<if test="name != ''">
name like #{name}
</if>
</select>
</mapper>
rust
///select page must have '?:&PageRequest' arg and return 'Page<?>'
#[html_sql("example/example.html")]
async fn select_by_condition(rb: &mut dyn Executor, page_req: &PageRequest, name: &str) -> Page<BizActivity> { impled!() }
```rust
use once_cell::sync::Lazy;
pub static RB:Lazy
/// Macro generates execution logic based on method definition, similar to @select dynamic SQL of Java/Mybatis
/// RB is the name referenced locally by Rbatis, for example DAO ::RB, com:: XXX ::RB... Can be
/// The second parameter is the standard driver SQL. Note that the corresponding database parameter mysql is? , pg is $1...
/// macro auto edit method to 'pub async fn select(name: &str) -> rbatis::core::Result
pub async fn select(rb: &Rbatis,name: &str) -> BizActivity {}
//or: pub async fn select(name: &str) -> rbatis::core::Result
pub async fn testmacro() { fastlog::init(fast_log::Config::new().console()); RB.link("mysql://root:123456@localhost:3306/test").await.unwrap(); let a = select(&RB,"1").await.unwrap(); println!("{:?}", a); } ```
捐赠
联系方式(添加好友请备注'rbatis') 微信群:先加微信,然后拉进群