生活随笔
收集整理的這篇文章主要介紹了
hibernate demo 搭建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
User 接口:
public class User implements java.io.Serializable {private int id;private String name;private String password;private String type;User(){}public int getId(){return this.id;}public void setId(int id){this.id = id;}public void setName(String name){this.name = name;}public String getName(){return this.name;}public void setPassword(String password){this.password =password;}public String getPassword(){return this.password;}public void setType(String type){this.type = type;}public String getType(){return this.type;}
}
User.hbm.xml 映射文件
<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- name 指定持久化類名 table 指定數據表-->
<class name="org.hibernate.entity.User" table="USER">
<!--將數據表和bean 映射對應起來-->
<id name="id" type="java.lang.Interger" column="USER_ID">
<generator class="increment">
</id><property name="name" type="java.lang.String" column="NAME" length="20">
</property><property name="password" type="java.lang.String" column="NAME" length="12">
</property><property name="type" type="java.lang.String" column="TYPE" length="6">
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml ?配置文件
<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Configration DTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 數據庫鏈接設置-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--數據庫url-><property name="hibernate.connection.url" >
jdbc:mysql://localhost:3306/mysqldb
</property><property name="hibernate.connection.username" >mysql</property><property name="hibernate.connection.password" >123456</property>
<property name="hibernate.connection.pool_size" >1</property>
<property name="dialect" >org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql" >true</property>
<!--列出所有映射文件-->
<mapping resource="org/hibernate/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
UserADO 接口類:
public interface UserDAO {void save(User user);User findById(int id);void delete(User user);void update(User user);
}
H會bernateUtil實現類:
public class HibernateUtil{private static SessionFactory sessionFactory;private static final ThreadLocal<Session> threadLocal= new ThreadLocal<Session>();static {try{Configuration cfg = new Configuration().configure();sessionFactory = cfg.buildSessionFactory();}catch (Throwable ex){throew new ExceptionInInitializerError(ex);}}// 獲得sessionfactory實例public static SessionFactory getSessionFactory(){return sessionFactory;}public static Sesstion getSession() throws HibernateExcaption{Session session = (Session) threadLocal.get();if(session == null || !session.isOpen()){if(sessionFactory == null){rebuildSesstionFactory();}session = (sessionFactory != null)? sessionFactory.openSession():null;threadLocal.set(session);}return session;}public staitc void closeSession() throws HibernateExcaption{Session session = (Session) threadLocal.get();threadLocal.setnull();if(session == null ) {sesstion.close();}}public static void rebuildSesstionFactory(){try{confuguration.confugure.(/hibernate.cfg.xml);sessionFactory configuration.buildSessionFactory();}catch(Exception e){System.err.println("error ncreate session factory.");e.printStackTrace();}}public static void shutdown(){getSessionFactory().close();}}
UserADOImpl 實現類:public class UserADOImpl implements UserDAO{public void save(User user){Session session = HibernateUtil.getSession();Transaction tx =session.beginTransaction();try{session.save(user);tx.commit();}catch(Exception e){e.printStackTrace();tx.rollback();}finally {HibernateUtil.closeSession();}}public void delete(User user){Session session = HibernateUtil.getSession();Transaction tx =session.beginTransaction();try{session.delete(user);tx.commit();}catch(Exception e){e.printStackTrace();tx.rollback();}finally {HibernateUtil.closeSession();}}public void update(User user){Session session = HibernateUtil.getSession();Transaction tx =session.beginTransaction();try{session.update(user);tx.commit();}catch(Exception e){e.printStackTrace();tx.rollback();}finally {HibernateUtil.closeSession();}}public User findByyId(int id){User user = null;Session session = HibernateUtil.getSession();Transaction tx =session.beginTransaction();try{user = session.get(User.class, id);tx.commit();}catch(Exception e){e.printStackTrace();tx.rollback();}finally {HibernateUtil.closeSession();}return user;}
}
總結
以上是生活随笔為你收集整理的hibernate demo 搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。