Hibernate实例(一)

 一.在Sql Server中建立数据表MYPD_Information(字段比较多)。
    sql语句如下:
CREATE TABLE [dbo].[MYPD_Information] (
 [ID] [int] IDENTITY (1, 1) NOT NULL ,
 [ShowName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [Type] [nvarchar] (4) COLLATE Chinese_PRC_CI_AS NULL ,
 [PriceTerms] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [Keywords] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [Content] [text] COLLATE Chinese_PRC_CI_AS NULL ,
 [Picture] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
 [UserId] [int] NULL ,
 [Sortid] [int] NULL ,
 [Add_time] [datetime] NULL ,
 [End_time] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
 [hits] [int] NULL ,
 [Auditing] [int] NULL ,
 [AdminID] [int] NULL ,
 [recommendation] [int] NULL ,
 [keys] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [keysendtime] [datetime] NULL
)
二.映射文件:MypdInformation.hbm.xml。(不要担心复杂,其实可以通过Eclipse自动生成,事先要生成 hibernate.cfg.xml和HibernateSessionFactory.java文件)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="org.yao.MypdInformation" table="MYPD_Information" schema="dbo" >
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="showName" type="java.lang.String">
            <column name="ShowName" length="50" />
        </property>
        <property name="type" type="java.lang.String">
            <column name="Type" length="4" />
        </property>
        <property name="priceTerms" type="java.lang.String">
            <column name="PriceTerms" length="50" />
        </property>
        <property name="keywords" type="java.lang.String">
            <column name="Keywords" length="50" />
        </property>
        <property name="content" type="java.lang.String">
            <column name="Content" />
        </property>
        <property name="picture" type="java.lang.String">
            <column name="Picture" length="100" />
        </property>
     
      
        <property name="addTime" type="java.util.Date">
            <column name="Add_time" length="23" />
        </property>
        <property name="endTime" type="java.lang.String">
            <column name="End_time" length="10" />
        </property>
        <property name="hits" type="java.lang.Integer">
            <column name="hits" />
        </property>
        <property name="auditing" type="java.lang.Integer">
            <column name="Auditing" />
        </property>
        <property name="adminId" type="java.lang.Integer">
            <column name="AdminID" />
        </property>
        <property name="recommendation" type="java.lang.Integer">
            <column name="recommendation" />
        </property>
        <many-to-one name="syscorporation"
        class="org.yao.SysCorporation"
         column="userId"
      outer-join="true"
         ></many-to-one>
        <many-to-one name="mypdclass" class="org.yao.MypdClass"
   column="Sortid"  insert="false" update="false" not-found="ignore" lazy="false">
  </many-to-one>
    </class>
</hibernate-mapping>

其中的详细解释,可以找一本相关的书来看。
三.对象类:MypdInformation.java。(也可以通过Eclipse自动生成)

package org.yao;

import java.util.Date;

/**
 * MypdInformation generated by MyEclipse - Hibernate Tools
 */

public class MypdInformation implements java.io.Serializable {

 // Fields

 private Integer id;

 private String showName;

 private String type;

 private String priceTerms;

 private String keywords;

 private String content;

 private String picture;

 private Date addTime = new Date();

 private String endTime;

 private Integer hits;

 private Integer auditing;

 private Integer adminId;

 private Integer recommendation;

 private SysCorporation syscorporation;

 private MypdClass mypdclass;

 // Constructors

 public MypdClass getMypdclass() {
  return mypdclass;
 }

 public void setMypdclass(MypdClass mypdclass) {
  this.mypdclass = mypdclass;
 }

 public SysCorporation getSyscorporation() {
  return syscorporation;
 }

 public void setSyscorporation(SysCorporation syscorporation) {
  this.syscorporation = syscorporation;
 }

 /** default constructor */
 public MypdInformation() {
 }

 /** full constructor */
 public MypdInformation(String showName, String type, String priceTerms,
   String keywords, String content, String picture, String endTime,
   Integer hits, Integer auditing, Integer adminId,
   Integer recommendation) {
  this.showName = showName;
  this.type = type;
  this.priceTerms = priceTerms;
  this.keywords = keywords;
  this.content = content;
  this.picture = picture;

  this.endTime = endTime;
  this.hits = hits;
  this.auditing = auditing;
  this.adminId = adminId;
  this.recommendation = recommendation;

 }

 public MypdInformation(String pic) {
  this.picture = pic;
 }

 // Property accessors

 public Integer getId() {
  return this.id;
 }

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

 public String getShowName() {
  return this.showName;
 }

 public void setShowName(String showName) {
  this.showName = showName;
 }

 public String getType() {
  return this.type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public String getPriceTerms() {
  return this.priceTerms;
 }

 public void setPriceTerms(String priceTerms) {
  this.priceTerms = priceTerms;
 }

 public String getKeywords() {
  return this.keywords;
 }

 public void setKeywords(String keywords) {
  this.keywords = keywords;
 }

 public String getContent() {
  return this.content;
 }

 public void setContent(String content) {
  this.content = content;
 }

 public String getPicture() {
  return this.picture;
 }

 public void setPicture(String picture) {
  this.picture = picture;
 }

 public Date getAddTime() {
  return this.addTime;
 }

 public void setAddTime(Date addTime) {
  this.addTime = addTime;
 }

 public String getEndTime() {
  return this.endTime;
 }

 public void setEndTime(String endTime) {
  this.endTime = endTime;
 }

 public Integer getHits() {
  return this.hits;
 }

 public void setHits(Integer hits) {
  this.hits = hits;
 }

 public Integer getAuditing() {
  return this.auditing;
 }

 public void setAuditing(Integer auditing) {
  this.auditing = auditing;
 }

 public Integer getAdminId() {
  return this.adminId;
 }

 public void setAdminId(Integer adminId) {
  this.adminId = adminId;
 }

 public Integer getRecommendation() {
  return recommendation;
 }

 public void setRecommendation(Integer recommendation) {
  this.recommendation = recommendation;
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值