package com.tb.dao.impl;import java.util.ArrayList;
import java.util.List;import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;import com.tb.dao.CustomerDao;
import com.tb.entity.Customer;
import com.tb.factory.HibernateSessionFactory;public class CustomerDaoImpl implements CustomerDao{ public void addCustomer(Customer customer) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction().begin();
session.save(customer);
session.beginTransaction().commit();
} public void deleteCustomer(Customer customer) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction().begin();
session.delete(customer);
session.beginTransaction().commit();
} public List<Customer> findByPropery(String properyName, Object value) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
String hql = "from Customer where "+properyName+"=?";
Query query = session.createQuery(hql);
query.setParameter(0, value);
return query.list();
} public List<Customer> getCustomerAll() throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
String hql = "from Customer order by id desc";
return session.createQuery(hql).list();
} public Customer getCustomerById(Integer id) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
return (Customer)session.get(Customer.class, id);
} public void modityCustomer(Customer customer) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction().begin();
session.update(customer);
session.beginTransaction().commit();
} public List<Customer> searchTopic(String customercompay, String typeid,String roleid) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
String hql = "from Customer where 1=1";
//System.out.println("所属公司------------:"+customercompay);
//System.out.println("客户类型------------:"+typeid);
List paras = new ArrayList();
if(!"".equals(customercompay))
{
hql+=" and companyname like ?";
paras.add(customercompay+"%");
}
if(!"".equals(typeid))
{
int id = Integer.parseInt(typeid);
hql+=" and customertype.id=?";
paras.add(id);
}
if(!"".equals(roleid))
{
int id = Integer.parseInt(roleid);
hql+=" and roleid like ?";
paras.add(roleid+"%");
}
hql += " order by id desc";
Query query = session.createQuery(hql);
for(int i =0;i<paras.size();i++)
{
query.setParameter(i, paras.get(i));
} return query.list();
}
public static void main(String[] args) {
Session se = HibernateSessionFactory.getSession();
String hql = "from Customer where customertype.id=5";
Query query = se.createQuery(hql);
List list = query.list();
for(int i = 0 ;i < list.size();i++)
{
System.out.println(((Customer)list.get(i)).getCompanyname());
}
}public List<Customer> searchTopic(String customercompay, String typeid) throws Exception {
// TODO Auto-generated method stub
return null;
}
}请问在里面如何在加入hql的模糊查询???
import java.util.List;import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;import com.tb.dao.CustomerDao;
import com.tb.entity.Customer;
import com.tb.factory.HibernateSessionFactory;public class CustomerDaoImpl implements CustomerDao{ public void addCustomer(Customer customer) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction().begin();
session.save(customer);
session.beginTransaction().commit();
} public void deleteCustomer(Customer customer) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction().begin();
session.delete(customer);
session.beginTransaction().commit();
} public List<Customer> findByPropery(String properyName, Object value) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
String hql = "from Customer where "+properyName+"=?";
Query query = session.createQuery(hql);
query.setParameter(0, value);
return query.list();
} public List<Customer> getCustomerAll() throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
String hql = "from Customer order by id desc";
return session.createQuery(hql).list();
} public Customer getCustomerById(Integer id) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
return (Customer)session.get(Customer.class, id);
} public void modityCustomer(Customer customer) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
session.beginTransaction().begin();
session.update(customer);
session.beginTransaction().commit();
} public List<Customer> searchTopic(String customercompay, String typeid,String roleid) throws Exception {
// TODO Auto-generated method stub
Session session = HibernateSessionFactory.getSession();
String hql = "from Customer where 1=1";
//System.out.println("所属公司------------:"+customercompay);
//System.out.println("客户类型------------:"+typeid);
List paras = new ArrayList();
if(!"".equals(customercompay))
{
hql+=" and companyname like ?";
paras.add(customercompay+"%");
}
if(!"".equals(typeid))
{
int id = Integer.parseInt(typeid);
hql+=" and customertype.id=?";
paras.add(id);
}
if(!"".equals(roleid))
{
int id = Integer.parseInt(roleid);
hql+=" and roleid like ?";
paras.add(roleid+"%");
}
hql += " order by id desc";
Query query = session.createQuery(hql);
for(int i =0;i<paras.size();i++)
{
query.setParameter(i, paras.get(i));
} return query.list();
}
public static void main(String[] args) {
Session se = HibernateSessionFactory.getSession();
String hql = "from Customer where customertype.id=5";
Query query = se.createQuery(hql);
List list = query.list();
for(int i = 0 ;i < list.size();i++)
{
System.out.println(((Customer)list.get(i)).getCompanyname());
}
}public List<Customer> searchTopic(String customercompay, String typeid) throws Exception {
// TODO Auto-generated method stub
return null;
}
}请问在里面如何在加入hql的模糊查询???
解决方案 »
- JSP过滤器无效
- 最近浏览功能
- 请教:Java发邮件在windows下面可以,在Linux上面发不了
- 请问一个Struts1原理的一个非常郁闷的问题,我调试到源码还是找不出原因,希望解决,谢谢
- 急呀!!!!!!!!!!!!!!!!!!!!!hibernate学习路径!
- 关于进制的问题
- 用理性的方法选择Web报表工具
- 散分,救命呀!cmp开发的时候发布异常(jbuilder+jboss)
- String类型参数传递的问题
- jsp的session.getAttribute()在不同的jsp页面显示null
- java的连接池,关于C3P0连接的问题,
- 为什么Linux下的Eclipse菜单项不显示图标?
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.like("name", "%ying%"));
List<Employee> list = criteria.list();
用QBC好一点吧。
.Add( Expression.Like("Name", "Fritz%") )
.List();
@SuppressWarnings("unchecked")
public void K_query(final DbUser dbuser,final Page<DbUser> p, final int currPage) {
// TODO Auto-generated method stub
List<DbUser> rel = null;
rel=super.getHibernateTemplate().executeFind(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException, SQLException {
// TODO Auto-generated method stub
List<DbUser> rel = null;
Criteria cri = session.createCriteria(DbUser.class);//查符合该条件的全部记录条数
Criteria cri2 = session.createCriteria(DbUser.class);//查当前页面上显示的数据记录集合
// cri2.createAlias("db_dep", "db_dep");
if(dbuser!=null){
if(dbuser.getUsername()!=null&&!"".equals(dbuser.getUsername())){
cri.add(Restrictions.like("username", "%"+dbuser.getUsername()+"%"));
cri2.add(Restrictions.like("username","%"+dbuser.getUsername()+"%"));
}if(dbuser.getName()!=null&&!"".equals(dbuser.getName())){
cri.add(Restrictions.like("name", "%"+dbuser.getName()+"%"));
cri2.add(Restrictions.like("name", "%"+dbuser.getName()+"%"));
}if(dbuser.getDbDep().getDepId()!=0){
cri.add(Restrictions.eq("dbDep.depId", dbuser.getDbDep().getDepId()));
cri2.add(Restrictions.eq("dbDep.depId", dbuser.getDbDep().getDepId()));
}
}
cri2.addOrder(Order.asc("dbDep.depId"));
String totalStr = cri.setProjection(Projections.rowCount()).uniqueResult().toString();
int total = Integer.valueOf(totalStr);
p.setTotal(total);//设置总记录条数
p.setCurrPage(currPage);//设置当前页
rel = cri2.setFirstResult((p.getCurrPage()-1)*p.getPageSize()).setMaxResults(p.getPageSize())
.list();//取得页面上要显示的数据记录集合
session.close();
return rel;
}
});
p.setPlist(rel);
}