分享
 
 
 

Hibernate入门 刘正仁

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有