Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2314067
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: Java

2012-01-10 09:56:25

Hibernate开发第一步
1  创建Hibernate映射文件和持久化类 并子啊applicationContext.xml中进行配置
 
 
[1]对数据库表 Address进行操作 按照以前的方法 生成
 
持久化类的抽象类 AbstractAddress.java
持久化类 Address.java
映射文件 Address.hbm.xml
  1. /*
  2.  * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
  3.  * by MyEclipse Hibernate tool integration.
  4.  *
  5.  * Created Thu Aug 23 16:46:04 CST 2007 by MyEclipse Hibernate Tool.
  6.  */
  7. package com.demo.hibernate.beans;

  8. import java.io.Serializable;

  9. /**
  10.  * A class that represents a row in the address table. You can customize the
  11.  * behavior of this class by editing the class, {@link Address()}. WARNING: DO
  12.  * NOT EDIT THIS FILE. This is a generated file that is synchronized by
  13.  * MyEclipse Hibernate tool integration.
  14.  */
  15. public abstract class AbstractAddress implements Serializable {
  16.     /**
  17.      * The cached hash code value for this instance. Settting to 0 triggers
  18.      * re-calculation.
  19.      */
  20.     private int hashValue = 0;

  21.     /** The composite primary key value. */
  22.     private java.lang.Integer id;

  23.     /** The value of the simple username property. */
  24.     private java.lang.String username;

  25.     /** The value of the simple name property. */
  26.     private java.lang.String name;

  27.     /** The value of the simple *** property. */
  28.     private java.lang.String ***;

  29.     /** The value of the simple mobile property. */
  30.     private java.lang.String mobile;

  31.     /** The value of the simple email property. */
  32.     private java.lang.String email;

  33.     /** The value of the simple qq property. */
  34.     private java.lang.String qq;

  35.     /** The value of the simple company property. */
  36.     private java.lang.String company;

  37.     /** The value of the simple address property. */
  38.     private java.lang.String address;

  39.     /** The value of the simple postcode property. */
  40.     private java.lang.String postcode;

  41.     /**
  42.      * Simple constructor of AbstractAddress instances.
  43.      */
  44.     public AbstractAddress() {
  45.     }

  46.     /**
  47.      * Constructor of AbstractAddress instances given a simple primary key.
  48.      *
  49.      * @param id
  50.      */
  51.     public AbstractAddress(java.lang.Integer id) {
  52.         this.setId(id);
  53.     }

  54.     /**
  55.      * Return the simple primary key value that identifies this object.
  56.      *
  57.      * @return java.lang.Integer
  58.      */
  59.     public java.lang.Integer getId() {
  60.         return id;
  61.     }

  62.     /**
  63.      * Set the simple primary key value that identifies this object.
  64.      *
  65.      * @param id
  66.      */
  67.     public void setId(java.lang.Integer id) {
  68.         this.hashValue = 0;
  69.         this.id = id;
  70.     }

  71.     /**
  72.      * Return the value of the username column.
  73.      *
  74.      * @return java.lang.String
  75.      */
  76.     public java.lang.String getUsername() {
  77.         return this.username;
  78.     }

  79.     /**
  80.      * Set the value of the username column.
  81.      *
  82.      * @param username
  83.      */
  84.     public void setUsername(java.lang.String username) {
  85.         this.username = username;
  86.     }

  87.     /**
  88.      * Return the value of the name column.
  89.      *
  90.      * @return java.lang.String
  91.      */
  92.     public java.lang.String getName() {
  93.         return this.name;
  94.     }

  95.     /**
  96.      * Set the value of the name column.
  97.      *
  98.      * @param name
  99.      */
  100.     public void setName(java.lang.String name) {
  101.         this.name = name;
  102.     }

  103.     /**
  104.      * Return the value of the *** column.
  105.      *
  106.      * @return java.lang.String
  107.      */
  108.     public java.lang.String get***() {
  109.         return this.***;
  110.     }

  111.     /**
  112.      * Set the value of the *** column.
  113.      *
  114.      * @param ***
  115.      */
  116.     public void set***(java.lang.String ***) {
  117.         this.*** = ***;
  118.     }

  119.     /**
  120.      * Return the value of the mobile column.
  121.      *
  122.      * @return java.lang.String
  123.      */
  124.     public java.lang.String getMobile() {
  125.         return this.mobile;
  126.     }

  127.     /**
  128.      * Set the value of the mobile column.
  129.      *
  130.      * @param mobile
  131.      */
  132.     public void setMobile(java.lang.String mobile) {
  133.         this.mobile = mobile;
  134.     }

  135.     /**
  136.      * Return the value of the email column.
  137.      *
  138.      * @return java.lang.String
  139.      */
  140.     public java.lang.String getEmail() {
  141.         return this.email;
  142.     }

  143.     /**
  144.      * Set the value of the email column.
  145.      *
  146.      * @param email
  147.      */
  148.     public void setEmail(java.lang.String email) {
  149.         this.email = email;
  150.     }

  151.     /**
  152.      * Return the value of the qq column.
  153.      *
  154.      * @return java.lang.String
  155.      */
  156.     public java.lang.String getQq() {
  157.         return this.qq;
  158.     }

  159.     /**
  160.      * Set the value of the qq column.
  161.      *
  162.      * @param qq
  163.      */
  164.     public void setQq(java.lang.String qq) {
  165.         this.qq = qq;
  166.     }

  167.     /**
  168.      * Return the value of the company column.
  169.      *
  170.      * @return java.lang.String
  171.      */
  172.     public java.lang.String getCompany() {
  173.         return this.company;
  174.     }

  175.     /**
  176.      * Set the value of the company column.
  177.      *
  178.      * @param company
  179.      */
  180.     public void setCompany(java.lang.String company) {
  181.         this.company = company;
  182.     }

  183.     /**
  184.      * Return the value of the address column.
  185.      *
  186.      * @return java.lang.String
  187.      */
  188.     public java.lang.String getAddress() {
  189.         return this.address;
  190.     }

  191.     /**
  192.      * Set the value of the address column.
  193.      *
  194.      * @param address
  195.      */
  196.     public void setAddress(java.lang.String address) {
  197.         this.address = address;
  198.     }

  199.     /**
  200.      * Return the value of the postcode column.
  201.      *
  202.      * @return java.lang.String
  203.      */
  204.     public java.lang.String getPostcode() {
  205.         return this.postcode;
  206.     }

  207.     /**
  208.      * Set the value of the postcode column.
  209.      *
  210.      * @param postcode
  211.      */
  212.     public void setPostcode(java.lang.String postcode) {
  213.         this.postcode = postcode;
  214.     }

  215.     /**
  216.      * Implementation of the equals comparison on the basis of equality of the
  217.      * primary key values.
  218.      *
  219.      * @param rhs
  220.      * @return boolean
  221.      */
  222.     public boolean equals(Object rhs) {
  223.         if (rhs == null)
  224.             return false;
  225.         if (!(rhs instanceof Address))
  226.             return false;
  227.         Address that = (Address) rhs;
  228.         if (this.getId() == null || that.getId() == null)
  229.             return false;
  230.         return (this.getId().equals(that.getId()));
  231.     }

  232.     /**
  233.      * Implementation of the hashCode method conforming to the Bloch pattern
  234.      * with the exception of array properties (these are very unlikely primary
  235.      * key types).
  236.      *
  237.      * @return int
  238.      */
  239.     public int hashCode() {
  240.         if (this.hashValue == 0) {
  241.             int result = 17;
  242.             int idValue = this.getId() == null ? 0 : this.getId().hashCode();
  243.             result = result * 37 + idValue;
  244.             this.hashValue = result;
  245.         }
  246.         return this.hashValue;
  247.     }
  248. }
 
  1. /*
  2.  * Created Thu Aug 23 15:30:25 CST 2007 by MyEclipse Hibernate Tool.
  3.  */
  4. package com.demo.hibernate.beans;

  5. import java.io.Serializable;

  6. /**
  7.  * A class that represents a row in the 'address' table.
  8.  * This class may be customized as it is never re-generated
  9.  * after being created.
  10.  */
  11. public class Address extends AbstractAddress implements Serializable {
  12.     
  13.     /**
  14.      * Simple constructor of Address instances.
  15.      */
  16.     public Address() {
  17.     }

  18.     /**
  19.      * Constructor of Address instances given a simple primary key.
  20.      * @param id
  21.      */
  22.     public Address(java.lang.Integer id) {
  23.         super(id);
  24.     }

  25.     /* Add customized code below */

  26. }
  1. <?xml version="1.0" encoding='UTF-8'?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.                             "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
  4.                             "" >

  5. <!-- DO NOT EDIT: This is a generated file that is synchronized -->
  6. <!-- by MyEclipse Hibernate tool integration. -->
  7. <!-- Created Thu Aug 23 16:46:04 CST 2007 -->
  8. <hibernate-mapping package="com.demo.hibernate.beans">

  9.     <class name="Address" table="address">
  10.         <id name="id" column="ID" type="java.lang.Integer">
  11.             <generator class="native"/>
  12.         </id>

  13.         <property name="username" column="username" type="java.lang.String" not-null="true" />
  14.         <property name="name" column="name" type="java.lang.String" not-null="true" />
  15.         <property name="***" column="***" type="java.lang.String" />
  16.         <property name="mobile" column="mobile" type="java.lang.String" />
  17.         <property name="email" column="email" type="java.lang.String" />
  18.         <property name="qq" column="qq" type="java.lang.String" />
  19.         <property name="company" column="company" type="java.lang.String" />
  20.         <property name="address" column="address" type="java.lang.String" />
  21.         <property name="postcode" column="postcode" type="java.lang.String" />
  22.     </class>
  23.     
  24. </hibernate-mapping>
[2]在applicationContext.xml中注册映射文件Address.hbm.xml
要让持久化类Address.java与映射文件Address.hbm.xml能够相互匹配 需要在applicationContext.xml中注册映射文件Address.hbm.xml 只需要在名为sessionFactory的元素中添加一行即可
  1. <!-- 配置Hibernate -->
  2.     <bean id="sessionFactory"
  3.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  4.         <property name="dataSource">
  5.             <ref local="dataSource" />
  6.         </property>
  7.         <property name="mappingResources">
  8.             <list>
  9.                 <value>com/demo/hibernate/beans/User.hbm.xml</value>
  10.                 <value>com/demo/hibernate/beans/Address.hbm.xml</value>
  11.             </list>
  12.         </property>
  13.         <property name="hibernateProperties">
  14.             <props>
  15.                 <prop key="hibernate.dialect">
  16.                     org.hibernate.dialect.MySQLDialect
  17.                 </prop>
  18.                 <prop key="hibernate.show_sql">true</prop>
  19.             </props>
  20.         </property>
  21.     </bean>
阅读(1729) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~