第一个hibernate例子

1、对于hibernate要先导入jar包

antlr-2.7.6.jar

commons-collections-3.1.jar

dom4j-1.6.1.jar

hibernate-jpa-2.0-api-1.0.0.Final.jar

hibernate3.jar

javassist-3.12.0.GA.jar

jta-1.1.jar

mysql-connector-java-5.1.12-bin.jar

slf4j-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>


4、配置hibernate.cfg.xml

<!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、控制台输出



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值