`
zwm
  • 浏览: 281233 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论
阅读更多
Hibernate
1.DataTable--->Object;
2.Servlet DoPost/DoGet
3.TableDAO
  void method.....{
  Session session=HibernateSessionFactory.getSession();
  Tracsaction tx=session.beginTransaction();
  session.save(tableObject);
  tx.commit();
}
***********************************************************
**********************             ************************
***********************************************************
package org.zwm.beans;



/**
* BasicUser generated by MyEclipse - Hibernate Tools
*/

public class BasicUser  implements java.io.Serializable {


    // Fields   

     private Integer id;
     private String name;


    // Constructors

    /** default constructor */
    public BasicUser() {
    }

   
    /** full constructor */
    public BasicUser(String name) {
        this.name = name;
    }

  
    // Property accessors

    public Integer getId() {
        return this.id;
    }
   
    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
  








}
  • 大小: 369.3 KB
分享到:
评论
5 楼 zwm 2006-12-24  
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'Add_user.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
 
  <body>
    <form action="servlet/AddUser" method="post">
    登录姓名<input type="text" name="username"/>
    <br>
    <div align="left"><input type="submit" value="提交系统">    
    </div></form>
  </body>
</html>
4 楼 zwm 2006-12-24  
hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/myhibernateDB
</property>
<property name="connection.username">admin</property>
<property name="connection.password"></property>
<property name="connection.driver_class">
org.gjt.mm.mysql.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="org/zwm/beans/BasicUser.hbm.xml" />

</session-factory>

</hibernate-configuration>
3 楼 zwm 2006-12-24  
package org.zwm;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.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 http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {

    /**
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses 
     * #resourceAsStream style lookup for its configuration file.
     * The default classpath location of the hibernate config file is
     * in the default package. Use #setConfigFile() to update
     * the location of the configuration file for the current session.  
     */
    private static String CONFIG_FILE_LOCATION = "/org/zwm/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

    private HibernateSessionFactory() {
    }

/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

        return session;
    }

/**
     *  Rebuild hibernate session factory
     *
     */
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
     *  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();
        }
    }

/**
     *  return session factory
     *
     */
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
     *  return session factory
     *
     * session factory will be rebuilded in the next call
     */
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
     *  return hibernate configuration
     *
     */
public static Configuration getConfiguration() {
return configuration;
}

}




2 楼 zwm 2006-12-24  
package org.zwm.daos;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.zwm.HibernateSessionFactory;
import org.zwm.beans.BasicUser;

public class Basic_UserDAO {
public void addBasicUser(BasicUser basicuser) {
Session session = HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
session.save(basicuser);
tx.commit();
session.close();
}
}
1 楼 zwm 2006-12-24  
BasicUser.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="org.zwm.beans.BasicUser" table="basic_user" catalog="myhibernatedb">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="20" />
        </property>
    </class>
</hibernate-mapping>

相关推荐

    hibernate jar包:hibernate-commons-annotations-4.0.1.Final.jar等

    hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-4.1.12.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar ...

    hibernate-configuration-3.0.dtd、hibernate-mapping-3.0.dtd

    《深入理解Hibernate配置与映射:hibernate-configuration-3.0.dtd与hibernate-mapping-3.0.dtd解析》 在Java世界里,Hibernate作为一款强大的对象关系映射(ORM)框架,极大地简化了数据库操作。而`hibernate-...

    hibernate-jpa-2.1-api-1.0.2.Final-API文档-中文版.zip

    赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...

    hibernate-jpa-2.1-api-1.0.2.Final-API文档-中英对照版.zip

    赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...

    hibernate-release-4.1.4

    【标题】"hibernate-release-4.1.4" 是Hibernate框架的一个版本发布,具体为4.1.4.Final。Hibernate是一个开源的对象关系映射(ORM)框架,它允许Java开发人员在处理数据库时使用面向对象的概念,极大地简化了数据库...

    Hibernate-extensions 完整安装包

    《Hibernate-Extensions全面指南》 Hibernate,作为Java领域中的一款著名对象关系映射(ORM)框架,极大地简化了数据库操作。然而,为了满足更复杂的业务需求,Hibernate还提供了丰富的扩展功能,这就是我们今天要...

    hibernate-distribution-3.3.1.GA

    很多人为了配置jpa找这个动态产生字节码的jar文件,hibernate-distribution-3.3.1.GA包太大,而hibernate-distribution-3.3.2.GA的jar没有这个jar文件,希望对大家有用

    hibernate-jpa-2.1-api-1.0.0.final-sources.jar

    hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码 hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码

    hibernate-release-5.2.10

    2. **库文件**:包含jar包,如hibernate-core.jar、hibernate-entitymanager.jar等,这些是我们在项目中引入Hibernate时需要用到的依赖库。 3. **文档**:通常包括用户指南、API文档、开发者文档等,这些文档提供了...

    hibernate-release-5.0.7.Final的所有jar包

    在这个`hibernate-release-5.0.7.Final`版本中,包含了所有相关的jar包,为开发者提供了一个完整的Hibernate ORM解决方案。 在Java开发中,jar(Java Archive)包是Java类库的打包形式,它包含了一系列的类文件和...

    hibernate-extensions和Middlegen-Hibernate

    《hibernate-extensions与Middlegen-Hibernate:数据库到Java对象的自动化转换》 在Java的持久化层开发中,Hibernate作为一款强大的ORM(对象关系映射)框架,极大地简化了数据库操作。然而,手动编写实体类和映射...

    hibernate-core-5.4.24.Final.jar

    hibernate-core-5.4.24.Final.jar

    hibernate-validator相关依赖jar包

    hibernate-validator相关依赖jar包,包括jboss-logging-3.1.0.CR2.jar,hibernate-validator-4.3.1.Final.jar,validation-api-1.0.0.GA.jar

    hibernate-commons-annotations-5.0.1.Final.jar

    在本文中,我们将深入探讨`hibernate-commons-annotations-5.0.1.Final.jar`的源码,了解其内部结构和主要功能。 一、元数据注解 HCA的核心在于提供了一系列的注解,如`@Entity`、`@Table`、`@Column`、`@Id`等,...

    hibernate-validator-5.0.0.CR2-dist.zip

    使用hibernate-validator 进行校验的jar包,里面包括了基础hibernate-validator-5.0.0.CR2.jar hibernate-validator-annotation-processor-5.0.0.CR2.jar 之外,还包括了el-api-2.2.jar javax.el-2.2.4等项目必不可...

    hibernate-validator jar 包

    hibernate-validator用于验证对象属性。包含hibernate-validator-4.3.0.Final.jar、jboss-logging-3.1.0.CR2.jar、validation-api-1.0.0.GA.jar三个包

    hibernate-jpa-2.0-api-1.0.1.Final.jar

    hibernate-jpa-2.0-api-1.0.1.Final.jar

    hibernate-jpa-2.0-api-1.0.1.Final-sources.jar

    hibernate-jpa-2.0-api-1.0.1.Final-sources.jar hibernate jpa 源代码

    hibernate-memcached包

    **hibernate-memcached包** 是一个专为Hibernate框架设计的扩展,目的是将流行的分布式内存缓存系统Memcached整合到Hibernate中,作为其二级缓存解决方案。在大型分布式应用中,缓存技术是提高性能的关键,特别是...

    hibernate-core 核心jar包

    `hibernate-core`是Hibernate的核心库,包含了执行ORM功能所需的主要组件。 在Hibernate-Core中,主要包含以下几个关键模块和概念: 1. **Session接口**:它是Hibernate的主要工作接口,提供了对数据库的操作方法...

Global site tag (gtag.js) - Google Analytics