Skip to content

Commit abee7d4

Browse files
committed
通过orderBy方法,使用属性进行排序
1 parent 4a529c5 commit abee7d4

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/main/java/tk/mybatis/mapper/entity/Example.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.ibatis.reflection.SystemMetaObject;
2929
import org.apache.ibatis.type.TypeHandler;
3030
import tk.mybatis.mapper.mapperhelper.EntityHelper;
31+
import tk.mybatis.mapper.util.StringUtil;
3132

3233
import java.util.*;
3334

@@ -56,6 +57,8 @@ public class Example implements IDynamicTableName {
5657
protected Map<String, EntityColumn> propertyMap;
5758
//动态表名
5859
protected String tableName;
60+
61+
protected OrderBy orderBy;
5962
/**
6063
* 默认exists为true
6164
*
@@ -92,6 +95,7 @@ public Example(Class<?> entityClass, boolean exists, boolean notNull) {
9295
for (EntityColumn column : table.getEntityClassColumns()) {
9396
propertyMap.put(column.getProperty(), column);
9497
}
98+
this.orderBy = new OrderBy(this, propertyMap);
9599
}
96100

97101
public Class<?> getEntityClass() {
@@ -106,6 +110,65 @@ public void setOrderByClause(String orderByClause) {
106110
this.orderByClause = orderByClause;
107111
}
108112

113+
public OrderBy orderBy(String property) {
114+
this.orderBy.orderBy(property);
115+
return this.orderBy;
116+
}
117+
118+
public static class OrderBy {
119+
private Example example;
120+
private Boolean isProperty;
121+
//属性和列对应
122+
protected Map<String, EntityColumn> propertyMap;
123+
protected boolean notNull;
124+
125+
public OrderBy(Example example, Map<String, EntityColumn> propertyMap) {
126+
this.example = example;
127+
this.propertyMap = propertyMap;
128+
}
129+
130+
private String property(String property) {
131+
if (propertyMap.containsKey(property)) {
132+
return propertyMap.get(property).getColumn();
133+
} else if (notNull) {
134+
throw new RuntimeException("当前实体类不包含名为" + property + "的属性!");
135+
} else {
136+
return null;
137+
}
138+
}
139+
140+
public OrderBy orderBy(String property) {
141+
String column = property(property);
142+
if (column == null) {
143+
isProperty = false;
144+
return this;
145+
}
146+
if (StringUtil.isNotEmpty(example.getOrderByClause())) {
147+
example.setOrderByClause(example.getOrderByClause() + "," + column);
148+
} else {
149+
example.setOrderByClause(column);
150+
}
151+
isProperty = true;
152+
return this;
153+
}
154+
155+
public OrderBy desc() {
156+
if (isProperty) {
157+
example.setOrderByClause(example.getOrderByClause() + " DESC");
158+
isProperty = false;
159+
}
160+
return this;
161+
}
162+
163+
public OrderBy asc() {
164+
if (isProperty) {
165+
example.setOrderByClause(example.getOrderByClause() + " ASC");
166+
isProperty = false;
167+
}
168+
return this;
169+
}
170+
}
171+
109172
public Set<String> getSelectColumns() {
110173
return selectColumns;
111174
}

src/test/java/tk/mybatis/mapper/test/example/TestSelectByExample.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void testSelectColumnsByExample() {
152152
try {
153153
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
154154
Example example = new Example(Country.class);
155-
example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
155+
example.createCriteria().andGreaterThan("id", 100).andLessThan("id", 151);
156156
example.or().andLessThan("id", 41);
157157
example.selectProperties("id", "countryname", "hehe");
158158
List<Country> countries = mapper.selectByExample(example);
@@ -169,7 +169,8 @@ public void testOrderBy() {
169169
try {
170170
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
171171
Example example = new Example(Country.class);
172-
example.setOrderByClause("id desc");
172+
// example.setOrderByClause("id desc");
173+
example.orderBy("id").desc().orderBy("countryname").orderBy("countrycode").asc();
173174
List<Country> countries = mapper.selectByExample(example);
174175
//查询总数
175176
Assert.assertEquals(183, (int) countries.get(0).getId());

0 commit comments

Comments
 (0)