在ExampleMapper的处理中,默认会使用StringTypeHander进行java-database的数据库字段映射,下面给出一套方案:
import org.apache.ibatis.type.TypeHandler;
import tk.mybatis.mapper.MapperException;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.entity.Example;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class CustomizeExample extends Example {
public CustomizeExample(Class<?> entityClass) {
super(entityClass);
}
public CustomizeExample(Class<?> entityClass, boolean exists) {
super(entityClass, exists);
}
public CustomizeExample(Class<?> entityClass, boolean exists, boolean notNull) {
super(entityClass, exists, notNull);
}
public static class CustomizeCriteria extends Example.Criteria {
protected CustomizeCriteria(Map<String, EntityColumn> propertyMap, boolean exists, boolean notNull) {
super(propertyMap, exists, notNull);
}
//TODO 方法重写区域
public Example.Criteria andEqualTo(String property, Object value,Class<? extends TypeHandler> typeHandlerClazz) {
this.addCriterion(this.column(property) + " =", value, this.property(property),typeHandlerClazz);
return this;
}
public Example.Criteria andIn(String property, Iterable values,Class<? extends TypeHandler> typeHandlerClazz) {
this.addCriterion(this.column(property) + " in", values, this.property(property),typeHandlerClazz);
return this;
}
//TODO 方法重写区域截止
protected void addCriterion(String condition, Object value, String property,Class<? extends TypeHandler> typeHandlerClazz) {
if (value == null) {
if (this.notNull) {
throw new MapperException("Value for " + property + " cannot be null");
}
} else if (property != null) {
this.criteria.add(new CustomizeExample.CustomizeCriterion(condition, value,typeHandlerClazz.getName()));
}
}
private String column(String property) {
if (this.propertyMap.containsKey(property)) {
return ((EntityColumn)this.propertyMap.get(property)).getColumn();
} else if (this.exists) {
throw new MapperException("当前实体类不包含名为" + property + "的属性!");
} else {
return null;
}
}
private String property(String property) {
if (this.propertyMap.containsKey(property)) {
return property;
} else if (this.exists) {
throw new MapperException("当前实体类不包含名为" + property + "的属性!");
} else {
return null;
}
}
}
public CustomizeExample.CustomizeCriteria createCriteria() {
CustomizeExample.CustomizeCriteria criteria = new CustomizeCriteria(this.propertyMap, this.exists, this.notNull);
if (this.oredCriteria.size() == 0) {
criteria.setAndOr("and");
this.oredCriteria.add(criteria);
}
return criteria;
}
public static class CustomizeCriterion extends Example.Criterion{
protected CustomizeCriterion(String condition, Object value, String typeHandler) {
super(condition, value, typeHandler);
}
}
}
application配置mybatis的typehander扫描:
mybatis.type-handlers-package=com.xxx typehandler实现类所在包名
使用:
CustomizeExample example = new CustomizeExample(TicketXygxxlr.class); UUID u2 = UUID.fromString("5830abda-fde2-4952-b57b-e6ac3275316b"); List<UUID> uuids = new ArrayList<>(); uuids.add(u2); example.createCriteria().andIn("oid",uuids,UUIDTypeHandler.class).orEqualTo("id",3); List<TestModel> list = testModelMapper.selectByExample(example);