Hibernate 第一个示例说明
下文是笔者编写的第一个Hibernate示例,如下所示:
第一步:持久化类
第二步:创建table
第三步:编写相应的映射文件
第四步:编写相应的Java操作文件,如下例所示:
持久化类
public class User { private int id; private String name; private int salary; public User () { name=null; salary =0; } public User (String name, int salary) { this.name = name; this.salary = salary; } public int getId() { return id; } public void setId( int id ) { this.id = id; } public String getname() { return name; } public void setname( String name ) { this.name = name; } public int getSalary() { return salary; } public void setSalary( int salary ) { this.salary = salary; } }数据表信息
create table tbUser ( id INT NOT NULL auto_increment, name nVARCHAR(20) default NULL salary INT default NULL, PRIMARY KEY (id) );映射文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="User" table="tbUser"> <meta attribute="class-description"> This class contains the User detail. </meta> <id name="id" type="int" column="id"> <generator class="native"/> </id> <property name="name" column=" name" type="string"/> <property name="salary" column="salary" type="int"/> </class> </hibernate-mapping>映射文件注意事项:
1.映射文件需使用 <classname>.hbm.xml 文件名保存 ,如: user.hbm.xml
2.标签说明:
class标签用于 其中的name属性表示 Java类名称,table属性值表示数据库表名称
meta标签是一个可选元素,用于修饰类
id标签通常用于表示主键
generator标签:可以设置 native 使 Hibernate 可以使用 identity, sequence 或 hilo 算法根据底层数据库的情况来创建主键
property标签用于表示Java中类的属性同数据库列的匹配关系
name属性值:用于设置类的属性名
column:用于定义数据库中表的列名称
type:用于定义数据库表中的列名称
编写应用程序类信息
import java.util.list; import java.util.Date; import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class ManageUser{ private static SessionFactory factory; public static void main(String[] args) { try{ factory = new Configuration().configure().buildSessionFactory(); }catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } ManageUser m = new ManageUser(); Integer u1= m.addUser("zhangSan", 88); Integer u2= m.addUser("liSi",99); Integer u3= m.addUser("wangEr",100); m.listUser(); /* Update user records */ m.updateUser(u1, 888888); m.deleteUser(u2); m.listUser(); } public Integer addUser(String name, int salary){ Session session = factory.openSession(); Transaction tx = null; Integer id= null; try{ tx = session.beginTransaction(); User user= new User(name,salary); id= (Integer) session.save(user); tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } return id; } public void listUser( ){ Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); List users= session.createQuery("FROM User").list(); for (Iterator iterator = users.iterator(); iterator.hasNext();){ User user= (User) iterator.next(); System.out.print("Name: " + user.getName()); System.out.println(" Salary: " + user.getSalary()); } tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } } public void updateUser(Integer id, int salary ){ Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); User user= (User)session.get(User.class, id); user.setSalary( salary ); session.update(user); tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } } public void deleteUser(Integer id){ Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); User user = (User)session.get(User.class, id); session.delete(user); tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。