In all, it Rust
, and Rust has become more and more popular. In the process, many pits have been stepped on, and due to Rust
, a lot of abandoned knowledge has to be supplemented; of course. I have a deeper understanding of computers, and my hair has naturally become thinner.
"Round" Regulations
Today, one by one to share their crate
, because recent advances Rust
more and more small partners, many of which are from Java
Kong turn, they turn first thing is crazy looking for a nice ORM framework, but Rust
currently relatively easy to use relational database framework diesel
, sqlx
etc., basically able to meet the daily demands of work. In order to facilitate the transition of more friends to Rust
, I used a small framework, refer to MyBatisPlus
, I believe that as long as the small partners who have used the framework should basically be able to seamlessly transition. At present, it has transitioned to version 0.2.0, and basically it is updated frequently. Because there are many early ideas and there is no fixed direction, it is often overthrown and restarted.
This crate is called akita
, which translates to the meaning of Akita dog, which also means cute and cute. The basic realization idea is to complete the mapping of the database table structure Rust
, and then encapsulate some convenient tools and methods for the assembly of SQL
Currently the project only supports MySQL
, and the thread pool used is r2d2
. It is planned to support ClickHouse
, SQLite
, MSSQL
, ORACLE
and other databases.
Open code directly
Don’t say much, let’s code directly, so let’s try one. First add dependencies, now the current version is 0.2.4.
[dependencies]
# The core APIs, including the Table traits. Always
# required when using Akita. using #[derive(Table)]
# to make Akita work with structs defined in your crate.
akita = { version = "0.2.0"] }
First we define a structure SystemUser
, Akita
provides three FromAkita
, ToAkita
and Table
, which are mainly used to parse the field structure of the structure and generate basic CRUD member methods. In addition, the table
annotation is provided to annotate the database table name, field
and table_id
annotate the column name and primary key, respectively have the name attribute, while retaining the exist attribute MyBatisPlus
#[derive(Debug, FromAkita, ToAkita, Table, Clone)]
#[table(name="t_system_user")]
struct SystemUser {
#[field = "name"]
id: Option<i32>,
#[table_id]
username: String,
#[field(name="ages", exist = "false")]
age: i32,
}
Come and come, Akita
provides two public managers, AkitaManager
and AkitaEntityManager
, the former mainly encapsulates some primitive SQL operations, the latter adds a relatively complete API, and these APIs are also implemented in the structure. Let's take CRUD operations as an example:
use akita::*;
use akita::prelude::*;
fn main() {
let mut pool = Pool::new(AkitaConfig{ max_size: None, url: String::from("mysql://root:password@localhost:3306/akita"), log_level: None }).unwrap();
let mut em = pool.entity_manager().expect("must be ok");
let user = SystemUser { id: 1.into(), username: "fff".to_string(), age: 1 };
// 新增
match em.save(&user) {
Ok(res) => { }
Err(err) => { }
}
/// 结构体示例
match user.insert(&mut em) {
Ok(res) => { }
Err(err) => { }
}
// 删除
match em.remove_by_id::<SystemUser, String>("id".to_string()) {
Ok(res) => { }
Err(err) => { }
}
// 修改
match em.update_by_id(&user, "id") {
Ok(res) => { }
Err(err) => { }
}
// 查询分页
let mut wrapper = UpdateWrapper::new();
wrapper.eq( "username", "ussd").eq("id", 1);
match em.page::<SystemUser, UpdateWrapper>(1, 10,&mut wrapper) {
Ok(res) => { }
Err(err) => { }
}
// 查询单条
match em.select_one::<SystemUser, UpdateWrapper>(&mut wrapper) {
Ok(res) => { }
Err(err) => { }
}
...
}
After reading it, are you very touched? For me who has written SQL for more than half a year, it is a good news... In addition, there are many performance aspects that we are optimizing, such as large amount of data paging and related slow SQL interception and some other optimizations. .
plan
The project is currently maintained by only a few people, so the update speed will not be very fast in the future. We will also use this framework in production to gradually optimize it. What we plan to do in the future:
- Support
ORACLE
,ClickHouse
,SQLite
,MSSQL
multiple features - Strengthen annotations, increase configuration mapping, support more custom annotations
- Support more data structures and optimize internal parameter processing
- Improve automatic code generation tools to reduce repetitive work
Run after the code (lazy)
So next is your time, it's time to copy, it's time to slip away. (escape
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。