知识大全 hibernate入门配置

Posted

篇首语:案头见蠹鱼,犹胜凡俦侣。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 hibernate入门配置相关的知识,希望对你有一定的参考价值。

  HibernateUtil java

  package ease wireless groupsms hbnt util;

  import net sf hibernate HibernateException;

  import net sf hibernate Session;

  import net sf hibernate cfg Configuration;

  /**

  * Configures and provides access to Hibernate sessions tied to the

  * current thread of execution   Follows the Thread Local Session

  * pattern see @link

  */

  public class HibernateUtil

  /**

  * Location of hibernate cfg xml file

  * NOTICE: Location should be on the classpath as Hibernate uses

  * #resourceAsStream style lookup for its configuration file That

  * is place the config file in a Java package the default location

  * is the default Java package <br><br>

  * Examples: <br>

  * <code>CONFIG_FILE_LOCATION = /nf xml

  * CONFIG_FILE_LOCATION = //foo/bar/nf xml </code>

  */

  private static String CONFIG_FILE_LOCATION = /hibernate cfg xml ;//hibernate cfg xml

  /** Holds a single instance of Session */

  private static final ThreadLocal threadLocal = new ThreadLocal();

  /** The single instance of hibernate configuration */

  private static final Configuration cfg = new Configuration();

  /** The single instance of hibernate SessionFactory */

  private static net sf hibernate SessionFactory sessionFactory;

  /**

  * Returns the ThreadLocal Session instance   Lazy initialize

  * the <code>SessionFactory</code> if needed

  *

  *  @return Session

  *  @throws HibernateException

  */

  public static Session currentSession() throws HibernateException

  Session session = (Session) threadLocal get();

  &nbsp;    if (session == null)

  if (sessionFactory == null)

  try

  nfigure(CONFIG_FILE_LOCATION);

  //nfigure();

  sessionFactory = cfg buildSessionFactory();

  

  catch (Exception e)

  System err println( %%%% Error Creating SessionFactory %%%% );

  System err println( ||||||||||||| + e getMessage());

  e printStackTrace();

  

  

  session = sessionFactory openSession();

  threadLocal set(session);

  

  return session;

  

  /**

  *  Close the single hibernate session instance

  *

  *  @throws HibernateException

  */

  public static void closeSession() throws HibernateException

  Session session = (Session) threadLocal get();

  threadLocal set(null);

  if (session != null)

  session close();

  

  

  /**

  * Default constructor

  */

  private HibernateUtil()

  

  

  一 建表

  cat表

  代码

  CREATE TABLE cat (

  cat_id varchar( ) NOT NULL

  NAME varchar( ) NOT NULL

  sex CHAR( )

  weight FLOAT

  PRIMARY KEY (cat_id)

  );

  二 po层(系统以cat hbm xml为准 一个xml可以写多个class)

  Xml代码

  <<<<<<<<<<<<<cat hbm xml>>>>>>>>>>>>>>>>>>>>>>>

  <?xml version= ?>

  <!DOCTYPE hibernate mapping PUBLIC //Hibernate/Hibernate Mapping DTD//EN mapping dtd >

  <hibernate mapping>

  <class name= ease wireless groupsms hbnt po Cat table= cat >

  <id name= id type= string unsaved value= null >

  <column name= cat_id sql type= varchar( ) not null= true />

  <generator class= uuid hex />

  </id>

  <property name= name >

  <column name= NAME sql type= varchar( ) not null= true />

  </property>

  <property name= sex />

  <property name= weight />

  </class>

  </hibernate mapping>

  Java代码

  <<<<<<<<<<<<<Cat java>>>>>>>>>>>>>>>>>>>>>>>>>

  package ease wireless groupsms hbnt po;

  public class Cat

  private String id;

  private String name;

  private char sex;

  private float weight;

  public Cat()

  

  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 char getSex()

  return sex;

  

  public void setSex(char sex)

  this sex = sex;

  

  public float getWeight()

  return weight;

  

  public void setWeight(float weight)

  this weight = weight;

  

  public String toString()

  String strCat = new StringBuffer()

   append(this getId()) append( )

   append(this getName()) append( )

   append(this getSex()) append( )

   append(this getWeight())

   toString();

  return strCat;

  

  

  三 hibernate cfg xml

  (切切不要自己去加属性 基本的就两个connection datasource和dialect)

  Xml代码

  <property name= connection username >root</property>

  <property name= connection password >root</property>

  <property name= connection provider_class >nnection DatasourceConnectionProvider</property>

  <property name= jndi class > gjt mm mysql Driver</property>

  <<<<<<<<<<<<<hibernate cfg xml>>>>>>>>>>>>>>>>>>>>>>>

  <?xml version= encoding= UTF ?>

  <!DOCTYPE hibernate configuration PUBLIC

   //Hibernate/Hibernate Configuration DTD //EN

   configuration dtd >

  <! DO NOT EDIT: This is a generated file that is synchronized >

  <! by MyEclipse Hibernate tool integration                    >

  <hibernate configuration>

  <session factory>

  <! properties >

  <property name= connection datasource >java:p/env/jdbc/test</property>

  <property name= show_sql >true</property>

  <property name= dialect >net sf hibernate dialect MySQLDialect</property>

  <! mapping files >

  <mapping resource= /netease/wireless/groupsms/hbnt/po/cat hbm xml />

  </session factory>

  </hibernate configuration>

  四 测试servlet

  //Session生成/关闭

  Java代码

  <<<<<<<<<<<<<测试HbntTestSvlt java>>>>>>>>>>>>>>>>>>>>>>>

  /*

  *

  * @(#)rpsms V

  * Copyright NetEase Inc All rights reserved

  *

  * coder: sweater

  * email:

  *

  * graphic designer:

  * email:

  *

  * fuction 向电话薄中添加组和电话号码

  *

  */

  package ease wireless groupsms vo;

  import java io IOException;

  import java io PrintWriter;

  import java util Iterator;

  import javax servlet ServletException;

  import javax servlet HttpServlet;

  import javax servlet HttpServletRequest;

  import javax servlet HttpServletResponse;

  import net sf hibernate HibernateException;

  import net sf hibernate Query;

  import net sf hibernate Session;

  import net sf hibernate Transaction;

  import ease wireless groupsms hbnt po Cat;

  import ease wireless groupsms hbnt util HibernateUtil;

  /**

  *

  * @author sweater

  */

  public class HbntTestSvlt extends HttpServlet

  /**

  * Constructor of the object

  */

  public HbntTestSvlt()

  super();

  

  /**

  * Destruction of the servlet <br>

  */

  public void destroy()

  super destroy(); // Just puts destroy string in log

  // Put your code here

  

  /**

  * The doGet method of the servlet <br>

  *

  * This method is called when a form has its tag value method equals to get

  *

  * @param request the request send by the client to the server

  * @param response the response send by the server to the client

  * @throws ServletException if an error occurred

  * @throws IOException if an error occurred

  */

  public void doGet(HttpServletRequest request HttpServletResponse response)

  throws ServletException IOException

  response setContentType( text/ );

  PrintWriter out = response getWriter();

  out println( <> );

  out println( <head> );

  out println( <title>Hello Hibernate</title> );

  out println( </head> );

  out println( <body> );

  out println( Hello Hibernate!<br> );

  try

  Session session = HibernateUtil currentSession();

  Transaction tx = session beginTransaction();

  Query query = session createQuery( select cat from Cat as cat where cat sex = :sex );

  query setCharacter( sex F );

  for (Iterator it = erate(); it hasNext();)

  Cat cat = (Cat) it next();

  out println( Cat: + cat toString() + <br> );

  

  mit();

  HibernateUtil closeSession();

   catch (HibernateException e)

  System out println(e getMessage());

  //System out println(e printStackTrace());

  

  out println( </body> );

  out println( </> );

  

  /**

  * The doPost method of the servlet <br>

  *

  * This method is called when a form has its tag value method equals to post

  *

  * @param request the request send by the client to the server

  * @param response the response send by the server to the client

  * @throws ServletException if an error occurred

  * @throws IOException if an error occurred

  */

  public void doPost(HttpServletRequest request HttpServletResponse response)

  throws ServletException IOException

  response setContentType( text/ );

  PrintWriter out = response getWriter();

  out println( <!DOCTYPE HTML PUBLIC \\ //W C//DTD HTML Transitional//EN\\ > );

  out println( <HTML> );

  out println(   <HEAD><TITLE>A Servlet</TITLE></HEAD> );

  out println(   <BODY> );

  out print(     This is );

  out print(this getClass());

  out println( using the POST method );

  out println(   </BODY> );

  out println( </HTML> );

  out flush();

  out close();

  

  /**

  * Returns information about the servlet such as

  * author version and copyright

  *

  * @return String information about this servlet

  */

  public String getServletInfo()

  return This is my default servlet created by Eclipse ;

  

  /**

  * Initialization of the servlet <br>

  *

  * @throws ServletException if an error occure

  */

  public void init() throws ServletException

  // Put your code here

  

  

  Xml代码

  <<<<<<<<<<<<<该servlet使用时的web xml>>>>>>>>>>>>>>>>>>>>>>>

  <?xml version= encoding= UTF ?>

  <web app version=

  xmlns=

  xmlns:xsi= instance

  xsi:schemaLocation=

   app_ _ xsd >

  <servlet>

  <description>hbnttest</description>

  <display name>hbnttest</display name>

  <servlet name>HbntTestSvlt</servlet name>

  <servlet class>ease wireless groupsms vo HbntTestSvlt</servlet class>

  </servlet>

  <servlet mapping>

  <servlet name>HbntTestSvlt</servlet name>

  <url pattern>/servlet/HbntTestSvlt</url pattern>

  </servlet mapping>

  </web app>

cha138/Article/program/Java/ky/201311/27890

相关参考

知识大全 Struts+Spring+Hibernate快速入门

Struts+Spring+Hibernate快速入门  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧

知识大全 Java开源项目Hibernate快速入门

Java开源项目Hibernate快速入门  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  其实H

知识大全 Hibernate--基础配置

Hibernate--基础配置  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  

知识大全 Hibernate的两种配置文件格式

Hibernate的两种配置文件格式  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Hibern

知识大全 MyEclipse+struts+Hibernate配置开发手册

MyEclipse+struts+Hibernate配置开发手册  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起

知识大全 在Spring中配置Hibernate事务(图)

在Spring中配置Hibernate事务(图)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  

知识大全 在Spring中配置Hibernate的事务

在Spring中配置Hibernate的事务  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  本文

知识大全 Hibernate如何配置操作多个数据库

Hibernate如何配置操作多个数据库  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 

知识大全 Hibernate中配置复合主键映射

Hibernate中配置复合主键映射  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  通常将复合主

知识大全 hibernate的关联关系配置问题

  我现在有一个借阅信息类如下  classBorrow  privateStringborrowId;//借阅流水  privateBookbook;  privateUserborrowUser;