28
28
import org .apache .ibatis .reflection .SystemMetaObject ;
29
29
import org .apache .ibatis .type .TypeHandler ;
30
30
import tk .mybatis .mapper .mapperhelper .EntityHelper ;
31
+ import tk .mybatis .mapper .util .StringUtil ;
31
32
32
33
import java .util .*;
33
34
@@ -56,6 +57,8 @@ public class Example implements IDynamicTableName {
56
57
protected Map <String , EntityColumn > propertyMap ;
57
58
//动态表名
58
59
protected String tableName ;
60
+
61
+ protected OrderBy orderBy ;
59
62
/**
60
63
* 默认exists为true
61
64
*
@@ -92,6 +95,7 @@ public Example(Class<?> entityClass, boolean exists, boolean notNull) {
92
95
for (EntityColumn column : table .getEntityClassColumns ()) {
93
96
propertyMap .put (column .getProperty (), column );
94
97
}
98
+ this .orderBy = new OrderBy (this , propertyMap );
95
99
}
96
100
97
101
public Class <?> getEntityClass () {
@@ -106,6 +110,65 @@ public void setOrderByClause(String orderByClause) {
106
110
this .orderByClause = orderByClause ;
107
111
}
108
112
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
+
109
172
public Set <String > getSelectColumns () {
110
173
return selectColumns ;
111
174
}
0 commit comments