我正在创建一个休眠应用程序,并且可以通过hibernate
. 尽管如此,我还是不知道如何从数据库中检索数据。谁能告诉我,如何以某种方式做到这一点,这将类似于我保存数据的方式?我一直在谷歌搜索,但一切,我没有找到任何东西,这看起来像我的方式......例如,他们使用了很多注释等。我对休眠很陌生,所以有可能,这不是一个好方法,如何创建hibernate应用程序。如果是这样,请将其写在评论中。
我的一部分UserDAOImpl
:
UserDAO user;
public UserDAOImpl() {
user = new UserDAO();
}
public void createUser(int id, String Username, String CreatedBy) {
user.setUserId(id);
user.setUsername(Username);
user.setCreatedBy(CreatedBy);
user.setCreatedDate(new Date());
}
public void saveUser() {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(user);
// save the data to the database
session.getTransaction().commit();
// close the connection
session.disconnect();
}
public void findUser() {
// ????????????
}
用户道:
public class UserDAO implements java.io.Serializable {
/**
* generated serialVersionUID
*/
private static final long serialVersionUID = -6440259681541809279L;
private int userId;
private String username;
private String createdBy;
private Date createdDate;
public UserDAO() {
}
public UserDAO(int userId, String username, String createdBy,
Date createdDate) {
this.userId = userId;
this.username = username;
this.createdBy = createdBy;
this.createdDate = createdDate;
}
public int getUserId() {
return this.userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getCreatedBy() {
return this.createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
}