【问题标题】:How to select data using hibernate?如何使用休眠选择数据?
【发布时间】:2015-06-06 06:26:06
【问题描述】:

我正在使用带有 Hibernate 的 spring mvc。我使用 Hibernate 编写插入,但我找不到从我的数据库中写入搜索数据的方法。我在下面的示例中插入了一个我喜欢的过程。如何使用 SessionFactory Autowired 对象编写选择?

我想用 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);  

}}

【问题讨论】:

标签: hibernate


【解决方案1】:

尝试使用这种方法:

@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();
}}

【讨论】:

    猜你喜欢
    • 2011-07-27
    • 2011-04-12
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多