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;
}
}
}