2015-06-06 66 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(); 
}}