Hibernate 专题
专题目录
您的位置:java > Hibernate 专题 > Web应用程序使用Hibernate
Web应用程序使用Hibernate
作者:--    发布时间:2019-11-20

在本文中,我们将学习使用hibernate创建一个web应用程序。 对于创建web应用程序,我们使用jsp表示逻辑层,使用bean类表示数据,以及使用dao类操作数据库。
在hibernate中创建简单的应用程序时,不需要在hibernate中执行额外的操作来创建web应用程序。 在这个示例中,我们使用jsp文件获取用户的值。

首先打开 myeclipse ,创建一个动态web项目:web-application-with-hibernate,其结果如下 -

使用hibernate创建web应用程序的示例

在这个例子中,我们将在数据库中插入用户的记录。它只是实现一个简单的注册表单的处理,即:从表单获取用户的输入信息,并将数据插入到对应的数据表中。

index.jsp

该页面用于从用户获取输入,并使用post方法将其发送到register.jsp文件。

<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>注册表单</title>
</head>
<body>
    <form action="register.jsp" method="post">
        <h2>用户注册表单</h2>
        <hr/>
        name:<input type="text" name="name" /><br>
        <br /> password:<input type="password" name="password" /><br>
        <br /> email:<input type="text" name="email" /><br>
        <br /> <input type="submit" value="注册" />
    </form>
</body>
</html>

register.jsp

这个文件获取所有请求参数,并将此信息存储到user类的对象中。 此外,它调用userdao类的 register() 方法传递user类对象。

<%@page import="com.h3.userdao"%>
<jsp:usebean id="obj" class="com.h3.user"></jsp:usebean>
<jsp:setproperty property="*" name="obj" />

<%
    int i = userdao.save(obj);
    if (i > 0) {
        out.print("you are successfully registered");
    }
%>

user.java

它是在hibernate中表示持久类的简单bean类。

package com.h3;

public class user {
    private int id;
    private string name, password, email;

    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

    public string getname() {
        return name;
    }

    public void setname(string name) {
        this.name = name;
    }

    public string getpassword() {
        return password;
    }

    public void setpassword(string password) {
        this.password = password;
    }

    public string getemail() {
        return email;
    }

    public void setemail(string email) {
        this.email = email;
    }

}

user.hbm.xml

它用于与user类与数据库的表映射。

<?xml version='1.0' encoding='utf-8'?>
<!doctype hibernate-mapping public
          "-//hibernate/hibernate mapping dtd 3.0//en"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.h3.user" table="tb_user">
        <id name="id">
            <generator class="increment"></generator>
        </id>
        <property name="name"></property>
        <property name="password"></property>
        <property name="email"></property>
    </class>

</hibernate-mapping>

userdao.java

一个dao类,包含存储user类的实例的方法。

package com.h3;

import org.hibernate.*;
import org.hibernate.boot.metadatasources;
import org.hibernate.boot.registry.standardserviceregistry;
import org.hibernate.boot.registry.standardserviceregistrybuilder;
import org.hibernate.cfg.*;


public class userdao {

    public static int save(user u) {
        int i = 0;

        final standardserviceregistry registry = new standardserviceregistrybuilder()
                .configure("hibernate.cfg.xml").build();
        // 2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
        sessionfactory sessionfactory = new metadatasources(registry)
                .buildmetadata().buildsessionfactory();


        session session = sessionfactory.opensession();// 从会话工厂获取一个session
        transaction t = session.begintransaction();
        i = (integer) session.save(u);
        t.commit();
        session.close();

        system.out.println(u.getname());
        return i;
    }
}

创建数据库表

创建一个与持久化user类对象关联的表:tb_user , 其结构和创建语句如下 -

create table `tb_user` (
  `id` int(10) unsigned not null auto_increment,
  `name` varchar(32) not null default '',
  `password` varchar(32) not null default '',
  `email` varchar(32) not null default '',
  primary key (`id`)
) engine=innodb default charset=utf8;

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="hbm2ddl.auto">update</property>
        <property name="connection.driver_class">com.mysql.jdbc.driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="dialect">org.hibernate.dialect.mysqldialect</property>
        <property name="show_sql">true</property>
        <mapping resource="user.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

发布上面的工程到 tomcat sever中,打开浏览器访问url:http://localhost:8080/web-application-with-hibernate/ , 没有错误应该会看到下面界面并写入相关内容 -

提交后,看到以下结果 -

myeclipse终端输出结果如下 -

打开数据库,查看 tb_user 表应该会看到刚才插入的数据。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册