吐槽mybatis+熊貓哥優化教程
吐槽mybatis+熊貓哥優化教程
我將使用反射機制+自定義註解,該方法是由一個比我厲害的10倍的人教我的,感謝。百忙之中,水一篇 ,該文需要一定編程基礎的人看
一,吐槽mybatis
我先吐槽這個動態條件sql,不知道mybatis那幫人爲什麼不能優化,可能是他們看這樣的代碼習慣了。反正我看到下面的代碼,就感覺在看一堆shit,不僅辣眼睛,維護起來也是相當操蛋,是真的操蛋
<if test="customer.email != null and customer.email !=''"> AND a.email like "%"#{customer.email}"%" </if> <if test="customer.salesName != null and customer.salesName !=''"> AND d.sales_name = #{customer.salesName} </if> <if test="customer.firstName != null and customer.firstName !=''"> AND a.first_name like "%"#{customer.firstName}"%" </if> <if test="customer.lastName != null and customer.lastName !=''"> AND a.last_name like "%"#{customer.lastName}"%" </if> <if test="customer.whatapp != null and customer.whatapp !=''"> AND a.whatapp like "%"#{customer.whatapp}"%" </if> <if test="customer.country != null and customer.country !=''"> AND a.country = #{customer.country} </if> <if test="customer.province != null and customer.province !=''"> AND a.province like "%"#{customer.province}"%" </if> <if test="customer.orderCount != null and customer.orderCount !=''"> AND a.order_count like "%"#{customer.orderCount}"%" </if> <if test="customer.orderAmount != null and customer.orderAmount !=''"> AND a.order_amount = #{customer.orderAmount} </if> <if test="customer.levelId != null and customer.levelId !=''"> AND a.level_id = #{customer.levelId} </if> <if test="customer.lastLoginDateStartDate != null"> AND s.last_order_date <![CDATA[>= ]]> #{customer.lastLoginDateStartDate} </if> <if test="customer.lastLoginDateEndDate != null"> AND s.last_order_date <![CDATA[< ]]> #{customer.lastLoginDateEndDate} </if> <if test="customer.lastOrderDateStartDate != null"> AND s.last_login_date <![CDATA[>= ]]> #{customer.lastOrderDateStartDate} </if> <if test="customer.lastOrderDateEndDate != null"> AND s.last_login_date <![CDATA[< ]]> #{customer.lastOrderDateEndDate} </if> <if test="customer.createAtStartDate != null"> AND s.create_at <![CDATA[>= ]]> #{customer.createAtStartDate} </if> <if test="customer.createAtEndDate != null"> AND s.create_at <![CDATA[< ]]> #{customer.createAtEndDate} </if>
二,優化
安靜看代碼,上面一堆shit一樣的代碼,我已經優化爲下面這樣,變量名a,是不是乾淨又清爽,維護起來也是相當簡單
<where> <foreach collection="conditionList" item="a" separator="and"> ${a} </foreach> </where>
熊貓哥優化教程
我將使用反射機制+自定義註解
第一:自定義註解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<String> getCondition(Object obj) throws IllegalAccessException { List<String> 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<SalesOrderQueryVo> 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<SalesOrderModel> getOrderListByPage(Page<Object> page,@Param("conditionList") List<String> 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)
- : [email protected] - . .
- 本文作者: 低調小熊貓
- 本文鏈接: https://aodeng.cc/archives/1559652374417
- 版權聲明: 本博客所有文章除特別聲明外,均採用 CC BY-NC-SA 3.0 許可協議。轉載請註明出處!