1:建立maven project
2:pom中添加hibernate支持包
1 <dependency> 2 <groupId>mysql</groupId> 3 <artifactId>mysql-connector-java</artifactId> 4 <version>5.1.31</version> 5 </dependency> 6 7 <dependency> 8 <groupId>org.hibernate</groupId> 9 <artifactId>hibernate-core</artifactId> 10 <version>4.3.0.Final</version> 11 </dependency>
3:创建实体类User
1 public class User { 2 private Long id; 3 private String title; 4 private Date date; 5 }
4:配置User.hbm.xml(文件放在工程根目录下)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping > 6 <class name="com.zlt.hibernatedemo.User" table="user"> 7 <id name="id" column="id"> 8 <generator class="increment"></generator> 9 </id> 10 11 <property name="date" column="data" type="timestamp"></property> 12 <property name="title"></property> 13 </class> 14 15 </hibernate-mapping>
5:配置hibernate.cfg.xml(文件放在工程根目录下)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 8 <property name="connection.url">jdbc:mysql://localhost:3306/hibernatetest</property> 9 <property name="connection.username">root</property> 10 <property name="connection.password">root</property> 11 12 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 13 <property name="show_sql">true</property> 14 <property name="hbm2ddl.auto">create</property> 15 16 <mapping resource="User.hbm.xml"/> 17 </session-factory> 18 19 </hibernate-configuration>
6:创建session工具类 HibernateFactory.java
1 public class HibernateFactory { 2 //sessionFactory是一个线程安全的全局对象,只需要创建一次 3 public static final SessionFactory sessionFactory; 4 5 static { 6 try { 7 sessionFactory = new Configuration().configure().buildSessionFactory(); 8 } catch (Throwable ex) { 9 System.err.println("Initial SessionFactory creation failed." + ex); 10 throw new ExceptionInInitializerError(ex); 11 } 12 } 13 14 public static final ThreadLocal session = new ThreadLocal(); 15 16 public static Session currentSession() throws HibernateException { 17 Session s = (Session) session.get(); 18 // Open a new Session, if this thread has none yet 19 if (s == null) { 20 s = sessionFactory.openSession(); 21 // Store it in the ThreadLocal variable 22 session.set(s); 23 } 24 return s; 25 } 26 27 public static void closeSession() throws HibernateException { 28 Session s = (Session) session.get(); 29 if (s != null) 30 s.close(); 31 session.set(null); 32 } 33 34 }
7:测试程序
1 public class HibernateTest { 2 public static void main(String[] args) { 3 Session session = HibernateFactory.currentSession(); 4 Transaction tx = session.beginTransaction(); 5 User user = new User(); 6 user.setDate(new Date()); 7 user.setTitle("title"); 8 session.save(user); 9 10 tx.commit(); 11 session.close(); 12 } 13 }
8:结果
使用maven构建hibernate
时间: 2024-11-12 22:36:43