用 websphere 操作 hibernate 连 MSSQL

 网上操作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();
  }

 }
}

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

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

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

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值