Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wansenai/mybatis
Object relational mapping SQL framework for Rust
https://github.com/wansenai/mybatis
cargo mapper mybatis mybatis-plus mysql orm rust sqlserver
Last synced: about 9 hours ago
JSON representation
Object relational mapping SQL framework for Rust
- Host: GitHub
- URL: https://github.com/wansenai/mybatis
- Owner: wansenai
- License: other
- Created: 2021-03-15T14:49:21.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-02-05T04:12:59.000Z (10 months ago)
- Last Synced: 2024-03-25T14:03:41.197Z (8 months ago)
- Topics: cargo, mapper, mybatis, mybatis-plus, mysql, orm, rust, sqlserver
- Language: Rust
- Homepage:
- Size: 6.79 MB
- Stars: 85
- Watchers: 4
- Forks: 14
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# MyBatis Rust
![Build Status](https://github.com/rust-lang/book/workflows/CI/badge.svg)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fsummer-os%2Fsummer-mybatis.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fsummer-os%2Fsummer-mybatis?ref=badge_shield)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. Use
[sqlx-core crate](https://crates.io/crates/sqlx-core)、
[tokio crate](https://crates.io/crates/tokio)、
[rbatis crate](https://crates.io/crates/rbatis)、
[bson crate](https://crates.io/crates/bson)
is used for secondary packaging development.In addition, its speed is very fast.
## Features:
* Cross platform support (linux, macos, windows)
* No Runtimes,No Garbage Collection
* Powerful and flexible where conditional wrapper.
* Support multiple database drivers (mssql, mysql, postgres, sqlite).
* Support asynchronous operation with tokio.
* Structure automatic serialization and deserialization.## Getting Started
* Add mybatis dependency
```rust
mybatis = { version = "2.0.4"}
```* Example
Use #[mybatis_plus] macro data table mapping
```rust
use serde::{Serialize, Deserialize};#[mybatis_plus]
#[derive(Debug, Serialize, Deserialize)]
pub struct Books {
pub id: Option,
pub name: Option,
pub types: Option
}#[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;
}
}
```
If you don't want to use macros, you can create a structure corresponding to the database table and map the method
```rust
use mybatis::mybatis_sql::string_util::to_snake_name;
use mybatis::plus::MybatisPlus;
use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Pets {
pub id: Option,
pub name: Option,
pub birthday: Option,
pub delete_flag: Option,
}
impl MybatisPlus for Pets {fn table_name() -> String {
let type_name = std::any::type_name::();
let mut name = type_name.to_string();
let names: Vec<&str> = name.split("::").collect();
name = names.get(names.len() - 1).unwrap_or(&"").to_string();to_snake_name(&name)
}fn table_columns() -> String {
String::from("id,name,birthday,delete_flag")
}
}
#[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(&cat,&[]).await;
}///
/// Query a single object according to the specified field and return Option
///
#[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 = mybatis.fetch_by_column("name", &"Cindy").await.unwrap();
println!("result: {:?}", result);
}
}
```
## Community
If you have any use questions, you can ask questions in [mybatis discussions](https://github.com/summer-rust/summer-mybatis/discussions) or submit an issue.
## LicenseLicensed under either of
* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fsummer-os%2Fsummer-mybatis.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fsummer-os%2Fsummer-mybatis?ref=badge_large)
## ContributionSo 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.
We looking for various contributions. Look forward to your contributions!