2015-06-06 67 views
0

我使用Hibernate的spring mvc。我使用Hibernate編寫插入,但是我找不到從我的數據庫編寫搜索數據的方法。我在下面的例子中插入了一個我喜歡的過程。我如何使用SessionFactory Autowired對象編寫select?如何使用休眠選擇數據?

我想使用Hibernate做select * from employee where username='hesh'

@Repository 
public class EmployeeDaoImpl implements EmployeeDAO{ 

@Autowired 
private SessionFactory sessionFactory; 

@Override 
public void AddEmployee(Employee employee) throws ClassNotFoundException, SQLException { 
    this.sessionFactory.getCurrentSession().save(employee); 

}} 
+0

http://www.tutorialspoint.com/hibernate/hibernate_query_language.htm – ArunM

回答

0

嘗試使用此方法:

@Override 
public List listEmployee(String username) throws Exception{ 
    Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Employee.class) 
     .add(Restrictions.eq("username", username)); 
criteria.setMaxResults(10);//optionally set max return rows.  
return criteria.list(); 
}}