`
zwm
  • 浏览: 283258 次
  • 性别: 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>

相关推荐

    java编写门禁系统

    ORM(Object-Relational Mapping)框架如Hibernate或MyBatis可以帮助我们更方便地进行数据库操作。 2. 用户认证:这是门禁系统的核心部分,涉及到用户身份的验证。Java提供内置的认证框架JAAS(Java Authentication...

    一个支持annotation的SSH整合示例项目

    │ │ │ │ │ ├─hibernate │ │ │ │ │ │ LoginDao.java │ │ │ │ │ │ │ │ │ │ │ ├─jdbc │ │ │ │ │ │ DataAccessExceptionTranslator.java │ │ │ │ │ │ StoredProcedureService....

    大学英语四 课后中译英翻译

    - 翻译:“I handed them several magazines that were in a plastic bag hanging on our door.” - 分析:此处需注意汉语句子结构与英语不同,需重新组织句式。 2. **表达习惯差异**: - 原文:“后来一个星期...

    java_门禁系统_附源码

    Java的文件操作、JDBC(Java数据库连接)或ORM框架(如Hibernate)可用于实现数据的持久化存储。 9. **界面设计**:虽然描述中提到的是命令行版本,但实际的门禁系统通常会有图形用户界面。Java的Swing或JavaFX库...

    java写的门禁系统

    2. **数据持久化**:用户信息和访问权限通常存储在数据库中,可以使用JDBC或ORM框架(如Hibernate)来处理数据库操作。 3. **多线程**:门禁系统可能需要同时处理多个请求,使用Java的并发库来实现线程安全的控制。 ...

    门禁系统的面向对象设计

    在门禁系统中,我们可以定义多个类,如“门”(Door)、“用户”(User)、“控制器”(Controller)和“验证模块”(AuthenticationModule)。类是对象的模板,定义了对象的属性(如门的状态,用户的权限)和方法...

Global site tag (gtag.js) - Google Analytics