1.注解內拼寫 Mybatis SQL 腳本
@Repository public interface CustomerFeedMapper extends BaseCrudMapper<CustomerFeed> { @Select("<script>" + "SELECT customer_id,COUNT(customer_id) total " + "FROM t_customer_feed " + "<where> " + "<if test='feedQO.feedTypes != null and feedQO.feedTypes.size()>0'> " + "type IN " + "<foreach item='type' collection='feedQO.feedTypes' open='(' close=')' separator=','> " + "#{type}" + "</foreach> AND " + "</if>" + "<if test='feedQO.customerIds != null and feedQO.customerIds.size()>0'> " + "customer_id IN " + "<foreach item='customerId' collection='feedQO.customerIds' open='(' close=')' separator=','> " + "#{customerId}" + "</foreach> AND " + "</if>" + "<if test='feedQO.timePoint != null'>" + "create_time > #{feedQO.timePoint} " + "</if>" + "</where>" + "GROUP BY customer_id " + "ORDER BY create_time DESC " + "</script>") @Results({ @Result(property = "customerId", column = "customer_id"), @Result(property = "total", column = "total") }) List<CustomerFeedSummaryVO> summary(@Param("feedQO") CustomerFeedQO feedQO); }
2.基於org.apache.ibatis.jdbc.SQL對象構建SQL
Mapper 接口
在 mapper 接口的方法上添加注解 @SelectProvider 配置其兩個屬性 type (構建SQL的類)和 method (構建 SQL 的類中的方法)
@Repository public interface UserCustomerRelationMapper extends BaseCrudMapper<UserCustomerRelation> { /** * Page by customer attrs list. * @param userCustomerRelationQO the user customer relation qo * @return the list */ @SelectProvider(type = UserCustomerRelationProvider.class, method = "listByCustomerAttr") List<UserCustomerRelation> pageByCustomerAttrs(@Param("condition") UserCustomerRelationCondition userCustomerRelationQO); }
electProvider 類實現
此 Provider 類無需繼承實現其他類,只要實現接口方法中注解 @SelectProvider 的 method 屬性指定的方法 listByCustomerAttr ,Mapper 接口中的參數,將以 Map<String,Object> 的形式傳入我們實現的指定方法。
public class UserCustomerRelationProvider { /** * List by customer attr string. * @param params the params * @return the string */ public String listByCustomerAttr(Map<String, Object> params) { UserCustomerRelationCondition qo = (UserCustomerRelationCondition) params.get("condition"); SQL querySql = new SQL(); querySql.SELECT("ucr.user_id as userId,ucr.customer_id as customerId,ucr.create_time as createTime,ucr.update_time as updateTime") .FROM("t_user_customer_relation ucr", "t_customer_attr ca") .WHERE("ucr.customer_id=ca.objectId"); String userId = qo.getUserId(); if (StringUtils.isNotBlank(userId)) { querySql.WHERE("ucr.user_id=#{condition.userId}"); } Long customerId = qo.getCustomerId(); if (customerId != null) { querySql.WHERE("ucr.customer_id=#{condition.customerId}"); } List<CustomerAttr> customerAttrs = qo.getCustomerAttrs(); if (!CollectionUtils.isEmpty(customerAttrs)) { for (CustomerAttr customerAttr : customerAttrs) { String key = customerAttr.getKey(); if (StringUtils.isNotBlank(key)) { querySql.WHERE(String.format("ca.`key`='%s'", key)); } String value = customerAttr.getValue(); if (StringUtils.isNotBlank(value)) { querySql.WHERE(String.format("ca.`value`='%s'", value)); } } } return querySql.toString(); } }