Skip to content

Commit

Permalink
单表排序
Browse files Browse the repository at this point in the history
  • Loading branch information
qrqhuang committed Nov 10, 2018
1 parent 6d1ce6e commit 4abde55
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 13 deletions.
2 changes: 2 additions & 0 deletions base/src/test/java/tk/mybatis/mapper/model/Country.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package tk.mybatis.mapper.model;

import tk.mybatis.mapper.annotation.Order;
import tk.mybatis.mapper.entity.IDynamicTableName;

import javax.persistence.Column;
Expand All @@ -40,6 +41,7 @@ public class Country extends Entity<Integer, String> implements Serializable, ID
private static final long serialVersionUID = -1626761012846137805L;
List<Country> list;
@Column
@Order(value = "DESC", priority = 2)
private String countryname;
private String countrycode;
@Transient
Expand Down
5 changes: 3 additions & 2 deletions base/src/test/java/tk/mybatis/mapper/model/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

package tk.mybatis.mapper.model;

import tk.mybatis.mapper.annotation.Order;

import javax.persistence.Id;
import javax.persistence.OrderBy;
import javax.persistence.Transient;
import java.io.Serializable;

Expand All @@ -37,6 +38,7 @@ public class Entity<ID extends Serializable, NAME extends Serializable> {
//这里的a,b,c,d仅用来测试FieldHelper中的静态字段
private static Integer a, b, c, d;

@Order(priority = 1)
private ID id;

@Transient
Expand All @@ -47,7 +49,6 @@ public ID getId() {
return id;
}

@OrderBy("desc")
public void setId(ID id) {
this.id = id;
}
Expand Down
27 changes: 27 additions & 0 deletions core/src/main/java/tk/mybatis/mapper/annotation/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tk.mybatis.mapper.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @description: 字段排序
* @author: qrqhuangcy
* @date: 2018-11-11
**/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Order {
/**
* 升降序
* @return
*/
String value() default "ASC";

/**
* 优先级, 值小的优先
* @return
*/
int priority() default 1;
}
10 changes: 10 additions & 0 deletions core/src/main/java/tk/mybatis/mapper/entity/EntityColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class EntityColumn {
private String generator;
//排序
private String orderBy;
private Integer orderPriority;
//可插入
private boolean insertable = true;
//可更新
Expand Down Expand Up @@ -343,6 +344,14 @@ public void setUseJavaType(boolean useJavaType) {
this.useJavaType = useJavaType;
}

public Integer getOrderPriority() {
return orderPriority;
}

public void setOrderPriority(Integer orderPriority) {
this.orderPriority = orderPriority;
}

@Override
public String toString() {
return "EntityColumn{" +
Expand All @@ -357,6 +366,7 @@ public String toString() {
", blob=" + blob +
", generator='" + generator + '\'' +
", orderBy='" + orderBy + '\'' +
", orderPriority='" + orderPriority + '\'' +
", insertable=" + insertable +
", updatable=" + updatable +
", order=" + order +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import tk.mybatis.mapper.mapperhelper.resolve.DefaultEntityResolve;
import tk.mybatis.mapper.mapperhelper.resolve.EntityResolve;

import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -81,15 +80,34 @@ public static String getOrderByClause(Class<?> entityClass) {
if (table.getOrderByClause() != null) {
return table.getOrderByClause();
}
StringBuilder orderBy = new StringBuilder();

List<EntityColumn> orderEntityColumns = new ArrayList<EntityColumn>();
for (EntityColumn column : table.getEntityClassColumns()) {
if (column.getOrderBy() != null) {
if (orderBy.length() != 0) {
orderBy.append(",");
}
orderBy.append(column.getColumn()).append(" ").append(column.getOrderBy());
orderEntityColumns.add(column);
}
}

Collections.sort(orderEntityColumns, new Comparator<EntityColumn>() {
@Override
public int compare(EntityColumn o1, EntityColumn o2) {
return o1.getOrderPriority() - o2.getOrderPriority();
}
});

StringBuilder orderBy = new StringBuilder();
for (EntityColumn column : orderEntityColumns) {
if (orderBy.length() != 0) {
orderBy.append(",");
}
orderBy.append(column.getColumn()).append(" ").append(column.getOrderBy());
}

// String orderBy = table.getEntityClassColumns().stream().filter(t -> t.getOrder() != null)
// .sorted(Comparator.comparing(EntityColumn::getOrderPriority))
// .map(t -> t.getColumn() + " " + t.getOrder())
// .collect(Collectors.joining(","));

table.setOrderByClause(orderBy.toString());
return table.getOrderByClause();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tk.mybatis.mapper.annotation.ColumnType;
import tk.mybatis.mapper.annotation.KeySql;
import tk.mybatis.mapper.annotation.NameStyle;
import tk.mybatis.mapper.annotation.Order;
import tk.mybatis.mapper.code.IdentityDialect;
import tk.mybatis.mapper.code.ORDER;
import tk.mybatis.mapper.code.Style;
Expand Down Expand Up @@ -170,13 +171,18 @@ protected void processField(EntityTable entityTable, EntityField field, Config c
* @param entityColumn
*/
protected void processOrderBy(EntityTable entityTable, EntityField field, EntityColumn entityColumn) {
if (field.isAnnotationPresent(OrderBy.class)) {
OrderBy orderBy = field.getAnnotation(OrderBy.class);
if ("".equals(orderBy.value())) {
if(field.isAnnotationPresent(OrderBy.class)){
throw new MapperException(OrderBy.class + " is outdated, use " + Order.class + " instead!");
}

if (field.isAnnotationPresent(Order.class)) {
Order order = field.getAnnotation(Order.class);
if ("".equals(order.value())) {
entityColumn.setOrderBy("ASC");
} else {
entityColumn.setOrderBy(orderBy.value());
entityColumn.setOrderBy(order.value());
}
entityColumn.setOrderPriority(order.priority());
}
}

Expand Down

0 comments on commit 4abde55

Please sign in to comment.