SpringSide
很好很强大,整合了各式主流的、实用的、好玩的开源项目,非常值的学习。springSide3中有一个功能很实用、很方便、就是页面条件过滤(搜索)功能。由于springSide该功能目前只支持String类型的搜索,但在项目中仅仅只有String类型是远远不够的,所以自己就将其功能做了一些扩展。
在扩展中需考滤以下几个问题:
1、需将PropertyFilter类中的枚举MatchType扩展更多的属性比较类型。
2、在构建Criterion对象时,由于由于PropertyFilte中的过滤的属性值都是通过request.getParameter(name)方式所获取,所以需将String类型的值转换成与过滤属性类型一致。
3、要将String类型进行转换,首先需获得过滤属性的类型,属性单一时很好处理,如果为链方式时(如A.B.C.id),怎样处理?怎样来获取最后一个属性类型(id的类型),又假如属性是继承父类而来的又将怎样处理。
围绕以上3点,我主要做了以下扩展:
一、扩展PropertyFilter类中的枚举MatchType
public enum MatchType {
EQ, LIKE, LT, LE, GT, GE;
}
添加了小于、小于等于、大于、大于等于.
二、在ReflectionUtils类中添加获取过滤属性的类型方法
public static Class<?> getFieldType(Class<?> entityClass, String propertyName) {
Assert.hasText(propertyName, "propertyName不能为空");
Class<?> propertyType = null;
try {
if (StringUtils.contains(propertyName, ".")) {
for (String str : propertyName.split("\\.")) {
Field declaredField = getDeclaredField(entityClass, str);
entityClass = declaredField.getType();
}
propertyType = entityClass;
} else {
propertyType = getDeclaredField(entityClass, propertyName).getType();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return propertyType;
}
三、在HibernateWebUtils类中添加值类型转换方法
public static Object convertValue(Object value, Class<?> toType) {
Assert.notNull(value, "value不能为空");
Object result = null;
try {
if (toType == Date.class) {
result = DateUtils.parseDate((String) value, new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
} else {
result = OgnlOps.convertValue(value, toType);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
public static Object convertValue(Object value, Class<?> entityClass, String propertyName) {
return convertValue(value, ReflectionUtils.getFieldType(entityClass, propertyName));
}
四、重构HibernateDao类中的buildPropertyFilterCriterion方法
protected Criterion buildPropertyFilterCriterion(final String propertyName, final Object value,
final MatchType matchType) {
Assert.hasText(propertyName, "propertyName不能为空");
Criterion criterion = null;
Object pValue = null;
try {
pValue = HibernateWebUtils.convertValue(value, entityClass, propertyName);
if (MatchType.EQ.equals(matchType)) {
criterion = Restrictions.eq(propertyName, pValue);
}
if (MatchType.LIKE.equals(matchType)) {
criterion = Restrictions.like(propertyName, (String) value, MatchMode.ANYWHERE);
}
if (MatchType.LT.equals(matchType)) {
criterion = Restrictions.lt(propertyName, pValue);
}
if (MatchType.LE.equals(matchType)) {
criterion = Restrictions.le(propertyName, pValue);
}
if (MatchType.GT.equals(matchType)) {
criterion = Restrictions.gt(propertyName, pValue);
}
if (MatchType.GE.equals(matchType)) {
criterion = Restrictions.ge(propertyName, pValue);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return criterion;
}
分享到:
相关推荐
封装SpringSide扩展功能的Hibernat DAO泛型基类. 扩展功能包括分页查询,按属性过滤条件列表查询. 可在Service层直接使用,也可以扩展泛型DAO子类使用,见两个构造函数的注释.
springside-3.2.2源码springside-3.2.2源码
springside
SpringSide4.0 Reference Manual 自己整理的官方文档,PDF格式,方便查阅,所有连接都已整理。
springside4
springside3.0.zip
pom.xml配置 ...mvn install:install-file -DgroupId=org.springside -DartifactId=springside-core -Dversion=4.2.2.GA -Dfile=./springside-core-4.2.2.GA.jar -Dpackaging=jar -DgeneratePom=true
SpringSide文档.chm。
springside开发全面讲解 springside集成了多项技术,不适合入门者学习,因此本资料也尽量从最简单最基础的讲起。
本版本是在myeclipse 下集成 springside3.3的完整功能代码。包含数据.sql
分享 江南白衣的 springside4 非常强大的Javaee 框架。 官网:http://www.springside.org.cn/ 代码:https://github.com/springside/springside4
springside3.3.4 使用方法 ssh整合 最流行的web框架 使用说明
springside示例quickstart的eclipse工程
Springside4 学习整理 Shiro 服务端验证
SpringSide对Hibernate做了三层封装: 第一层:HibernateGenericDao,基于spring的HibernateDaoSupport,但加入了分页函数与各种...第三层:HibernateExtendDao,基于HibernateEntityDao,主要扩展各种选择性的功能。
SpringSide
springside-core-4.3.0.jar 最新版springside-core的jar包
springside3-core-3.3.4.jar,springside的3.3.4版本。
最近公司要求高maven+springside管理项目,凑合学,总结了一下
springside-joddform-2.0.zip