Hibernate入门 广东精鹰软件工作室(刘正仁)

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CUSTOMERS]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[CUSTOMERS]
GO

CREATE TABLE [dbo].[CUSTOMERS] (
 [ID] [bigint] NOT NULL ,
 [NAME] [varchar] (15) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [EMAIL] [varchar] (128) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [PASSWORD] [varchar] (8) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [PHONE] [int] NULL ,
 [ADDRESS] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

 

 

hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.driver_class=com.microsoft.jdbc.sqlserver.SQLServerDriver
hibernate.connection.url=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Hibernate
hibernate.connection.username=sa
hibernate.connection.password=bb
hibernate.show_sql=true

 

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <class name="com.bean.Customer" table="CUSTOMERS">
    
 
    <id name="id" column="ID" type="long">
      <generator class="increment"/>
    </id>
    <property name="name"  column="NAME"  type="string" not-null="true" /> 
    <property name="email"     column="EMAIL"     type="string" not-null="true" />
    <property name="password"  column="PASSWORD"  type="string" not-null="true"/>
    <property name="phone"     column="PHONE"     type="int" />
    <property name="address"   column="ADDRESS"   type="string" /> 

  </class>

</hibernate-mapping>

 

 

/*
 * 创建日期 2005-6-28
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.bean;

/**
 * @author Administrator
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
import java.io.Serializable;


public class Customer implements Serializable {
  private Long id;
  private String name;
  private String email;
  private String password;
  private int phone;
  private String address;


  public Customer(){}

  public Long getId(){
 return id;
  }

  public void setId(Long id){
 this.id = id;
  }

  public String getName(){
 return name;
  }

  public void setName(String name){
 this.name=name;
  }

  public String getEmail(){
 return email;
  }

  public void setEmail(String email){
 this.email =email ;
  }

  public String getPassword(){
 return password;
  }

  public void setPassword(String password){
   this.password =password ;
  }

  public int getPhone(){
 return phone;
  }

  public void setPhone(int phone){
 this.phone =phone ;
  }

  public String getAddress(){
 return address;
  }

  public void setAddress(String address){
 this.address =address ;
  }


}

 


package com.service;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import java.util.*;
import com.util.FactorySession;
import javax.servlet.*;
import javax.servlet.http.*;
import  com.bean.*;
import java.io.*;

public class CustomerService extends HttpServlet{
 
 public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
 {
   Session session=null;
   Transaction tx=null;
   try {
  
    session=FactorySession.getSession();
    tx=session.beginTransaction();
   Customer c=new Customer();
   c.setName("zmbb");
   c.setEmail("zhengmenbb@163.com");
   c.setPassword("81101123");
   c.setPhone(13808);
   c.setAddress("13808749810");
   session.save(c);
   tx.commit();
  }catch (Exception e) {
    try{
       tx.rollback();
        }catch (Exception ex) {System.out.println("test");}
   
  }finally {
   try{
        session.close();
        }catch (Exception ex) {System.out.println("test");}
     }
  
 }
 
 public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
 {
  doGet(request,response);
 }

 
 
 
}

 

package com.util;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import com.bean.Customer;

public class FactorySession
{
 public static SessionFactory sessionFactory;
    static
    {
  try
  {
     Configuration config = new Configuration();
     config.addClass(Customer.class);
     sessionFactory = config.buildSessionFactory();
  }
  catch(Exception e){e.printStackTrace();}
   }
  
   public static Session  getSession() throws Exception
   {
     Session session = sessionFactory.openSession();
     return session;
   }
  
   public static void closeSession(Session session) throws Exception
   {
     if(session!=null)
      session.close();
   }

}

 

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

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
  <display-name>HelloApp Struts Application</display-name>
 <!-- The Usual Welcome File List -->
<servlet>
 <servlet-name>
  test
 </servlet-name>
 <servlet-class>
 com.service.CustomerService
 </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
  test
 </servlet-name>

<url-pattern>/hello</url-pattern>
</servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值