用 websphere 操作 hibernate 连 MSSQL

来源:互联网 发布:雅思考试与高口 知乎 编辑:程序博客网 时间:2024/06/07 22:54

 网上操作hibernate的列子很多.不过都没有很详细完整的例字.最近参照网上一哥们的例子写了个完整的例子,废话少说,具体如下

第一步:新建一个项目.名字为TestHibernate

第二步:新建Customer类

package test;

public class Customer {

  private int id;
     private String username;
     private String password;

 /**
  * @return
  */
 public int getId() {
  return id;
 }

 /**
  * @return
  */
 public String getPassword() {
  return password;
 }

 /**
  * @return
  */
 public String getUsername() {
  return username;
 }

 /**
  * @param i
  */
 public void setId(int i) {
  id = i;
 }

 /**
  * @param string
  */
 public void setPassword(String string) {
  password = string;
 }

 /**
  * @param string
  */
 public void setUsername(String string) {
  username = string;
 }

}

第三步: 创建数据库hibernate_test,创建表

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

CREATE TABLE [dbo].[CUSTOMER] (
 [CID] [int] NOT NULL ,
 [USERNAME] [varchar] (12) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [PASSWORD] [varchar] (12) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

第四步:创建hibernate.cfg.xml文件

<!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="show_sql">true</property>
  <property name="myeclipse.connection.profile">MSSQL</property>
  <property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=hibernate_test</property>
  <property name="connection.username">sa</property>
  <property name="connection.password">sa</property>
  <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
  <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
  <!-- Mapping files -->
  <mapping resource="test/Customer.hbm.xml" />
 </session-factory>
</hibernate-configuration>
第五步:创建Customer.hbm.xml文件

<?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="test">
 <class name="Customer" table="CUSTOMER">
  <id name="id" column="CID">
   <generator class="increment" />
  </id>
  <property name="username" column="USERNAME" />
  <property name="password" column="PASSWORD" />
 </class>
</hibernate-mapping>
第六步:编写测试代码

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.dialect.SQLServerDialect;

/**
 * @author xianglp
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class Test {

 public static void main(String[] args) {
  try {
   SessionFactory sf =
    new Configuration().configure().buildSessionFactory();
   Session session = sf.openSession();
   Transaction tx = session.beginTransaction();

      for (int i = 0; i < 200; i++) {
       Customer customer = new Customer();
       customer.setUsername("customer" + i);
       customer.setPassword("customer");
       session.save(customer);
      }

  
   
   Query query =
    session.createQuery("from Customer Where id =:id");
    
   query.setParameter("id",new Integer(1));

    
   List list = query.list();
   
   Customer c = (Customer)list.get(0);
   
   c.setUsername("mmmmmmmmm");
   session.save(c);

   tx.commit();
   session.close();
  } catch (HibernateException e) {
   e.printStackTrace();
  }

 }
}

以上就是所有步骤,不过对于初学者来说还有注意包的引用,具体图形如下

http://p.blog.csdn.net/images/p_blog_csdn_net/cdsgajxlp/未命名.bmp

按如下步骤,基本上没有问题了

 

 

原创粉丝点击