參考:github, https://github.com/liuxiaochen0625/MyBatis-Spring-Boot-master.git 從controller組裝tk.mybatis.mapper.entity.Example 對象,操作起來較為麻煩,不符合我們日常書寫習慣,因而改 ...
參考:github, https://github.com/liuxiaochen0625/MyBatis-Spring-Boot-master.git
從controller組裝tk.mybatis.mapper.entity.Example 對象,操作起來較為麻煩,不符合我們日常書寫習慣,因而改造一下。
調用方法:
WhereBuilder builder = new WhereBuilder(UserInfo.class); Example example = builder.and("username", OP.EQ, username).or("username", OP.EQ, "tom").and("password", OP.EQ, password).build();
鏈式操作,是不是很方便?~
具體WhereBuilder構造是參考android xutils的WhereBuilder和StringBuffer寫的,如下:
public class WhereBuilder { private final Example example; //定義examle對象,用於返回 public WhereBuilder(Class<?> tClass){this.example = new Example(tClass);}//構造方法中傳入Class參數,實例化example public WhereBuilder and(String columnName,String op,Object value){ //and Example.Criteria criteria = example.createCriteria(); assembleParams(criteria,columnName,op,value); return this; } public WhereBuilder or(String columnName,String op,Object value){ //or Example.Criteria criteria = example.createCriteria(); assembleParams(criteria,columnName,op,value); example.or(criteria); //or 需要example調用or(Example.Criteria criteria) 方法 return this; } private void assembleParams(Example.Criteria criteria,String columnName,String op,Object value){ //組裝參數 switch (op) { case OP.LIKE: if (!CommonUtils.isEmpty(value)) { criteria.andLike(columnName, "%" + value + "%"); } break; case OP.LEFT_LIKE: if (!CommonUtils.isEmpty(value)) { criteria.andLike(columnName, "%" + value); } break; case OP.LIKE_RIGHT: if (!CommonUtils.isEmpty(value)) { criteria.andLike(columnName, value + "%"); } break; case OP.EQ: if (!CommonUtils.isEmpty(value)) { criteria.andEqualTo(columnName, value); } break; case OP.NE: if (!CommonUtils.isEmpty(value)) { criteria.andNotEqualTo(columnName, value); } break; case OP.GT: if (!CommonUtils.isEmpty(value)) { criteria.andGreaterThan(columnName, value); } break; case OP.NL: if (!CommonUtils.isEmpty(value)) { criteria.andGreaterThanOrEqualTo(columnName, value); } break; case OP.LT: if (!CommonUtils.isEmpty(value)) { criteria.andLessThan(columnName, value); } break; case OP.NG: if (!CommonUtils.isEmpty(value)) { criteria.andLessThanOrEqualTo(columnName, value); } break; case OP.NULL: criteria.andIsNull(columnName); break; case OP.NOTNULL: criteria.andIsNotNull(columnName); break; case OP.IN: if (!CommonUtils.isEmpty(value)) { if (value instanceof ArrayList) criteria.andIn(columnName, (ArrayList) value); } break; case OP.BETWEEN: if (!CommonUtils.isEmpty(value)) { if (value instanceof ArrayList) { criteria.andBetween(columnName, ((ArrayList) value).get(0), ((ArrayList) value).get(1)); } } break; case OP.NOTBETWEEN: if (!CommonUtils.isEmpty(value)) { if (value instanceof ArrayList) { criteria.andNotBetween(columnName, ((ArrayList) value).get(0), ((ArrayList) value).get(1)); } } break; case OP.NOTIN: if (!CommonUtils.isEmpty(value)) { if (value instanceof ArrayList) criteria.andNotIn(columnName, (ArrayList) value); } break; default: } } public Example build(){ //返回example實例 return example; } }