写点什么

Magician has released a new version

发布于: 3 小时前

This update is mainly to upgrade Magician-Web and Magician-JDBC.


The changes are as follows:


  1. Fix the problem that the wildcard of magician-Web interceptor is too strict

  2. Enhance the sql helper of Magician-JDBC

The interceptor wildcard is too strict

In the previous version, if our interface URL is like this:

[/Api/order/list, /api/order/create, /api/lottery/list]


If you want to intercept all interfaces beginning with api, you can definitely consider configuring in the interceptor

@Interceptor(pattern = "/api/*")public class DemoInterceptor implements MagicianInterceptor {}
复制代码

Unfortunately, the previous version does not support it. You must either configure [pattren="/api/order/"] to intercept all interfaces beginning with [/api/order/], or configure [pattren="/api/lottery/ "] Intercept all interfaces beginning with [/api/lottery/], or configure like the following

@Interceptor(pattern = "/api/*/*")public class DemoInterceptor implements MagicianInterceptor {}
复制代码

But this will be a bit problematic. It is not flexible enough. It is equivalent to limiting the number of URL levels. It must be equal to 3 levels, not more or less. So the new version fixes this small problem, now it can be configured like this

@Interceptor(pattern = "/api/*")public class DemoInterceptor implements MagicianInterceptor {}
复制代码

Enhanced SqlBuilder of Magician-JDBC

Let me take a query as an example. Previously the where condition must be like this

String sql = SqlBuilder                .select("lottery_order_list")                .column(OrderPO.class)                .where("user_id = #{user_id} and pay_status = #{pay_status} and lottery_status = #{lottery_status} order by create_time desc")                .builder();
复制代码

There are three problems with writing like this


  1. The parameter of the where method is too long and difficult to read

  2. If the conditions require dynamic splicing, you have to define a StringBuffer yourself

  3. The last group by, order by, limit, etc. are nested with where, which is a bit strange


So in this version, this area has been enhanced, and the example above is still the same, we can change it to this

String sql = SqlBuilder                .select("lottery_order_list")                .column(OrderPO.class)                // where 可以传入多次,SqlBuilder会自动拼接                .where("user_id = #{user_id}")                .where("and pay_status = #{pay_status}")                .where("and lottery_status = #{lottery_status}")                // order by 和 where分开了                .end("order by create_time desc")                .builder();
复制代码

Where can be passed in multiple times, and SqlBuilder will automatically splice these conditions. If written in this way, line by line is very clear, and if you encounter the need to dynamically spell sql, you can do this

Select select = SqlBuilder                .select("lottery_order_list")                .column(OrderPO.class);                // The condition in if is just an example, java does not have this kind of syntaxif(user_id not null){    select.where("user_id = #{user_id}");}if(pay_status not null){    select.where("and pay_status = #{pay_status}");}if(lottery_status not null){    select.where("and lottery_status = #{lottery_status}");}           select.end("order by create_time desc");
String sql = select.builder();
复制代码


Let's talk about a little episode here. The sql assistant should be able to understand it literally. It is just an assistant for writing sql, not the design idea of sql coding, let alone the JPA set of things. He is just a tool to assist in writing sql. And only for the addition, deletion and modification of a single table.


Magician-JDBC has followed the design direction of writing SQL from the beginning. All peripheral tools are for writing SQL more conveniently. Don't understand it wrongly, ^_^.


Magician official website: https://www.magician-io.com

发布于: 3 小时前阅读数: 6
用户头像

还未添加个人签名 2020.12.08 加入

还未添加个人简介

评论

发布
暂无评论
Magician has released a new version