hibernate环境配置和使用
一.hibernate简介
二.hibernate环境搭建
1.导入hibernate核心jar包
2.添加hibernate核心配置文件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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/zhouxiang/model/User.hbm.xml"/> </session-factory> </hibernate-configuration>
3.添加表与实体间映射关系文件 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.zhouxiang.model"> <class name="User" polymorphism="explicit"> <id name="id"> <generator class="uuid" ></generator> </id> <property name="name" column="username"></property> <property name="password" column="password"></property> </class> </hibernate-mapping>
三.使用hibernate的7个步骤
1.第一步:
2.第二步:
3.第三步:打开session
4.第四步:开启事务Transaction
5.第五步:进行持久化操作,即增删查改等操作
6.第六步:提交事务
7.关闭资源,也就是关闭session
四.实例代码
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/zhouxiang/model/User.hbm.xml"/> </session-factory> </hibernate-configuration>
<?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.zhouxiang.model"> <class name="User" polymorphism="explicit"> <id name="id"> <generator class="uuid" ></generator> </id> <property name="name" column="username"></property> <property name="password" column="password"></property> </class> </hibernate-mapping>
/** * */ package com.zhouxiang.model; /** * @ClassName: User * @Description: TODO * @author zx * @date 2014年5月15日 上午10:40:43 * */ public class User { private String id; private String name; private String password; public User() {} public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
/** * */ package com.zhouxiang.test; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.zhouxiang.model.User; /** * @ClassName: Test1 * @Description: TODO * @author zx * @date 2014年5月16日 上午10:09:55 * */ public class Test1 { public static void main(String args[]) { SessionFactory factory=null; Session session=null; try { Configuration cfg = new Configuration().configure(); factory=cfg.buildSessionFactory(); session=factory.openSession(); session.getTransaction().begin(); User user=new User(); user.setName("bbb"); user.setPassword("123456"); session.save(user); session.getTransaction().commit(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); session.getTransaction().rollback(); } finally { if(session!=null) { if(session.isOpen()) { session.close(); } } } } }
五.总结
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。