层次结构
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>
- <property name="show_sql">true</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.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">mysqladmin</property>
- <mapping resource="org/zbq/bean/User.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
User.
Java
- package org.zbq.util;
- import java.util.List;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import org.hibernate.service.ServiceRegistryBuilder;
- import org.zbq.bean.User;
- public class HibernateUtil {
- private static SessionFactory sessionFactory;
- static{
- try{
- Configuration conf = new Configuration();
- conf.configure();
- ServiceRegistry sr = new ServiceRegistryBuilder()
- .applySettings(conf.getProperties())
- .buildServiceRegistry();
- sessionFactory = conf.buildSessionFactory(sr);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void save(User user) throws Exception{
- Transaction tr = null;
- Session session = sessionFactory.openSession();
- try{
- tr = session.beginTransaction();
- // tr.begin();
- session.save(user);
- tr.commit();
- } catch (Exception e) {
- if(null != tr){
- tr.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- @SuppressWarnings("unchecked")
- public static List<User> listUsers() throws Exception{
- List<User> list = null;
- Transaction tr = null;
- Session session = sessionFactory.openSession();
- try{
- tr = session.beginTransaction();
- // tr.begin();
- String sql = "from User";
- Query query = session.createQuery(sql);
- list = (List<User>)query.list();
- tr.commit();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- session.close();
- }
- return list;
- }
- }
User.hbm.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="org.zbq.bean.User" table="user">
- <id name="id" column="id" type="int">
- <generator class="increment"/>
- </id>
- <property name="name" column="name" type="string"/>
- <property name="pass" column="pass" type="string"/>
- </class>
- </hibernate-mapping>
HibernateUtil.java
- package org.zbq.util;
- import java.util.List;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import org.hibernate.service.ServiceRegistryBuilder;
- import org.zbq.bean.User;
- public class HibernateUtil {
- private static SessionFactory sessionFactory;
- static{
- try{
- Configuration conf = new Configuration();
- conf.configure();
- ServiceRegistry sr = new ServiceRegistryBuilder()
- .applySettings(conf.getProperties())
- .buildServiceRegistry();
- sessionFactory = conf.buildSessionFactory(sr);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void save(User user) throws Exception{
- Transaction tr = null;
- Session session = sessionFactory.openSession();
- try{
- tr = session.beginTransaction();
- // tr.begin();
- session.save(user);
- tr.commit();
- } catch (Exception e) {
- if(null != tr){
- tr.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- @SuppressWarnings("unchecked")
- public static List<User> listUsers() throws Exception{
- List<User> list = null;
- Transaction tr = null;
- Session session = sessionFactory.openSession();
- try{
- tr = session.beginTransaction();
- // tr.begin();
- String sql = "from User";
- Query query = session.createQuery(sql);
- list = (List<User>)query.list();
- tr.commit();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- session.close();
- }
- return list;
- }
- }
测试类
- package org.zbq.test;
- import java.util.List;
- import org.zbq.bean.User;
- import org.zbq.util.HibernateUtil;
- public class HibernatUtilTest {
- public static void main(String[] args) throws Exception {
- User user = new User();
- user.setId(2);
- user.setName("cat");
- user.setPass("intel123");
- HibernateUtil.save(user);
- List<User> list = HibernateUtil.listUsers();
- for(User u : list){
- System.out.println(u.getName() + ":" + u.getPass());
- }
- }
- }