• 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>

    数据运行成功的结果:

    作者:马家升
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    C#中利用iTextSharp开发二维码防伪标签(1)
    delphi 数据库中Connection与Query连接数量问题思考
    cPanel 安装方法
    招商行用卡人工服务方式
    软链接的创建和查看
    zencart低版本由php5.2.17升级PHP5.3环境下错误及解决方案
    EXCEL应用:高级筛选里的条件或和与的条件怎么写 例:不包含,包含等
    array_walk与array_map 的不同 array_filter
    zen cart global $db 这噶哒
    hdu 5655 CA Loves Stick
  • 原文地址:https://www.cnblogs.com/majiasheng/p/10080131.html
Copyright © 2020-2023  润新知