Akita is a mini framework for MySQL.
You may be looking for:
Click to show Cargo.toml.
Run this code in the playground.
```toml [dependencies]
akita = { version = "1.0", features = ["derive"] }
```
```rust use serde::{Serialize, Deserialize};
/// Annotion Support: Table、Id、column
pub struct User { #[id(name="id")] pub pk: i64, pub id: String, pub name: String, pub headline: String, pub avatarurl: String, pub gender: i32, pub isorg: bool, #[column(name="token")] pub urltoken: String, pub usertype: String, }
fn main() { // use r2d2 pool let opts = Opts::fromurl("mysql://root:127.0.0.1:3306/test").expect("database url is empty."); let pool = Pool::builder().maxsize(4).build(MysqlConnectionManager::new(OptsBuilder::from_opts(opts))).unwrap(); let mut conn = pool.get().unwrap();
/// build the wrapper.
let mut wrapper = UpdateWrapper::new()
.like(true, "username", "ffff");
.eq(true, "username", 12);
.eq(true, "username", "3333");
.in_(true, "username", vec![1,44,3]);
.not_between(true, "username", 2, 8);
.set(true, "username", 4);
let user = User{
id: 2,
username: "username".to_string(),
mobile: "mobile".to_string(),
password: "password".to_string()
};
let conn = ConnMut::Pooled(&mut conn);
// Transaction
conn.start_transaction(TxOpts::default()).map(|mut transaction| {
match user.update( & mut wrapper, conn) {
Ok(res) => {}
Err(err) => {
println!("error : {:?}", err);
}
}
});
/// update by identify
match user.update_by_id(conn) {
Ok(res) => {}
Err(err) => {
println!("error : {:?}", err);
}
}
/// delete by identify
match user.delete_by_id(conn) {
Ok(res) => {}
Err(err) => {
println!("error : {:?}", err);
}
}
/// delete by condition
match user.delete:: < UpdateWrapper > ( & mut wrapper, conn) {
Ok(res) => {}
Err(err) => {
println!("error : {:?}", err);
}
}
/// insert data
match user.insert(conn) {
Ok(res) => {}
Err(err) => {
println!("error : {:?}", err);
}
}
/// find by identify
match user.find_by_id(conn) {
Ok(res) => {}
Err(err) => {
println!("error : {:?}", err);
}
}
/// find one by condition
match user.find_one::<UpdateWrapper>(&mut wrapper, conn) {
Ok(res) => {}
Err(err) => {
println!("error : {:?}", err);
}
}
/// find page by condition
match user.page::<UpdateWrapper>(1, 10,&mut wrapper, conn) {
Ok(res) => {}
Err(err) => {
println!("error : {:?}", err);
}
}
} ```
To setup the development envrionment run cargo run
.
MrPan <1049058427@qq.com>
Akita is a personal project. At the beginning, I just like Akita dog because of my hobbies. I hope this project will grow more and more lovely. Many practical database functions will be added in the future. I hope you can actively help this project grow and put forward suggestions. I believe the future will be better and better.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Akita by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.