吐槽mybatis+熊猫哥优化教程
2012 年 12 月 10 日
吐槽mybatis+熊猫哥优化教程
我将使用反射机制+自定义注解,该方法是由一个比我厉害的10倍的人教我的,感谢。百忙之中,水一篇 ,该文需要一定编程基础的人看
一,吐槽mybatis
我先吐槽这个动态条件sql,不知道mybatis那帮人为什么不能优化,可能是他们看这样的代码习惯了。反正我看到下面的代码,就感觉在看一堆shit,不仅辣眼睛,维护起来也是相当操蛋,是真的操蛋
AND a.email like "%"#{customer.email}"%" AND d.sales_name = #{customer.salesName} AND a.first_name like "%"#{customer.firstName}"%" AND a.last_name like "%"#{customer.lastName}"%" AND a.whatapp like "%"#{customer.whatapp}"%" AND a.country = #{customer.country} AND a.province like "%"#{customer.province}"%" AND a.order_count like "%"#{customer.orderCount}"%" AND a.order_amount = #{customer.orderAmount} AND a.level_id = #{customer.levelId} AND s.last_order_date = ]]> #{customer.lastLoginDateStartDate} AND s.last_order_date <![CDATA[ #{customer.lastLoginDateEndDate} AND s.last_login_date = ]]> #{customer.lastOrderDateStartDate} AND s.last_login_date <![CDATA[ #{customer.lastOrderDateEndDate} AND s.create_at = ]]> #{customer.createAtStartDate} AND s.create_at <![CDATA[ #{customer.createAtEndDate}
二,优化
安静看代码,上面一堆shit一样的代码,我已经优化为下面这样,变量名a,是不是干净又清爽,维护起来也是相当简单
${a}
熊猫哥优化教程
我将使用反射机制+自定义注解
第一:自定义注解annotation JewelryAlias
/** * @program:ant * @author:aodeng * @blog:低调小熊猫(https://aodeng.cc) * @微信公众号:低调小熊猫 * @create:2019-06-04 18:41 **/ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface JewelryAlias { String value() default ""; String column() default ""; String type() default ""; }
第二:封装工具:JewelryAnnotationUtil
/** * @author:aodeng(低调小熊猫) * @Description: TODO * @Date: 19-6-4 **/ public class JewelryAnnotationUtil { private JewelryAnnotationUtil() { } public static List getCondition(Object obj) throws IllegalAccessException { List result = new ArrayList(); System.out.println(obj.getClass()+"=============="); Field[] fieldList = obj.getClass().getSuperclass().getDeclaredFields(); for (Field field : obj.getClass().getDeclaredFields()) { field.setAccessible(true); if (field.isAnnotationPresent(JewelryAlias.class) && (field.get(obj) != null)) { JewelryAlias j = field.getAnnotation(JewelryAlias.class); StringBuilder s = new StringBuilder(); s.append(!StrUtil.isBlank(j.value()) ? j.value().concat(".") : ""); s.append(!StrUtil.isBlank(j.column()) ? j.column() : field.getName()); switch (j.type()) { case "=": s.append(" = "); if(field.getType().equals(String.class)){ s.append("'").append(field.get(obj)).append("'"); }else{ s.append(field.get(obj)); } break; case "like": s.append(" like "); if(field.getType().equals(String.class)){ s.append("'").append("%").append(field.get(obj)).append("%").append("'"); }else{ s.append("%").append(field.get(obj)).append("%"); } break; default: break; } result.add(s.toString()); } } return result; } }
举个例子
controller:
@RequestMapping(value = "/getOrderListByPage", method = RequestMethod.POST) public SuccessData getOrderListByPage(@RequestBody ConditionalQueryVo queryVo) throws IllegalAccessException{ if (ObjectUtil.isNull(queryVo.getConditionalObject())){ queryVo.setConditionalObject(new SalesOrderQueryVo()); } return CheckSqlStatus.getSuccessData(orderService.getOrderListByPage(JewelryAnnotationUtil.getCondition(queryVo.getConditionalObject()), queryVo.getPageIndex(), queryVo.getPageSize())); }
model:
/** * @author:aodeng(低调小熊猫) * @Description: TODO * @Date: 19-6-4 **/ @Data public class SalesOrderQueryVo { @JewelryAlias(value = "sot", column = "carrier_code", type = "=") private String carrierCode; @JewelryAlias(value = "sot", column = "track_number", type = "like") private String trackNumber; @JewelryAlias(value = "so", column = "order_increment", type = "like") private String orderIncrement; @JewelryAlias(value = "sot", column = "status", type = "=") private Integer status; }
mapper:
List getOrderListByPage(Page page,@Param("conditionList") List conditionList);
:
- My personal blog
- My WeChat public number (low-key panda)
- Hope-boot uses the GPL-v3.0 protocol to open source
- Low-key little panda QQ group
- Low-key red panda Telegram group
- My Gitee link
- My Github link
- My open source organization (welcome like-minded friends to join)
- : java@aodeng.cc – . .
-
本文作者:
低调小熊猫 -
本文链接:
https://aodeng.cc/archives/1559652374417 -
版权声明:
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0
许可协议。转载请注明出处!