Summer MyBatis is an ORM framework based on rust language and mybatis framework. It is used to simplify development. Developers do not need to pay attention to complex SQL. They can carry out business development through dynamic SQL dialect. It can save your time. In addition, its speed is very fast.
Step1. Add mybatis dependency
rust
serde = { version = "1", features = ["derive"] }
rbson = "2.0"
tokio = { version = "1.18.2", features = ["full"] }
mybatis = { version = "1.0.2"}
Step2. Create a structure corresponding to the database table and map the method
```rust use mybatis::crud::CRUD; use mybatis::mybatissql::stringutil::tosnakename; use mybatis::crud::CRUDTable; use mybatis::mybatis::Mybatis; use mybatis::snowflake::SNOWFLAKE; use serde::{Serialize, Deserialize};
pub struct Pets {
pub id: Option
impl CRUDTable for Pets {
fn tablename() -> String {
let typename = std::any::typename::
to_snake_name(&name)
}
fn tablecolumns() -> String { String::from("id,name,birthday,deleteflag") }
} ```
Next, you can do some database business operations
```rust /// /// Save a single object ///
async fn save_pets() { let mybatis = Mybatis::new();
mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();
let id = SNOWFLAKE.generate(); let cat = Pets { id: Some(id.tostring()), name: Some("Cindy".tostring()), birthday: Some(mybatis::DateTimeNative::now()), delete_flag: Some(0), };
mybatis.save(&cat,&[]).await; }
/// /// Batch insert multiple objects ///
async fn savebatchpets() { let mybatis = Mybatis::new();
mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();
let cat = Pets { id: Some(SNOWFLAKE.generate().tostring()), name: Some("Tom".tostring()), birthday: Some(mybatis::DateTimeNative::now()), delete_flag: Some(0), };
let dog = Pets { id: Some(SNOWFLAKE.generate().tostring()), name: Some("Jerry".tostring()), birthday: Some(mybatis::DateTimeNative::now()), delete_flag: Some(0), };
mybatis.save_batch(&vec![cat, dog],&[]).await; }
/// /// Query multiple results and return dynamic array ///
async fn queryallpets() { let mybatis = Mybatis::new();
mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();
let result: Vec
/// /// Query a single object according to the specified field and return Option
async fn querypetby_name() { let mybatis = Mybatis::new();
mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();
let result: Option
/// /// Object modification through wrapper constructor ///
async fn updatepetby_name() { let mybatis = Mybatis::new();
mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();
let updatewrapper = mybatis.newwrapper().eq("name", "Tom");
let james = Pets { id: None, name: Some("James".tostring()), birthday: None, deleteflag: None, };
// Specifies which fields skip modifying the mapping let mut skipcolumns = Vec::new(); skipcolumns.push(Skip::Column("id")); skipcolumns.push(Skip::Column("birthday")); skipcolumns.push(Skip::Column("delete_flag"));
mybatis.updatebywrapper(&james, updatewrapper, &skipcolumns).await; }
/// /// Physically delete a single object ///
async fn deletepetby_name() { let mybatis = Mybatis::new();
mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();
mybatis.removebycolumn::
/// /// Physical batch deletion of multiple objects ///
async fn deletebetchpetbyname() { let mybatis = Mybatis::new();
mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();
mybatis.removebatchby_column::
So far, we have only implemented some flexible conditional wrappers for summer-mybatis. We are developing the mapping of XML files and supporting Oracle database driver. If you have any interests and ideas, please submit to PR or contact me.
I have been working hard and are looking for various contributions. Look forward to your help!