日期:2014-05-16 浏览次数:20610 次
一下代码,是我简单写的hibernate的示例,很简单,算是对hibernate的的回忆,很简单
pojo类,user,代码如下:
package com.pzoom.xiaochen.pojo; import java.util.Date; /** * * @author lenovo * */ public class User { private String id ; private String name ; private String password ; private Date date1 ; private Date date2 ; 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; } public Date getDate1() { return date1; } public void setDate1(Date date1) { this.date1 = date1; } public Date getDate2() { return date2; } public void setDate2(Date date2) { this.date2 = date2; } }
package com.pzoom.xiaochen.client; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ExportDb { public static void main(String[] args) { Configuration conf = new Configuration().configure() ; SchemaExport schema = new SchemaExport(conf) ; schema.create(true, true) ; } }
package com.pzoom.xiaochen.client; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.pzoom.xiaochen.pojo.User; /** * * @author lenovo * */ public class Client { public static void main(String[] args) { Configuration conf = new Configuration().configure() ; SessionFactory factory = conf.buildSessionFactory() ; Session session = null ; try { session = factory.openSession() ; /** * 开启事务 */ Transaction tran = session.beginTransaction() ; User user = new User(); user.setName("张三"); user.setPassword("123"); user.setDate1(new Date()); user.setDate2(new Date()); session.save(user) ; tran.commit() ; // session.getTransaction().commit() ; } catch (Exception e) { session.getTransaction().rollback() ; e.printStackTrace() ; } finally { if(session != null) { session.close() ; } } } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.pzoom.xiaochen.pojo.User" > <id name="id"> <generator class="uuid"></generator> </id> <property name="name"></property> <property name="password"></property> <property name="date1"></property> <property name="date2"></property> </class> </hibernate-mapping>主配置文件:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name="foo"> <property name="show_sql">true</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.passw