``` rust
rbatis = "*" ```
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");
}
xml
<mapper>
<result_map id="BaseResultMap">
<id column="id" property="id"/>
<result column="name" property="name" lang_type="string"/>
<result column="pc_link" property="pcLink" lang_type="string"/>
<result column="h5_link" property="h5Link" lang_type="string"/>
<result column="remark" property="remark" lang_type="string"/>
<result column="version" property="version" lang_type="number" version_enable="true"/>
<result column="create_time" property="createTime" lang_type="time"/>
<result column="delete_flag" property="deleteFlag" lang_type="number" logic_enable="true" logic_undelete="1" logic_deleted="0"/>
</result_map>
<select id="select_by_condition" result_map="BaseResultMap">
<bind name="pattern" value="'%' + name + '%'"/>
select * from biz_activity
<where>
<if test="name != null">and name like #{pattern}</if>
<if test="startTime != null">and create_time >= #{startTime}</if>
<if test="endTime != null">and create_time <= #{endTime}</if>
</where>
order by create_time desc
<if test="page != null and size != null">limit #{page}, #{size}</if>
</select>
</mapper>
toml
[dependencies]
rbatis = "*"
log = "0.4"
fast_log="1.0.2"
``` rust
fn main() { asyncstd::task::blockon(async move { fastlog::log::initlog("requests.log", &RuntimeType::Std).unwrap(); let rb = Rbatis::new(MYSQLURL).await.unwrap(); let py = r#" SELECT * FROM bizactivity WHERE deleteflag = #{deleteflag} if name != null: AND name like #{name+'%'} if ids != null: AND id in ( trim ',': for item in ids: #{item}, )"#; let data: serdejson::Value = rb.pyfetch("", py, &json!({ "delete_flag": 1 })).await.unwrap(); println!("{}", data); }); } ```
``` rust use crate::core::rbatis::Rbatis; use serde_json::{json, Value, Number}; /** * 数据库表模型 */
pub struct Activity {
pub id: Option
fn main() {
fastlog::log::initlog("requests.log").unwrap();//1 启用日志(可选,不添加则不加载日志库)
let mut rb = Rbatis::new("mysql://root:TEST@localhost:3306/test").await.unwrap();//2 初始化rbatis
rb.loadxml("ExampleActivityMapper.xml".tostring(),fs::readtostring("./src/example/ExampleActivityMapper.xml").unwrap());//4 加载xml配置
let dataresult: Vec
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支持 | √ |
| LogicDelPlugin(逻辑删除插件) | x |
| VersionLockPlugin(乐观锁插件,防止并发修改数据) | x |
| PagePlugin(分页插件) | 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 ```