设为首页 收藏本站
查看: 752|回复: 0

[经验分享] JBuilderX+SQL Server开发hibernate

[复制链接]

尚未签到

发表于 2016-10-30 06:29:52 | 显示全部楼层 |阅读模式
  环境:

   开发的IDE:JBuilderX
 
   使用的数据库:MS Sql Server 2000
 
   使用的数据库驱动:JSQL Driver(JDBC 3.0)

  说明:

  1、hibernate在配置文件中明确说明“Microsoft Driver (not recommended!)”,因此先使用JSQL Driver。
 
  2、JSQL Driver可以到http://www.jnetdirect.com中得到,需要先注册个用户,才能下载到试用的版本。

  3、JDBC3.0只能在JDK1.4及以上版本中使用,JBuilderX默认的是JDK1.4

  准备工作:

  1、下载Hibernate,目前最高版本是2.1.2

  2、在JBuilder中创建一个lib,起名为hibernate_full,将hibernate\lib下的所有jar通通放进去,并将hibernate\hibernate2.jar也放进去

  3、在JBuilder中创建一个lib,起名为JSQL3,将JSQL Driver下的JNetDirect\JSQLConnect\JDBC_3.0_Driver\JSQLConnect.jar放进去

  开始进行例子:
 
  1、创建一个project,命名为testhibernate

  2、在属性里的Required Libraries里加入hibernate_full和JSQL3

  3、在菜单Project --> Project Properties --> Build --> Resource 里选中xml文件,选择“Copy” --在编译该项目的时候,会自动将src文件夹里的xml文件拷贝到classes文件夹里的相应目录下

  4、在testhibernate项目中创建一个src目录

  5、将hibernate源文件里的hibernate\src\hibernate.properties 和 log4j.properties拷贝到testhibernate项目中的src目录下

  6、修改hibernate.properties中关于MS Sql Server 2000驱动方面的配置

  找到

## HypersonicSQL

hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class org.hsqldb.jdbcDriver
hibernate.connection.username sa
hibernate.connection.password
hibernate.connection.url jdbc:hsqldb:hsql://localhost
hibernate.connection.url jdbc:hsqldb:test
hibernate.connection.url jdbc:hsqldb:.

  这段,这里是说默认的是使用HypersonicSQL,我们使用的是MS Sql Server,因此将整段注释掉

## HypersonicSQL

#hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
#hibernate.connection.driver_class org.hsqldb.jdbcDriver
#hibernate.connection.username sa
#hibernate.connection.password
#hibernate.connection.url jdbc:hsqldb:hsql://localhost
#hibernate.connection.url jdbc:hsqldb:test
#hibernate.connection.url jdbc:hsqldb:.

  并且,找到

## MS SQL Server

#hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
#hibernate.connection.username sa
#hibernate.connection.password sa

## JSQL Driver
#hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver
#hibernate.connection.url jdbc:JSQLConnect://1E1/test

  这段,比如我们使用的数据库服务器机器名为yuj,数据库名为testhi,连接到数据库上去的用户名为sa,密码为sa,则修改后这段成为

## MS SQL Server

hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.username sa
hibernate.connection.password sa

## JSQL Driver
hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver
hibernate.connection.url jdbc:JSQLConnect://yuj/testhi

  7、创建一个类testhibernate.Person,这是个标准的JavaBean,只有3个属性和相应的get\set方法

package testhibernate;

public class Person
{
 private String id;
 private String name;
 private String address;

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

 public String getId()
 {
  return id;
 }

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

 public String getName()
 {
  return name;
 }

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

 public String getAddress()
 {
  return address;
 }
}


  8、创建一个对象-关系映射的xml文件Person.hbm.xml,放在和Person.java相同的目录下面

<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
 <class name="testhibernate.Person">
  <!--hibernate为我们生成主键id-->
  <id name = "id" unsaved-value = "null">
   <generator class="uuid.hex"/>
  </id>

  <!--默认把类的变量映射为相同名字的表列,当然我们可以修改其映射方式-->
  <property name="name"/>
  <property name="address"/>
 </class>
</hibernate-mapping>

  9、创建调用类Person的客户端程序Client1.java

package testhibernate;

import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

/**
*本类只是用来创建表的,并不往表内部插入任何数据,并且只能使用一次,否则会删除已有的表的
*/
public class Client1
{
 private static SessionFactory sessionFactory;

 public static void main(String[] args) throws Exception
 {
  Configuration conf = new Configuration().addClass(Person.class);

  //第一次运行时用来在数据库中创建表
  //并且把sql语句输出到txt文件用的
  //以后的运行不能使用该段代码,否则每次都会先删除原表,再新建该表
  SchemaExport dbExport = new SchemaExport(conf);
  dbExport.setOutputFile("sql.txt");
  dbExport.create(true, true);
 }
}

package testhibernate;

import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

public class Client2
{
 private static SessionFactory sessionFactory;

 public static void main(String[] args) throws Exception
 {
  Configuration conf = new Configuration().addClass(Person.class);
  sessionFactory = conf.buildSessionFactory();
  Session s = sessionFactory.openSession();

  Transaction t = s.beginTransaction();

  Person yuj = new Person();
  yuj.setName("john");
  yuj.setAddress("上海");

  Person x = new Person();
  x.setName("zhaoyh");
  x.setAddress("上海");

  //持久化
  s.save(yuj); //此时yuj已经可以在数据库中找到
  s.save(x); //此时x已经可以在数据库中找到

  t.commit();
  s.close();
 }
}

  查看数据库中,增加了2条记录,OK!初步使用成功了,剩下的慢慢研究吧……
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-293066-1-1.html 上篇帖子: SQL Server编程入门 下篇帖子: SQL Server常用语句(二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表