版本:4.2.2
配置文件:hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by eclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!--显示执行的SQL语句-->
<property name="show_sql">true</property>
<!--连接字符串-->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<!--连接数据库的用户名-->
<property name="connection.username">root</property>
<!--数据库用户密码-->
<property name="connection.password"></property>
<!--数据库驱动-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--JDBC连接池(使用内置的连接池)-->
<!-- <property name="connection.pool_size">1</property> -->
<!--设置Hibernate自动管理上下文的策略-->
<!-- <property name="current_session_context_class">thread</property> -->
<!--选择使用的方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--在启动时删除并重新创建数据库 hibernate mapping data defination language(create/update/validate/create-drop)-->
<property name="hbm2ddl.auto">create</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- 配置文件mapping -->
<!--
<mapping resource="events/Event.hbm.xml"/>
<mapping resource="events/Person.hbm.xml"/>
-->
<!-- 注解mapping -->
<mapping class="com.basic.hibernate.model.Student"></mapping>
</session-factory>
</hibernate-configuration>
实体类:
annotation,注解一般加在get方法上,不加在成员变量上
@Entity
@Table(name="student")
public class Student {
private Integer id;
private String name;
private Integer age;
private String t;
private Date birthDate;
private Title title;
@Transient //该属性不存入数据库
public String getT() {
return t;
}
public void setT(String t) {
this.t = t;
}
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic //没写时
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Temporal(TemporalType.TIME) //时间精度 value=TemporalType.TIME
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@Enumerated(EnumType.STRING) //枚举类型
public Title getTitle() {
return title;
}
public void setTitle(Title title) {
this.title = title;
}
}
Junit测试类
加junit jar包, 对象保存
public class StudentTest {
private static SessionFactory sf = null;
@SuppressWarnings("deprecation")
@BeforeClass
public static void beforeClass(){
sf = new Configuration().configure().buildSessionFactory();
}
@Test
public void testStudentSave() {
Student s = new Student();
s.setId(1);
s.setName("s1");
s.setAge(1);
s.setBirthDate(new Date());
s.setTitle(Title.high);
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
//fail("Not yet implemented");
}
@AfterClass
public static void afterClass(){
sf.close();
}
}