Hibernate3.0配置

我的系统Win10(64x),Eclipse jee 2018-09 ,Sql2018版本。

以下是Hibernate3.0配置包

链接:https://pan.baidu.com/s/10KizbyeMwjnHlG4JQ8WtaA 
提取码:iptb

以下再lib下导入8个包。

以下是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" >
<hibernate-configuration>
  <session-factory>
      <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
      <property name="connection.url">jdbc:sqlserver://localhost:1433; DatabaseName=Student</property>
      <property name="connection.username">sa</property>
      <property name="connection.password">******</property>
      <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
      <property name="hbm2ddl.auto">update</property>
      <property name="show_sql">true</property>
      <mapping resource="domain/hf.hbm.xml" /><!--加入一个空格-->
  </session-factory>
</hibernate-configuration>

以下是一个用例建立hf表

hf.hbm.xml文件(跟domain再同一个package下)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="domain">
 <class name="hf">
     <id name="id" type="java.lang.Integer">
     <column name="id"/>
         <generator class="native"/>
     </id>
     <property name="name"/>
     <property name="birthday"/>
 </class>
</hibernate-mapping>

bean代码

package domain;

import java.util.Date;

public class hf {
    private Integer id;
    private String name;
    private String birthday;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
}

loginservlet类

package src.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import domain.hf;
/**
 * Servlet implementation class loginservlet
 */
public class loginservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setHeader("content-type","text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String name=request.getParameter("name");
        String birthday=request.getParameter("birthday");
        Configuration cfg =new Configuration();
        cfg.configure();
        SessionFactory sf=cfg.buildSessionFactory();
        Session se=null;
        Transaction tx=null;
        try {
            se=sf.openSession();
            tx=se.beginTransaction();
            hf user=new hf();
            user.setName(name);
            user.setBirthday(birthday);
            se.save(user);
            tx.commit();
        }catch(Exception e) {
            if(tx != null) tx.rollback();
        }finally {
            if(se != null) se.close();
            PrintWriter out=response.getWriter();
            out.println("注册操作完成!");//乱码?
            System.out.println(name);
            System.out.println(birthday);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Web.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
 <servlet-name>loginservlet</servlet-name>
 <servlet-class>src.servlet.loginservlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>loginservlet</servlet-name>
 <url-pattern>/loginservlet</url-pattern>
 </servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

login JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册提交页面</title>
</head>
<body>
<form action="loginservlet" method="post">
用户名:<input type="text" name="name"><br><br>
出生日期:<input type="text" name="birthday"><br><br>
<input type="submit" value="登录">
</form>
</body>
</html>

数据运行成功的结果:

posted on 2018-12-06 22:42  马家升  阅读(472)  评论(0编辑  收藏  举报