Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ publish = false
serde = { version = "1", features = ["derive"] }
rbson = "2.0"
tokio = { version = "1.18.2", features = ["full"] }
mybatis = { path = "../mybatis", features = ["debug_mode"]}
mybatis-util = {version = "1.0.8", path = "../mybatis-util"}
mybatis-core = {version = "1.0.8", path = "../mybatis-core"}
mybatis = { version = "2.0.0", path = "../mybatis", features = ["debug_mode"]}
mybatis-util = {version = "2.0.0", path = "../mybatis-util"}
mybatis-core = {version = "2.0.0", path = "../mybatis-core"}
6 changes: 3 additions & 3 deletions example/src/pets.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use mybatis::{mybatis_sql::string_util::to_snake_name};
use mybatis::mybatis::Mybatis;
use mybatis::mybatis_sql::string_util::to_snake_name;
use mybatis::plus::MybatisPlus;
use mybatis::snowflake::SNOWFLAKE;
use serde::{Serialize, Deserialize};


Expand Down Expand Up @@ -32,6 +30,8 @@ impl MybatisPlus for Pets {

#[cfg(test)]
mod tests {
use mybatis::mybatis::Mybatis;
use mybatis::snowflake::SNOWFLAKE;
use mybatis::plus::{Skip, Mapping};

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion mybatis-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mybatis-core"
version = "1.0.8"
version = "2.0.0"
description = "mybatis core by sqlx"
license = "Apache-2.0"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion mybatis-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mybatis-macro"
version = "1.0.8"
version = "2.0.0"
description = "mybatis macro driver"
authors = ["James Zow <jameszow@163.com>"]
edition = "2021"
Expand Down
6 changes: 3 additions & 3 deletions mybatis-sql/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mybatis-sql"
version = "1.0.8"
version = "2.0.0"
edition = "2021"
rust-version = "1.59.0"
authors = ["James Zow <Jameszow@163.com>"]
Expand All @@ -14,8 +14,8 @@ debug_mode = []

[dependencies]
once_cell = "1.9.0"
mybatis-core = { version = "1.0.8", path = "../mybatis-core" }
mybatis-macro = { version = "1.0.8", path = "../mybatis-macro" }
mybatis-core = { version = "2.0.0", path = "../mybatis-core" }
mybatis-macro = { version = "2.0.0", path = "../mybatis-macro" }

#serde
serde = { version = "1", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions mybatis-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "mybatis-util"
version = "1.0.8"
version = "2.0.0"
edition = "2021"
rust-version = "1.59.0"
authors = ["James Zow <Jameszow@163.com>"]
license = "Apache-2.0"
description = "mybatis util"

[dependencies]
mybatis-core = {version = "1.0.3", path = "../mybatis-core"}
mybatis-core = {version = "2.0.0", path = "../mybatis-core"}
rbson = { version = "2.0.3", features = ["uuid-0_8", "chrono-0_4"] }
10 changes: 5 additions & 5 deletions mybatis/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mybatis"
version = "1.0.8"
version = "2.0.0"
edition = "2021"
rust-version = "1.59.0"
authors = ["James Zow <Jameszow@163.com>"]
Expand Down Expand Up @@ -38,10 +38,10 @@ runtime-async-std-native-tls = ["mybatis-core/runtime-async-std-native-tls"]
format_bson = ["mybatis-core/format_bson"]

[dependencies]
mybatis-util = { version = "1.0.8", path = "../mybatis-util", default-features = false}
mybatis-sql = { version = "1.0.8", path = "../mybatis-sql", default-features = false, optional = true}
mybatis-core = { version = "1.0.8", path = "../mybatis-core", default-features = false, optional = true}
mybatis-macro = { version = "1.0.8", path = "../mybatis-macro", default-features = false, optional = true}
mybatis-util = { version = "2.0.0", path = "../mybatis-util", default-features = false}
mybatis-sql = { version = "2.0.0", path = "../mybatis-sql", default-features = false, optional = true}
mybatis-core = { version = "2.0.0", path = "../mybatis-core", default-features = false, optional = true}
mybatis-macro = { version = "2.0.0", path = "../mybatis-macro", default-features = false, optional = true}

################################################################
rbson = { version = "2.0.3", features = ["uuid-0_8", "chrono-0_4"] }
Expand Down
233 changes: 96 additions & 137 deletions mybatis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,66 @@ Summer MyBatis is an ORM framework based on rust language and mybatis framework.

## Getting Started

* Step1. Add mybatis dependency

* Add mybatis dependency
```rust
mybatis = { version = "1.0.8"}
// other dependency
mybatis = { version = "2.0.0"}

/// other dependencys
serde = { version = "1", features = ["derive"] }
rbson = "2.0"
tokio = { version = "1.18.2", features = ["full"] }
```
* Step2. Create a structure corresponding to the database table and map the method


* Example

<details open=“open”>
<summary> Use #[mybatis_plus] macro data table mapping </summary>

```rust
use serde::{Serialize, Deserialize};

#[mybatis_plus]
#[derive(Debug, Serialize, Deserialize)]
pub struct Books {
pub id: Option<String>,
pub name: Option<String>,
pub types: Option<String>
}


#[cfg(test)]
mod tests {
use super::*;
use mybatis::mybatis::Mybatis;
use mybatis::snowflake::SNOWFLAKE;
use mybatis::plus::Mapping;

#[tokio::test]
async fn save_books() {
let mybatis = Mybatis::new();

mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();

let id = SNOWFLAKE.generate();
let cat = Books {
id: Some(id.to_string()),
name: Some("《Daughter of the sea》".to_string()),
types: Some("Fairy Tales".to_string()),
};

mybatis.save(&cat,&[]).await;
}
}
```
</details>

<details>
<summary>If you don't want to use macros, you can create a structure corresponding to the database table and map the method </summary>

```rust
use mybatis::crud::CRUD;
use mybatis::mybatis_sql::string_util::to_snake_name;
use mybatis::crud::CRUDTable;
use mybatis::mybatis::Mybatis;
use mybatis::snowflake::SNOWFLAKE;
use mybatis::plus::MybatisPlus;
use serde::{Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -45,7 +88,7 @@ Summer MyBatis is an ORM framework based on rust language and mybatis framework.
pub delete_flag: Option<i32>,
}

impl CRUDTable for Pets {
impl MybatisPlus for Pets {

fn table_name() -> String {
let type_name = std::any::type_name::<Self>();
Expand All @@ -61,136 +104,52 @@ Summer MyBatis is an ORM framework based on rust language and mybatis framework.
}

}
```
* Next, you can do some database business operations

```rust
///
/// Save a single object
///
#[tokio::test]
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.to_string()),
name: Some("Cindy".to_string()),
birthday: Some(mybatis::DateTimeNative::now()),
delete_flag: Some(0),
};

mybatis.save(&cat,&[]).await;
}

///
/// Batch insert multiple objects
///
#[tokio::test]
async fn save_batch_pets() {
let mybatis = Mybatis::new();

mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();

let cat = Pets {
id: Some(SNOWFLAKE.generate().to_string()),
name: Some("Tom".to_string()),
birthday: Some(mybatis::DateTimeNative::now()),
delete_flag: Some(0),
};

let dog = Pets {
id: Some(SNOWFLAKE.generate().to_string()),
name: Some("Jerry".to_string()),
birthday: Some(mybatis::DateTimeNative::now()),
delete_flag: Some(0),
};
#[cfg(test)]
mod tests {
use super::*;
use mybatis::mybatis::Mybatis;
use mybatis::snowflake::SNOWFLAKE;
use mybatis::plus::{Skip, Mapping};

///
/// Save a single object
///
#[tokio::test]
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.to_string()),
name: Some("Cindy".to_string()),
birthday: Some(mybatis::DateTimeNative::now()),
delete_flag: Some(0),
};

mybatis.save_batch(&vec![cat, dog],&[]).await;
}

///
/// Query multiple results and return dynamic array
///
#[tokio::test]
async fn query_all_pets() {
let mybatis = Mybatis::new();

mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();

let result: Vec<Pets> = mybatis.fetch_list().await.unwrap();
println!("result: {:?}", result);
}

///
/// Query a single object according to the specified field and return Option<Object>
///
#[tokio::test]
async fn query_pet_by_name() {
let mybatis = Mybatis::new();

mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();

let result: Option<Pets> = mybatis.fetch_by_column("name", &"Cindy").await.unwrap();
println!("result: {:?}", result);
}

///
/// Object modification through wrapper constructor
///
#[tokio::test]
async fn update_pet_by_name() {
let mybatis = Mybatis::new();

mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();

let update_wrapper = mybatis.new_wrapper().eq("name", "Tom");

let james = Pets {
id: None,
name: Some("James".to_string()),
birthday: None,
delete_flag: None,
};

// Specifies which fields skip modifying the mapping
let mut skip_columns = Vec::new();
skip_columns.push(Skip::Column("id"));
skip_columns.push(Skip::Column("birthday"));
skip_columns.push(Skip::Column("delete_flag"));

mybatis.update_by_wrapper(&james, update_wrapper, &skip_columns).await;
}

///
/// Physically delete a single object
///
#[tokio::test]
async fn delete_pet_by_name() {
let mybatis = Mybatis::new();

mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();
mybatis.remove_by_column::<Pets,_>("name", "James").await;
}


///
/// Physical batch deletion of multiple objects
///
#[tokio::test]
async fn delete_betch_pet_by_name() {
let mybatis = Mybatis::new();

mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();

mybatis.remove_batch_by_column::<Pets,_>("name", &["Cindy", "Jerry"]).await;
}
```
mybatis.save(&cat,&[]).await;
}

///
/// Query a single object according to the specified field and return Option<Object>
///
#[tokio::test]
async fn query_pet_by_name() {
let mybatis = Mybatis::new();

mybatis.link("mysql://root:passw0rd@localhost:3306/test").await.unwrap();

let result: Option<Pets> = mybatis.fetch_by_column("name", &"Cindy").await.unwrap();
println!("result: {:?}", result);
}
}
```
</details>

## Contribution

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!
I have been working hard and are looking for various contributions. Look forward to your help!