1、对于hibernate要先导入jar包
antlr-2.7.6.jar
commons-collections-3.1.jardom4j-1.6.1.jar
hibernate-jpa-2.0-api-1.0.0.Final.jarhibernate3.jar
javassist-3.12.0.GA.jarjta-1.1.jar
mysql-connector-java-5.1.12-bin.jarslf4j-api-1.6.1.jar
2、创建实体类Employee.java
package cn.incast.a_hello;
import java.util.Date;
public class Employee {
private int empId;
private String empName;
private Date workDate;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Date getWorkDate() {
return workDate;
}
public void setWorkDate(Date workDate) {
this.workDate = workDate;
}
}
3、配置Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.incast.a_hello">
<!--name为实体类的名称 table为表明 -->
<class name="Employee" table="Employee">
<!-- 主键字段 -->
<!-- name为实体类中的属性名 column为数据库的字段名 -->
<id name="empId" column="id">
<!-- native为自增长 -->
<generator class="native"></generator>
</id>
<!-- 其他字段 -->
<property name="empName" column="empName"></property>
<property name="workDate" column="workDate"></property>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<!-- 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///employee</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 其他相关配置 -->
<!-- 显示hibernate在运行时执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 加载所有映射 -->
<mapping resource="cn/incast/a_hello/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、创建App类进行进行测试
package cn.incast.a_hello;
import java.util.Date;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class App {
public static void main(String[] args) throws Exception {
//创建对象
Employee emp=new Employee();
emp.setEmpId(1);
emp.setEmpName("李四");
emp.setWorkDate(new Date());
//获取加载配置文件的管理类对象
Configuration config=new Configuration();
config.configure();//默认加载src/hibernate.cfg.xml文件
//创建session的工厂对象
SessionFactory sf=config.buildSessionFactory();
//创建session(代表一个回话,与数据库连接的回话)
Session session=sf.openSession();
//开启事物
Transaction ts=session.beginTransaction();
session.update(emp);
//提交事物
ts.commit();
session.close();
}
}
5、控制台输出