Hibernate入门 刘正仁

2008-02-23 09:34:54来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java2实用教程(第2版)(清华版)

下一篇:Experienced In Lucene