日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hibernate demo 搭建

發(fā)布時(shí)間:2023/12/19 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hibernate demo 搭建 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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 指定數(shù)據(jù)表--> <class name="org.hibernate.entity.User" table="USER"> <!--將數(shù)據(jù)表和bean 映射對(duì)應(yīng)起來--> <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> <!-- 數(shù)據(jù)庫鏈接設(shè)置--> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!--數(shù)據(jù)庫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會(huì)bernateUtil實(shí)現(xiàn)類:


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實(shí)例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 實(shí)現(xiàn)類: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;} }

總結(jié)

以上是生活随笔為你收集整理的hibernate demo 搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。