创建一个hibernate项目
这里为了快速和方便体现hibernate,创建的是一个简易的java项目,架构如下
首先第一步是:创建一个java项目或者web项目
第二步是:导入hibernate的一些依赖jar包,如果是java项目,就直接在项目右键properties,右边选择第二个(外部导入)
如果是web项目可以直接把jar包复制到lib下
这里是hibernate的一些依赖jar包
第三步是:创建一个实体类,例如student类(假设类所在的这个包是com.MyHibernateDemo,entity),类属性有,name,id,year,给seter和geter方法;并且在这个包下配置对象对应的xml文件(取名xxx.hbm.xml,xxx表示的是实体类名称),同时也在src目录下创建一个hibernate.cfg.xml配置文件。
其中xxx.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="com.MyHibernateDemo.entity">
<class name="Student" table="student">//name表示这个包下的一个类,table表示一个数据库表,这里是把类和表关联起来。这里指的注意的是,最好让实体类的属性和表的字段关联起来。
<id name="id" column="id">
<generator class="native">//这里是mysql的主键自增的意思
</generator>
</id>
<property name="stu_name" />
<property name="stu_no" />
<property name="stu_score" />
</class>
</hibernate-mapping>
hibernate.cfg.xml的内容为:
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/student?characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
//这里是表示映射到这个包下的某个配置文件
<mapping resource="com/MyHibernateDemo/entity/Student.hbm.xml" /></session-factory>
</hibernate-configuration>
第四步是:创建一个工具类(可以重新创建在一个新包中),例如HibernateUtil类,在这个类中的内容是
public class HibernateUtil {
private static SessionFactory sessionfactory;
static{//这里是静态代码块,会首先执行这里的代码,也就是说会先加载配置文件,得到sessionfactory的初始化数据
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
sessionfactory =cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sessionfactory;
}
public static Session getSession(){
return sessionfactory.openSession();
}
public static void add(Object entity){
Session s=null;
Transaction tx=null;
try{
s=HibernateUtil.getSession();
tx=s.beginTransaction();
s.save(entity);
tx.commit();
}finally{
if(s!=null){
s.close();
}
}
}
public static void update(Object entity){
Session s=null;
Transaction tx=null;
try{
s=HibernateUtil.getSession();
tx=s.beginTransaction();
s.update(entity);
tx.commit();
}finally{
if(s!=null){
s.close();
}
}
}
public static void delete(Object entity){
Session s=null;
Transaction tx=null;
try{
s=HibernateUtil.getSession();
tx=s.beginTransaction();
s.delete(entity);
tx.commit();
}finally{
if(s!=null){
s.close();
}
}
}
public static Object get(Serializable id){
Session s=null;
try{
s=HibernateUtil.getSession();
Object obj=s.get(Student.class, id);
return obj;
}finally{
if(s!=null){
s.close();
}
}
}
}
这个工具类包含了增删改查基本的操作
第五步是:创建一个测试类内容为:
package com.MyHibernateDemo.Test;
import com.MyHibernateDemo.Util.HibernateUtil;
import com.MyHibernateDemo.entity.Student;
public class HibernateTest {
public static void main(String []args){
Student student=new Student();
student.setStu_score(100);
student.setStu_name("Hello World LinJi");
student.setStu_no(123);
HibernateUtil.add(student);
System.out.println("添加结束");
System.out.println(HibernateUtil.get(2));
}
}
以上为添加一个学生,并且获取一个id号为2的学生的信息
我们的实体类内容为:
package com.MyHibernateDemo.entity;
public class Student {
private int id;
private int stu_no;
private String stu_name;
private int stu_score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStu_no() {
return stu_no;
}
public void setStu_no(int stu_no) {
this.stu_no = stu_no;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public int getStu_score() {
return stu_score;
}
public void setStu_score(int stu_score) {
this.stu_score = stu_score;
}
@Override
public String toString() {
return "Student [id=" + id + ", stu_no=" + stu_no + ", stu_name="
+ stu_name + ", stu_score=" + stu_score + "]";
}
}
这就是基本的hibernate框架入门使用。