使用hql修改语句查询

package com.bdqn.test;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.xml.crypto.Data;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.bdqn.entitydao.Electronics;
import com.bdqn.entitydao.ElectronicsDAO;


public class P122 {
	public static void main(String[] args) throws ParseException {
		
		
		查找指定的model(类型)
		ElectronicsDAO dao = new ElectronicsDAO();
		List<Electronics> electronics = dao.findByModle("CDMA-2");
		for (Electronics e : electronics) {
			System.out.print(e.getId()+"--");
			System.out.print(e.getModel()+"--");
			System.out.print(e.getPrice()+"--");
			System.out.println(e.getProductdate());
		}
		查找出厂价格小于指定价格的备件
		ElectronicsDAO dao = new ElectronicsDAO();
		List<Electronics> electronics = dao.MyfindByPrice(800);
		for (Electronics e : electronics) {
			System.out.print(e.getId()+"--");
			System.out.print(e.getModel()+"--");
			System.out.print(e.getPrice()+"--");
			System.out.println(e.getProductdate());
		}
		查出出厂日期,在指定日期方位内的
		ElectronicsDAO dao = new ElectronicsDAO();
		String times1 = "2018-8-18";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(times1);
        System.out.println(date1);
        String times2 = "2018-10-18";
        Date date2 = sdf.parse(times2);
        System.out.println(date2);
		List<Electronics> electronics = dao.MyfindByProductdate(date1,date2);
		for (Electronics e : electronics) {
			System.out.print(e.getId()+"--");
			System.out.print(e.getModel()+"--");
			System.out.print(e.getPrice()+"--");
			System.out.println(e.getProductdate());
		}
		符合条件的查询,可按型号模糊查询,按价格区间查询,按出厂日期查询,这三个条件可任意组合
		ElectronicsDAO dao = new ElectronicsDAO();
		String times1 = "2018-8-18";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(times1);
        String times2 = "2018-10-18";
        Date date2 = sdf.parse(times2);
		List<Electronics> electronics = dao.findByTIAOJIAN("%CD%", 100.00, 1000.00, date1, date2);
		for (Electronics e : electronics) {
			System.out.print(e.getId()+"--");
			System.out.print(e.getModel()+"--");
			System.out.print(e.getPrice()+"--");
			System.out.println(e.getProductdate());
		}
	}
}

对应的dao层

package com.bdqn.entitydao;

import java.util.Date;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * Electronics entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.bdqn.entitydao.Electronics
 * @author MyEclipse Persistence Tools
 */
public class ElectronicsDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(ElectronicsDAO.class);
	// property constants
	public static final String MODEL = "model";
	public static final String PRICE = "price";

	public void save(Electronics transientInstance) {
		log.debug("saving Electronics instance");
		try {
			getSession().beginTransaction();
			getSession().save(transientInstance);
			getSession().getTransaction().commit();
			getSession().close();
			
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	//P122页——1使用HQL语句,查找指定语句
	public List findByModle(String model) {
		log.debug("finding all Electronics instances");
		try {
			getSession().beginTransaction();
			String queryString = "from Electronics e where model = ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setString(0, model);
			getSession().getTransaction().commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	符合条件的查询,可按型号模糊查询,按价格区间查询,按出厂日期查询,这三个条件可任意组合
	public List findByTIAOJIAN (String model,double price1,double price2,Date date1,Date date2) {
		log.debug("finding all Electronics instances");
		try {
			getSession().beginTransaction();
			String queryString = "FROM Electronics WHERE model like :model and price between :price1 and :price2 and productdate between :date1 and :date2";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setString("model", model);
			queryObject.setDouble("price1", price1);
			queryObject.setDouble("price2", price2);
			queryObject.setDate("date1", date1);
			queryObject.setDate("date2", date2);

			getSession().getTransaction().commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	//查出出厂日期,在指定日期方位内的
	public List MyfindByProductdate(Date startTime,Date endTime) {
		log.debug("finding all Electronics instances");
		try {
			getSession().beginTransaction();
			String queryString = "from Electronics where productdate between ? and ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setDate(0, startTime);
			queryObject.setDate(1, endTime);
			getSession().getTransaction().commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public void saveOrUpdate(Electronics transientInstance) {
		log.debug("saving Electronics instance");
		try {
			getSession().beginTransaction();
			getSession().saveOrUpdate(transientInstance);
			getSession().getTransaction().commit();
			getSession().close();
			
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Electronics persistentInstance) {
		log.debug("deleting Electronics instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Electronics findById(java.lang.Short id) {
		log.debug("getting Electronics instance with id: " + id);
		try {
			Electronics instance = (Electronics) getSession().get(
					"com.bdqn.entitydao.Electronics", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Electronics instance) {
		log.debug("finding Electronics instance by example");
		try {
			List results = getSession()
					.createCriteria("com.bdqn.entitydao.Electronics")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Electronics instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Electronics as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByModel(Object model) {
		return findByProperty(MODEL, model);
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findAll() {
		log.debug("finding all Electronics instances");
		try {
			String queryString = "from Electronics";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Electronics merge(Electronics detachedInstance) {
		log.debug("merging Electronics instance");
		try {
			Electronics result = (Electronics) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Electronics instance) {
		log.debug("attaching dirty Electronics instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Electronics instance) {
		log.debug("attaching clean Electronics instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值