Hibernate 注解

来源:百度知道 编辑:UC知道 时间:2024/05/22 21:40:34
配置文件:
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/worklog
hibernate.connection.username=root
hibernate.connection.password=acc

持久化类:

package mem;
import javax.persistence.*;

@Entity(name="Member")
@Table
public class Member implements java.io.Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@Column(name="id")
private Integer id;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
public Member() {
}
public Member(String username, String password) {
this.username = username;
this.password = password;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}

在 hibernate.cfg.xml 文件

<hibernate-configuration>
中添加 <mapping class="mem.Member" />

这个就是把持久化类加载进去

Configuration c=new Configuration().configure();
这里改成这样

刚做过Struts2的项目, 也用了 Annotation, 你写的有些地方没看明白,既然要用到sessionFactory = configuration.buildSessionFactory(); 为什么不在session 中使用 dao 的方法.
有问题可继续交流.我会继续关注.

靠 你要赶什么 啊?