myeclipse下hibernate入门实例介绍

myeclipse下hibernate初步实例

说明:本例采用myeclipse+sqlserver2000
 hibernate版本为3.0.
 数据库驱动为:jtds-1.0.2。

1。配置myeclipse环境(eclipse-SDK-3.1.1+myeclipse4.1.1GA_E3.1)。
2.配置好sqlserver2000数据库:建立数据库:goto  . 表 test_products

CREATE TABLE [dbo].[test_products] (
 [id] [int] IDENTITY (1, 1) NOT NULL ,
 [aotoid] [char] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [username] [char] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [price] [int] NULL ,
 [amount] [int] NULL
)
id为自动编号。

3。新建项目--myeclipse--Web Project 。本例为:prodcast
4. 右单击prodcast,加入myeclipse的hibernate.并在此工程中建立数据连结。
建立完成后,在src下自动生成hibernate.cfg.xml,内容为:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
 <property name="connection.username">sa</property>
 <property name="connection.url">jdbc:jtds:sqlserver://192.168.1.88:1433;DatabaseName=goto</property>
 <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
 <property name="connection.password">hcsys</property>
 <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>

</session-factory>

</hibernate-configuration>
5。建立:Product.java:
package test.hibernate;

import java.io.Serializable;

public class Product implements Serializable {

 private static final long serialVersionUID = 3858043967100350732L;
 private Integer id;
 private String aotoid;
 private String username;
 private Integer price;
 private Integer amount;

 public Integer getAmount() {
  return amount;
 }
 public void setAmount(Integer amount) {
  this.amount = amount;
 }
 public String getAotoid() {
  return aotoid;
 }
 public void setAotoid(String aotoid) {
  this.aotoid = aotoid;
 }
 public Integer getPrice() {
  return price;
 }
 public void setPrice(Integer price) {
  this.price = price;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 } 
}
6.建立InsertProduct.java
package test.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.*;
import org.hibernate.classic.Session;

public class InsertProduct {

 public static void main(String[] args) {
  
  SessionFactory sf = new Configuration().configure()
       .buildSessionFactory();
  Session session = sf.openSession();
  Transaction tx = session.beginTransaction();
  
  Product p  = new Product();
  p.setAotoid("1");
  p.setUsername("zhang");
  p.setAmount(new Integer(10));
  p.setPrice(new Integer(10));
  System.out.println(p+"_______________1");
  try{
   session.save(p);
   tx.commit();
   session.close();
   System.out.println(p+"_______________2");
  }catch(Exception ex){
   ex.printStackTrace();
  }  
 }
}
7.在test.hibernate包下建立:Product.hbm.xml.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
   
<hibernate-mapping package="test.hibernate">
 <class table="test_products" name="Product">
  <!--自动编号-->
  <id name="id" type="int" column="id" unsaved-value="0">
         <generator class="identity"/>
  </id>

  <property name="aotoid" column="aotoid" type="string" length="50"/> 
  <property name="username" column="username" type="string" length="50"/>    
  <property name="price" column="price" type="int"/>
  <property name="amount" column="amount" type="int"/>
 
 </class>

</hibernate-mapping>

8.打开hibernate.cfg.xml.加入:
 <mapping resource="hibernate.cfg.xml" />
 <mapping resource="test/hibernate/Product.hbm.xml" />
形成完整的config:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
 <property name="connection.username">sa</property>
 <property name="connection.url">jdbc:jtds:sqlserver://192.168.1.88:1433;DatabaseName=goto</property>
 <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
 <property name="connection.password">hcsys</property>
 <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
 <mapping resource="hibernate.cfg.xml" />
 <mapping resource="test/hibernate/Product.hbm.xml" />

</session-factory>

</hibernate-configuration>
9.运行InsertProduct.java就可以看到数据库goto表test_products中增加了一条记录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
**数字乡村解决方案摘要** **国家战略与乡村振兴** 中国正实施国家大数据战略,以加快数字中国的建设,整合数据资源,保障数据安全,并推动产业升级。2023年中央一号文件强调了乡村振兴战略,旨在通过产业兴旺、生态宜居、乡风文明、治理有效和生活富裕五大方面,实现农业农村现代化。 **数字乡村建设的重要性** 数字乡村建设是乡村振兴战略的重要组成部分,通过整合资源数据,推动物联网和遥感技术在农业中的应用,促进农业现代化。它被视为促进乡村转型发展的新方向,是数字化在农业农村经济社会发展中的应用。 **关键要素与效益** 数字乡村建设的关键要素包括数据资源整合、产业促进、农业发展规模化和标准化,以及改善乡村生活服务体系。预期效益包括提升国家治理能力,实现政府决策科学化和社会治理精准化,同时推动公共服务高效化。 **体系架构与数据融合** 数字乡村的体系架构包括乡村生态、基层治理、慧治、慧享、慧融、慧美、慧智和慧图等多个方面,实现城乡、政企、农户各级平台及服务的融合。数据融合是核心,整合乡村人口、产值、环境监测等多方面数据,为乡村治理和产业发展提供支撑。 **多业务协同与智慧治理** 数字乡村通过多业务协同应用平台,实现乡村物联网的布局和触达,涵盖农业生产、农资厂商、消费者等环节。区块链技术在农产品质量溯源中的应用,确保了产品全过程可追溯。乡村智慧治理通过村务管理、财务管理等方面提升治理效能,实现绿色发展理念。综合服务体系通过“互联网+服务”模式,提供便民服务和乡村经济发展支持,推动乡村全面振兴。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值