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();
} }

  

最新文章

  1. PropertyGrid控件由浅入深(一):文章大纲
  2. Elasticsearch索引自动套用模板
  3. CSS3动画几个平时没注意的属性
  4. centos7 docker activemq
  5. object-c 系列教程
  6. Jquery DataTables warning : Requested unknown from the data source for row 0
  7. Appium官网Introduction
  8. Hibernate 关于load和get方法区别
  9. 第一个OC类、解析第一个OC程序
  10. python : 批量下载R语言库包
  11. STM8单片机启动流程彻底探究--基于IAR开发环境
  12. 手把手 学习Git
  13. Hadoop 2.6.0-cdh5.4.0集群环境搭建和Apache-Hive、Sqoop的安装
  14. 转://oracle 软件的收费模式
  15. Outsider(HNOI2019)
  16. Centos 7最小化Mongodb部署操作
  17. ESP32 environment ubuntu
  18. sql注入(一)
  19. Spring 注解原理(三)@Qualifier @Value
  20. C#编程(五十七)----------位数组

热门文章

  1. MySQL常用的数据类型及函数_20160920
  2. poj3666序列对应——DP
  3. ie下的布局(layout)和拥有布局(hasLayout)
  4. JavaScript高级程序设计学习笔记第五章--引用类型
  5. In-App Purchase Configuration Guide for iTunes Connect---(一)----Introduction
  6. jq操作select集合
  7. Spring:JdbcTemplate使用指南
  8. dede问答汉字变星号
  9. [51nod] 1007 正整数分组 dp
  10. 最优化理论-Simplex线性规划