第一个Hibernate 程序终于测试通过了

   
   java 好多地方都要自己配置,习惯了 vs 的东西,真是不适应。

   CurUser.java
package test;

public class CurUser {
    
private String Curid;

    

    
private String Name;



    
public String getCurid() {
        
return Curid;
    }


    
public void setCurid(String id) {
        
this.Curid = id;
    }



    
public String getName() {
        
return Name;
    }


    
public void setName(String name) {
        Name 
= name;
    }


    
}


CurUser.hbm.xml
<?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="test.CurUser" table="test">



    
<id name="Curid" type="string"  unsaved-value="null">
           
<generator class="uuid.hex"/>
    
</id>

   

    
<property name="Name" type="string"/>
 
   

 
</class>
</hibernate-mapping>

 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="connection.username">root</property>
    
<property name="connection.url">
        jdbc:mysql://localhost/first
    
</property>
    
<property name="dialect">
        org.hibernate.dialect.MySQLDialect
    
</property>
    
<property name="connection.password">root</property>
    
<property name="connection.driver_class">
        com.mysql.jdbc.Driver
    
</property>
    
<mapping resource="test/CurUser.hbm.xml" />

</session-factory>

</hibernate-configuration>

 测试页面
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="org.hibernate.*"%>
<%@ page import="org.hibernate.cfg.*"%>

<%@ page import="test.CurUser"%>

<%@ page import="java.sql.*"%>


<%
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 'index.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>
 
<%  try{
    
//获得Hibernate配置
    SessionFactory sf
=
           
new Configuration().configure().buildSessionFactory();
    
//开启连接
    Session ses
=sf.openSession();
    
//开启事务
    Transaction tx
=ses.beginTransaction();
    
//连续写入10条纪录
    
for(int i=0;i<10;i++)
      {
        CurUser cr
=new CurUser();
        cr.setName(
"abc");
        ses.save(cr);
      }
    
//提交事务
    tx.commit();
    
//关闭连接
    ses.close();
    out.println(
"Hibernate 测试成功!");
   }
   
   catch(HibernateException e)
   {
      e.printStackTrace();
      out.println(
"Hibernate 测试异常!");
      out.println(e.toString());
   }
    
     
%>
  
</body>
</html>


 
create database if not exists `first`;

USE `first`;

/*数据表 `test` 的表结构*/

DROP TABLE IF EXISTS `test`;

CREATE TABLE `test` (
  `Curid` 
varchar(50) collate utf8_unicode_ci default NULL,
  `name` 
varchar(50) collate utf8_unicode_ci default NULL
) ENGINE
=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

原文地址:https://www.cnblogs.com/gwazy/p/965406.html