JavaWeb unified query interface (jfinal)

I have found all my controller queries should call two methods getById service layer and roughly the same parameters getByKey

The time to write basically copy paste. . . So the old idea prompted a large number of duplicate code good Fana so he improved a bit

All service related query interface to achieve a QueryService

Interface defines two methods

1 import com.jfinal.plugin.activerecord.Page;
2 
3 public interface QueryService<T> {
4 
5     Page<T> getById(String id, String flag);
6 
7     Page<T> getByKey(int page, int pageSize, String value, String flag);
8 }

Be sure to use a generic error-prone ah otherwise strong turn of this ghosts know which way I just call

Then layer those service parameters are unified look

Then write a static method in a common controller in the annotation should not write anyway controller does not recognize static methods

 1 @NotAction
 2 static <T> Page<T> query(QueryService<T> service,Controller controller, String key, String value, String flag, int page, int pageSize) {
 3     value = value == null ? "" : value;
 4     Page<T> list;
 5     if ("id".equals(key)) {
 6         list = service.getById(value, flag);
 7     } else if ("key".equals(key)) {
 8         list = service.getByKey(page, pageSize, value, flag);
 9     } else {
10         list = null;
11     }
12     if (list == null) {
13         controller.renderJson("result", "{'code':'203','info':'unknown key'}");
14     } else {
15         controller.renderJson("result", "{'code':'103','info':" + Json.getJson().toJson(list) + "}");
16     }
17     return list;
18 }

The first time with a write-in controller objects in the service call, this service must be achieved QueryService interfaces, on the second pass this, because the end of the query to be passed to the front desk, I was lazy, they are written here

If you render the words here do not write the second argument put deleted, the method 12-16 business code lines back into the controller's write

Finally, return list, I do not know I think doing so I want to open a deleted forget to put the return value

Guess you like

Origin www.cnblogs.com/panther1942/p/11127314.html