最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

实现按条件查询

来源:懂视网 责编:小采 时间:2020-11-09 15:07:43
文档

实现按条件查询

实现按条件查询:第一步: 定义接口: public interface ICommonDaoT { ListT findCollectionByConditionNoPage(String codition,Object[] params, MapString, String orderby); } 第二步: 实现接口的类: Class entityClass =
推荐度:
导读实现按条件查询:第一步: 定义接口: public interface ICommonDaoT { ListT findCollectionByConditionNoPage(String codition,Object[] params, MapString, String orderby); } 第二步: 实现接口的类: Class entityClass =

第一步: 定义接口: public interface ICommonDaoT { ListT findCollectionByConditionNoPage(String codition,Object[] params, MapString, String orderby); } 第二步: 实现接口的类: Class entityClass = TUtils.getTClass(this.getClass());public cl

第一步:

定义接口:

public interface ICommonDao {

List findCollectionByConditionNoPage(String codition,Object[] params, Map orderby);

}

第二步:
实现接口的类:

Class entityClass = TUtils.getTClass(this.getClass());

public class TUtils {

	/**泛型转换,目的获取子类传递父类的真实类型,也就是T所对应的类型*/
	public static Class getTClass(Class entity) {
	ParameterizedType type = (ParameterizedType)entity.getGenericSuperclass();
	Class entityClass = (Class) type.getActualTypeArguments()[0];
	return entityClass;
	}
} 
/**指定查询条件查询对应的结果,返回List(不分页)*/
	/**
	 * FROM ElecText o WHERE 1=1 #Dao层
	AND o.textName LIKE '%张%'	#Service层
	AND o.textRemark LIKE '%张%' #Service层
	ORDER BY o.textDate ASC,o.textName DESC #Service层
	 */
	public List findCollectionByConditionNoPage(String condition,
	final Object[] params, Map orderby) {
	//定义hql语句
	String hql = "FROM "+entityClass.getSimpleName()+" o WHERE 1=1";
	//定义排序语句
	String orderbyHql = this.orderbyHql(orderby);
	//定义最终的语句
	final String finalHql = hql + condition + orderbyHql;
	//执行语句一
	//List list = this.getHibernateTemplate().find(finalHql, params);
	//执行语句二
//	SessionFactory sf = this.getHibernateTemplate().getSessionFactory();
//	Session s = sf.getCurrentSession();
//	Query query = s.createQuery(finalHql);
//	List list = query.list();
	//执行语句三
	List list = this.getHibernateTemplate().execute(new HibernateCallback() {

	public Object doInHibernate(Session session)
	throws HibernateException, SQLException {
	Query query = session.createQuery(finalHql);
	if(params!=null && params.length>0){
	for(int i=0;i orderby) {
	StringBuffer buffer = new StringBuffer("");
	if(orderby!=null && orderby.size()>0){
	buffer.append(" order by ");
	for(Map.Entry map:orderby.entrySet()){
	buffer.append(map.getKey()+" "+map.getValue()+",");
	}
	//删除最后一个逗号
	buffer.deleteCharAt(buffer.length()-1);
	}
	return buffer.toString();
	}

下面是根据不同的业务来编写不同的代码。

第三步:

service接口:

public interface IElecTextService {
	public static final String SERVICE_NAME = "com.itheima.elec.service.impl.ElecTextServiceImpl";
	
	List findCollectionByConditionNoPage(ElecText elecText);
}

第四步:

service实现

/**指定查询条件查询对应的结果,返回List*/
	/**
	 * FROM ElecText o WHERE 1=1 #Dao层
	AND o.textName LIKE '%张%'	#Service层
	AND o.textRemark LIKE '%张%' #Service层
	ORDER BY o.textDate ASC,o.textName DESC #Service层
	 */
	public List findCollectionByConditionNoPage(ElecText elecText) {
	//查询条件
	String condition = "";
	List paramsList = new ArrayList();
	//判断是否添加查询条件
	if(StringUtils.isNotBlank(elecText.getTextName())){
	condition += " and o.textName like ?";
	paramsList.add("%"+elecText.getTextName()+"%");
	}
	if(StringUtils.isNotBlank(elecText.getTextRemark())){
	condition += " and o.textRemark like ?";
	paramsList.add("%"+elecText.getTextRemark()+"%");
	}
	Object [] params = paramsList.toArray();
	//排序语句(hql语句和sql语句的排序是有顺序的
	Map orderby = new LinkedHashMap();
	orderby.put("o.textDate", "asc");
	orderby.put("o.textName", "desc");
	//查询
	List list = elecTextDao.findCollectionByConditionNoPage(condition,params,orderby);
	return list;
	}


第五步:

/**模拟Action,调用Service,指定查询条件查询对应的结果,返回List*/
	@Test
	public void findCollectionByConditionNoPage(){
	ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
	IElecTextService elecTextService = (IElecTextService) ac.getBean(IElecTextService.SERVICE_NAME);
	
	ElecText elecText = new ElecText();
//	elecText.setTextName("张");
//	elecText.setTextRemark("张");
	
	List list = elecTextService.findCollectionByConditionNoPage(elecText);
	if(list!=null && list.size()>0){
	for(ElecText elecText2:list){
	System.out.println(elecText2.getTextName()+" "+elecText2.getTextDate()+" "+elecText2.getTextRemark());
	}
	}
	}


真正的action:

package com.itheima.elec.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.elec.domain.ElecSystemDDL;
import com.itheima.elec.service.IElecSystemDDLService;


@SuppressWarnings("serial")
@Controller("elecSystemDDLAction")
@Scope(value="prototype")
public class ElecSystemDDLAction extends BaseAction{
	
	ElecSystemDDL elecSystemDDL = this.getModel();
	
	@Resource(name=IElecSystemDDLService.SERVICE_NAME)
	private IElecSystemDDLService elecSystemDDLService;
	public String home(){
	List list = elecSystemDDLService.findKeywordWithDistinct();
	request.setAttribute("list", list);
	return "home";
	}
	
	
	public String edit(){
	//获取数据类型
	String keyword = elecSystemDDL.getKeyword();
	//1:使用数据类型作为查询条件,查询数据字典表,返回List
	List list = elecSystemDDLService.findSystemDDLListByKeyword(keyword);
	request.setAttribute("systemList", list);
	return "edit";
	}
	
}
总结:根据什么样的条件查询,是在service层完成的,把所有的条件组织好后,给dao层,dao层再拼接SQL或者hql语句,进行真正的查询。web层的action只是传递参数,进行简单的调用service层的方法。

文档

实现按条件查询

实现按条件查询:第一步: 定义接口: public interface ICommonDaoT { ListT findCollectionByConditionNoPage(String codition,Object[] params, MapString, String orderby); } 第二步: 实现接口的类: Class entityClass =
推荐度:
标签: 查询 定义 条件
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

Top