1. 首页
  2. >
  3. 技术信息
  4. >
  5. 技术资讯

比 mybatis 强大优雅的 sqltoy-orm发布4.10.5,有用的小伙伴吗?

开源项目地址: https://github.com/chenrenfei/sagacity-sqltoy

感受sqltoy之美: https://chenrenfei.github.io/sqltoy/#/

更新内容:

1、缓存翻译对应的缓存更新机制增加增量更新2、查询结果计算增加环比计算,请参见sqltoy-showcase下的QueryCaseTest类

sqltoy的代表性特性展示:

1、最优雅的sql编写模式

  • mybatis的写法(一板一眼很工程化)
 select *
from sqltoy_device_order_info t
<where>
<if test="orderId!=null">
and t.ORDER_ID=#{orderId}
</if>
<if test="authedOrganIds!=null">
and t.ORGAN_ID in
<foreach collection="authedOrganIds" item="order_id" separator="," open="(" close=")">
#{order_id}
</foreach>
</if>
<if test="staffIds!=null">
and t.STAFF_ID in
<foreach collection="staffIds" item="staff_id" separator="," open="(" close=")">
#{staff_id}
</foreach>
</if>
<if test="beginDate!=null">
and t.TRANS_DATE>=#{beginDate}
</if>
<if test="endDate!=null">
and t.TRANS_DATE<#{endDate}
</if>
</where>
  • sqltoy的写法:
select 	*
from sqltoy_device_order_info t
where #[t.ORDER_ID=:orderId]
#[and t.ORGAN_ID in (:authedOrganIds)]
#[and t.STAFF_ID in (:staffIds)]
#[and t.TRANS_DATE>=:beginDate]
#[and t.TRANS_DATE<:endDate]

2、最具特色的缓存翻译和缓存条件检索,让复杂查询最接近单表查询

1. 感受sqltoy之美--缓存翻译、缓存条件检索

  • 缓存翻译和缓存检索化繁为简---查询订单表(简化为单商品订单便于演示)

订单号客户ID商品ID下单日期商品数量商品价格订单金额订单状态业务员ID部门S0001C100011012020-03-1010300030000021001N002

  • 要求查询日期在2020年1月10日至3月20日、客户名称中含<<星云科技>>字符的全部订单信息,要求显示商品名称、客户名称、业务员姓名、部门名称、订单状态中文
  • 往常的做法:硬功夫硬碰硬系列

关联客户表做like

关联商品表查询品名

关联员工信息表显示员工名字

关联机构表显示机构名称

关联数据字典翻译状态

  • 你是这么做的吗?看一下sqltoy怎么做吧!是不是变成了单表查询,效率毫无疑问秒杀多表关联无数倍!<sql id="order_showcase"> <!-- 通过缓存对最终结果代码进行翻译,显示名称 --> <translate cache="organIdName" columns="ORGAN_NAME" /> <translate cache="staffIdName" columns="STAFF_NAME" /> <translate cache="goodsIdName" columns="GOODS_NAME" /> <translate cache="customIdName" columns="CUSTOM_NAME" /> <translate cache="dictKeyName" cache-type="ORDER_STATUS" columns="STATUS_NAME" /> <filters> <!-- 将查询参数customName通过缓存进行类似like检索获取匹配的customId数组作为查询条件 --> <cache-arg cache-name="customIdName" param="customName" alias-name="customIds" /> </filters> <value> <![CDATA[ select ORDER_ID , TOTAL_QUANTITY, TOTAL_AMT, ORGAN_ID , ORGAN_ID ORGAN_NAME,-- 缓存翻译 STAFF_ID , STAFF_ID STAFF_NAME, SIGN_TIME, CUSTOM_ID, CUSTOM_ID CUSTOM_NAME, GOODS_ID , GOODS_ID GOODS_NAME, STATUS, STATUS STATUS_NAME from od_order_info t1 where #[SIGN_TIME>=:beginTime] #[and SIGN_TIME <=:endTime] -- 这里就是缓存条件检索 #[and CUSTOM_ID in (:customIds)] ]]> </value> </sql>

3、最高层级的分页查询优化:@fast 实现先分页后关联,page-optimize 将分页2次查询变成1.4次查询。

<!-- 快速分页和分页优化演示 -->
<sql id="sqltoy_fastPage">
<!-- 分页优化器,通过缓存实现查询条件一致的情况下在一定时间周期内缓存总记录数量,从而无需每次查询总记录数量 -->
<!-- alive-max:最大存放多少个不同查询条件的总记录量; alive-seconds:查询条件记录量存活时长(比如120秒,超过阀值则重新查询) -->
<page-optimize alive-max="100" alive-seconds="120" />
<value>
<![CDATA[
select t1.*,t2.ORGAN_NAME
-- @fast() 实现先分页取10条(具体数量由pageSize确定),然后再关联
from @fast(select t.*
from sqltoy_staff_info t
where t.STATUS=1
#[and t.STAFF_NAME like :staffName]
order by t.ENTRY_DATE desc
) t1
left join sqltoy_organ_info t2 on t1.organ_id=t2.ORGAN_ID
]]>
</value>

<!-- 这里为极特殊情况下提供了自定义count-sql来实现极致性能优化 -->
<!-- <count-sql></count-sql> -->
</sql>