浏览 3812 次
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2010-09-20
最后修改:2010-09-20
三、Service层 Service 主要是处理业务逻辑层的数据,传递给dao层,进行持久化,或者按条件查询。我们还是采用dao的设计思路,写一个抽象的manager层,其还有的代码如下:
package com.jxs.sys.core.base.service; import java.util.LinkedHashMap; import java.util.List; import com.jxs.sys.core.base.model.Model; import com.jxs.sys.core.base.model.ModelSet; public abstract interface Manager<T extends Model> { /** * 增加一个对象 * * @param modelT */ public abstract void add(T modelT); /** * 删除一个对象 */ public abstract List<String> delete(T modelT); /** * 删除一组对象 * * @param modelTId * 删除对象的集合 * @return 返回删除后出发的消息, */ public abstract List<String> delete(Integer[] modelTId); /** * 修改一个对象 * * @param modelT */ public abstract void modify(T modelT); /** * 查询一个对象 * * @param modelTId * model.id * @return 对象model */ public abstract T query(Integer modelTId); /** * 查询所有的对象集合 * * @return */ public abstract ModelSet<T> queryAll(); /** * 分页查询 * * @param firstindex * 首页 * @param maxresult * 数量 * @return */ public abstract ModelSet<T> queryAll(int firstindex, int maxresult); /** * * @param firstindex * @param maxresult * @param orderby * 排序 * @return */ public abstract ModelSet<T> queryAll(int firstindex, int maxresult, LinkedHashMap<String, String> orderby); /** * * @param firstindex * @param maxresult * @param wherejpql JPQL语句 * @param queryParams 参数 * @param orderby * @return */ public abstract ModelSet<T> queryAll(int firstindex, int maxresult, String wherejpql, Object[] queryParams, LinkedHashMap<String, String> orderby); /** * compass检索 * * @param queryString * @return */ public abstract List<T> search(String queryString); /** * compass分页检索 * * @param firstindex * @param maxresult * @return */ public abstract ModelSet<T> getSearchResult(int firstindex, int maxresult); /** * DisPlayTag 导出 * * @param firstindex * @param maxresult * @return */ public abstract List<T> getSearchResultForExport(int firstindex, int maxresult); } Service 层manager的支持怎么写呢? 我们还是先给出:
public abstract class ManagerSupport<T extends Model, D extends Dao<T>> implements Manager<T> {} 这样写有什么好处呢? Service层需Dao层的支持,Dao是将Model同步到数据库中的,其支持的又是Manger接口,这样讲所有的对象都给绑定在了一起,既然需要同步数据,那么传递竟来的是Dao怎么个实例化呢? 我们采用 javax.annotation.PostConstruct 这个annotation来实例化Dao,参见如下代码:
protected Dao<T> dao = null; @PostConstruct private void initDao() { if (this.dao == null) { String modelname = ReflectionUtils.getSuperClassGenricType(super.getClass()).getSimpleName(); StringBuffer daoname = new StringBuffer(); daoname.append(Character.toLowerCase(modelname.charAt(0))).append(modelname.substring(1)).append("Dao"); this.dao = ((Dao) SpringContextUtil.getBean(daoname.toString())); } } 之后我们因为实现了implements Manager<T>;所以我们只需要dao.xxx(xxx);方法就以了,参见如下代码: package com.jxs.sys.core.base.service; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import javax.annotation.PostConstruct; import javax.annotation.Resource; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; import org.perf4j.aop.Profiled; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.jxs.sys.core.base.dao.Dao; import com.jxs.sys.core.base.model.Model; import com.jxs.sys.core.base.model.ModelSet; import com.jxs.sys.core.base.utils.ReflectionUtils; import com.jxs.sys.core.base.utils.SpringContextUtil; import com.jxs.sys.core.base.utils.WebUtil; import com.jxs.sys.core.security.model.User; import com.jxs.sys.core.security.userholder.UserHolder; public abstract class ManagerSupport<T extends Model, D extends Dao<T>> implements Manager<T> { protected final Logger log = LoggerFactory.getLogger(super.getClass()); @Resource(name = "ehCache") protected Cache cache; @Resource(name = "springContextUtil") protected SpringContextUtil springContextUtil; protected Dao<T> dao = null; @PostConstruct private void initDao() { if (this.dao == null) { String modelname = ReflectionUtils.getSuperClassGenricType(super.getClass()).getSimpleName(); StringBuffer daoname = new StringBuffer(); daoname.append(Character.toLowerCase(modelname.charAt(0))).append(modelname.substring(1)).append("Dao"); this.dao = ((Dao) SpringContextUtil.getBean(daoname.toString())); } } @Profiled(tag = "addInManager", message = "model = {$0}") public void add(T model) { this.dao.add(model); } public List<String> delete(T model) { return delete(new Integer[] { model.getId() }); } @Profiled(tag = "deleteInManager", message = "modelIds = {$0}") public List<String> delete(Integer[] modelIds) { List<String> messages = new ArrayList<String>(); for (Integer id : modelIds) { try { this.dao.delete(id); } catch (Exception e) { String message = "删除模型失败,模型类别为:" + this.dao.getModelClass() + " ,模型Id为: " + id; this.log.info(message); messages.add(message); } } return messages; } @Profiled(tag = "modifyInManager", message = "model = {$0}") public void modify(T model) { this.dao.modify(model); } @Profiled(tag = "queryInManager", message = "modelId = {$0}") public T query(Integer modelId) { return (T) this.dao.query(modelId); } public ModelSet<T> queryAll() { return queryAll(-1, -1); } public ModelSet<T> queryAll(int pageIndex, int pageSize) { return queryAll(pageIndex, pageSize, null); } public ModelSet<T> queryAll(int pageIndex, int pageSize, LinkedHashMap<String, String> orderby) { return queryAll(pageIndex, pageSize, null, null, orderby); } @Profiled(tag = "queryAllInManager", message = "pageIndex = {$0}, pageSize = {$1}, wherejpql = {$1}, queryParams = {$1}, orderby = {$1}") public ModelSet<T> queryAll(int pageIndex, int pageSize, String wherejpql, Object[] queryParams, LinkedHashMap<String, String> orderby) { int firstindex = (pageIndex - 1) * pageSize; int maxresult = pageSize; ModelSet models = this.dao.queryAll(firstindex, maxresult, wherejpql, queryParams, orderby); return models; } @Profiled(tag = "searchInManager", message = "queryString = {$0}") public List<T> search(String queryString) { User user = UserHolder.getCurrentLoginUser(); user.setQueryString(queryString); return this.dao.search(queryString); } @Profiled(tag = "getSearchResultForExportInManager", message = "pageIndex = {$0}, pageSize = {$1}") public List<T> getSearchResultForExport(int pageIndex, int pageSize) { ModelSet qr = getSearchResult(pageIndex, pageSize); List constantModels = qr.getResultlist(); List models = new ArrayList(constantModels.size()); for (int i = 0; i < constantModels.size(); ++i) { try { models.add(((Model) constantModels.get(i)).clone()); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } removeHightlight(models); return models; } @Profiled(tag = "getSearchResultInManager", message = "pageIndex = {$0}, pageSize = {$1}") public ModelSet<T> getSearchResult(int pageIndex, int pageSize) { User user = UserHolder.getCurrentLoginUser(); ModelSet qr = null; Element element = null; List models = new ArrayList(); element = this.cache.get(user.getCacheName() + "search" + this.dao.getModelClass().getName()); if (element == null) { search(user.getQueryString()); element = this.cache.get(user.getCacheName() + "search" + this.dao.getModelClass().getName()); } this.log.info("从缓存中获取搜索结果以供分页显示,页数为: " + pageIndex + ",页面大小为: " + pageSize); this.log.info("缓存标识为: " + user.getCacheName() + "search" + this.dao.getModelClass().getName()); qr = (ModelSet) element.getValue(); int start = (pageIndex - 1) * pageSize; int end = pageIndex * pageSize + pageSize; if (end > qr.getResultlist().size()) { end = qr.getResultlist().size(); } for (int i = start; i < end; ++i) { models.add((Model) qr.getResultlist().get(i)); } long count = qr.getTotalrecord(); qr = new ModelSet(); qr.setResultlist(models); qr.setTotalrecord(count); return qr; } @Profiled(tag = "removeHightlightInManager", message = "models = {$0}") private void removeHightlight(List<T> models) { for (Model model : models) { for (String searchProperty : model.getSearchProperties()) { String[] multiLevelProperties = searchProperty.split(":"); String[] searchExpressions = multiLevelProperties[0].split("_"); String realProperty = searchExpressions[(searchExpressions.length - 1)]; String[] properties = (String[]) null; if (multiLevelProperties.length > 1) { properties = multiLevelProperties[1].split("_"); } List<Model> objs = new ArrayList<Model>(); objs.add(model); if (properties != null) { for (String props : properties) { List now = new ArrayList(); for (Model temp : objs) { if (props.endsWith("$")) { List temps = (List) ReflectionUtils.getFieldValue(temp, props.substring(0, props.length() - 1)); now.addAll(temps); } else { Model next = (Model) ReflectionUtils.getFieldValue(temp, props); now.add(next); } } objs = now; } } String newValue = null; try { for (Model obj : objs) { Object old = ReflectionUtils.getFieldValue(obj, realProperty); if (old != null) { String oldValue = old.toString(); newValue = WebUtil.removeHightlight(oldValue); ReflectionUtils.setFieldValue(obj, realProperty, newValue); } } } catch (Exception e) { this.log.info("移除高亮,给对象【" + model.getMetaData() + "】设置属性【" + realProperty + "】失败,值为:【" + newValue + "】"); } } } } } 实例: UserManager package com.jxs.sys.core.security.manager; import com.jxs.sys.core.base.service.Manager; import com.jxs.sys.core.security.model.User; public interface UserManager extends Manager<User> { public abstract User queryUserByName(String username); } UserManagerImpl: package com.jxs.sys.core.security.manager.impl; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.jxs.sys.core.base.service.ManagerSupport; import com.jxs.sys.core.security.dao.UserDao; import com.jxs.sys.core.security.manager.UserManager; import com.jxs.sys.core.security.model.User; @Service("userManager") @Transactional(readOnly = true) public class UserManagerImpl extends ManagerSupport<User, UserDao> implements UserManager { @Override public User queryUserByName(String username) { return ((UserDao) this.dao).queryUserByName(username); } } 期待action层
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-09-21
只能鼓励一下,打个新手贴,自己学习还是不错的,不过还是尽量用已有的吧,造轮子的事情只能自娱自乐啊
|
|
返回顶楼 | |
发表时间:2010-09-21
其实我想知道的是你的cache是怎么做的。。
|
|
返回顶楼 | |
发表时间:2010-09-21
oh,看到了。。。
原来是那样做的,和我的一样!! 不知道有没有更好的解决方案~~~ |
|
返回顶楼 | |