``` rust
serde = { version = "1.0", features = ["derive"] } serde_json = "1.0"
log = "0.4" fast_log="1.0.2"
rbatis-core = { version = "1.2.0", default-features = false , features = ["all","runtime-async-std"]} rbatis = "1.2.0" ```
rust
let activity = Activity {
id: Some("12312".to_string()),
name: None,
remark: None,
create_time: Some("2020-02-09 00:00:00".to_string()),
version: Some(1),
delete_flag: Some(1),
};
let r = rb.save(&activity).await;
if r.is_err() {
println!("{}", r.err().unwrap().to_string());
}
rust
let mut m = Map::new();
m.insert("a".to_string(), json!("1"));
let w = Wrapper::new(&DriverType::Mysql).eq("id", 1)
.and()
.ne("id", 1)
.and()
.in_array("id", &[1, 2, 3])
.and()
.not_in("id", &[1, 2, 3])
.and()
.all_eq(&m)
.and()
.like("name", 1)
.or()
.not_like("name", "asdf")
.and()
.between("create_time", "2020-01-01 00:00:00", "2020-12-12 00:00:00")
.group_by(&["id"])
.order_by(true, &["id", "name"])
.check().unwrap();
python
//执行到远程mysql 并且获取结果。支持serde_json可序列化的任意类型
let rb = Rbatis::new(MYSQL_URL).await.unwrap();
let py = r#"
SELECT * FROM biz_activity
WHERE delete_flag = #{delete_flag}
if name != null:
AND name like #{name+'%'}
if ids != null:
AND id in (
trim ',':
for item in ids:
#{item},
)"#;
let data: serde_json::Value = rb.py_fetch("", py, &json!({ "delete_flag": 1 })).await.unwrap();
println!("{}", data);
rust
//main函数加入
use log::{error, info, warn};
fn main(){
fast_log::log::init_log("requests.log", &RuntimeType::Std).unwrap();
info!("print data");
}
``` rust /** * 数据库表模型 */
pub struct Activity {
pub id: Option
let arg = &json!({
"delete_flag": 1,
"name": "test",
"startTime": null,
"endTime": null,
"page": 0,
"size": 20
});
let data: Vec<Activity> = rb.xml_fetch("", "test", "select_by_condition", arg).await.unwrap();
println!("{}", serde_json::to_string(&data).unwrap_or("".to_string()));
}
)
} //输出结果 //2020-06-27T03:13:40.422307200+08:00 INFO rbatis::rbatis - [rbatis] >> fetch sql: select * from bizactivity where name like ? order by createtime desc limit ? , ? (src\rbatis.rs:198) //2020-06-27T03:13:40.424307300+08:00 INFO rbatis::rbatis - [rbatis] >> fetch arg:["%test%",0,20] (src\rbatis.rs:199) //2020-06-27T03:13:40.446308900+08:00 INFO rbatis::rbatis - [rbatis] << 4 (src\rbatis.rs:234) //[{"id":"221","name":"test","pclink":"","h5link":"","pcbannerimg":null,"h5bannerimg":null,"sort":"0","status":0,"remark":"","createtime":"2020-06-17T20:10:23Z","version":0,"deleteflag":1},{"id":"222","name":"test","pclink":"","h5link":"","pcbannerimg":null,"h5bannerimg":null,"sort":"0","status":0,"remark":"","createtime":"2020-06-17T20:10:23Z","version":0,"deleteflag":1},{"id":"223","name":"test","pclink":"","h5link":"","pcbannerimg":null,"h5bannerimg":null,"sort":"0","status":0,"remark":"","createtime":"2020-06-17T20:10:23Z","version":0,"deleteflag":1},{"id":"178","name":"testinsret","pclink":"","h5link":"","pcbannerimg":null,"h5bannerimg":null,"sort":"1","status":1,"remark":"","createtime":"2020-06-17T20:08:13Z","version":0,"delete_flag":1}] ```
rust
async_std::task::block_on(async {
let rb = Rbatis::new(MYSQL_URL).await.unwrap();
let tx_id = "1";
rb.begin(tx_id).await.unwrap();
let v: serde_json::Value = rb.fetch(tx_id, "SELECT count(1) FROM biz_activity;").await.unwrap();
println!("{}", v.clone());
rb.commit(tx_id).await.unwrap();
});
``` rust
lazystatic! { static ref RB:Rbatis<'static>=asyncstd::task::blockon(async { Rbatis::new(MYSQLURL).await.unwrap() }); }
use std::convert::Infallible;
async fn hello(: hyper::Request
pub async fn testhyper(){
fastlog::log::initlog("requests.log",&RuntimeType::Std);
// For every connection, we must make a Service
to handle all
// incoming HTTP requests on said connection.
let makesvc = hyper::service::makeservicefn(|conn| {
// This is the Service
that will handle the connection.
// service_fn
is a helper to convert a function that
// returns a Response into a Service
.
async { Ok::<_, Infallible>(hyper::service::servicefn(hello)) }
});
let addr = ([0, 0, 0, 0], 8000).into();
let server = hyper::Server::bind(&addr).serve(make_svc);
println!("Listening on http://{}", addr);
server.await.unwrap();
}
```
| 数据库 | 已支持 |
| ------ | ------ |
| Mysql | √ |
| Postgres | √ |
| Sqlite | √ |
| TiDB | √ |
| CockroachDB | √ |
| 功能 | 已支持 |
| ------ | ------ |
| CRUD(内置CRUD模板(内置CRUD支持乐观锁/逻辑删除)) | √ |
| LogSystem(日志组件) | √ |
| Tx(事务/事务嵌套/注解声明式事务) | √ |
| Py(在SQL中使用和xml等价的类python语法) | √ |
| SlowSqlCount(内置慢查询日志分析) | √ |
| async/await支持 | √ |
| PagePlugin(分页插件) | √ |
| LogicDelPlugin(逻辑删除插件) | x |
| VersionLockPlugin(乐观锁插件,防止并发修改数据) | x |
| DataBaseConvertPlugin(数据库表结构转换为配置插件) | x |
| web(可视化Web UI) | x |
``` //sql构建性能 ExampleActivityMapper.xml -> selectby_condition 操作/纳秒nano/op: 0.202 s,each:2020 nano/op 事务数/秒 TPS: 495049.50495049503 TPS/s
//查询结果解码性能 decode/mysqljsondecoder -> benchdecodemysql_json 操作/纳秒nano/op: 0.24 s,each:2400 nano/op 事务数/秒 TPS: 416666.6666666667 TPS/s
//综合性能约等于 操作/纳秒nano/op: 4420 nano/op 事务数/秒 TPS: 200000 TPS/s ```
oracle数据库驱动支持?
不支持,应该坚持去IOE
直接使用驱动依赖项目里哪个库?
应该使用rbatis-core, Cargo.toml 加入 rbatis-core = "*"
如何选择运行时是tokio还是async_std?
```rust
rbatis-core = { features = ["runtime-async-std","all-type"]}
```