Hibernate开发第一步
1 创建Hibernate映射文件和持久化类 并子啊applicationContext.xml中进行配置
[1]对数据库表 Address进行操作 按照以前的方法 生成
持久化类的抽象类 AbstractAddress.java
持久化类 Address.java
映射文件 Address.hbm.xml
- /*
- * Created Thu Aug 23 15:30:25 CST 2007 by MyEclipse Hibernate Tool.
- */
- package com.demo.hibernate.beans;
- import java.io.Serializable;
- /**
- * A class that represents a row in the 'address' table.
- * This class may be customized as it is never re-generated
- * after being created.
- */
- public class Address extends AbstractAddress implements Serializable {
-
- /**
- * Simple constructor of Address instances.
- */
- public Address() {
- }
- /**
- * Constructor of Address instances given a simple primary key.
- * @param id
- */
- public Address(java.lang.Integer id) {
- super(id);
- }
- /* Add customized code below */
- }
- <?xml version="1.0" encoding='UTF-8'?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
- "" >
- <!-- DO NOT EDIT: This is a generated file that is synchronized -->
- <!-- by MyEclipse Hibernate tool integration. -->
- <!-- Created Thu Aug 23 16:46:04 CST 2007 -->
- <hibernate-mapping package="com.demo.hibernate.beans">
- <class name="Address" table="address">
- <id name="id" column="ID" type="java.lang.Integer">
- <generator class="native"/>
- </id>
- <property name="username" column="username" type="java.lang.String" not-null="true" />
- <property name="name" column="name" type="java.lang.String" not-null="true" />
- <property name="***" column="***" type="java.lang.String" />
- <property name="mobile" column="mobile" type="java.lang.String" />
- <property name="email" column="email" type="java.lang.String" />
- <property name="qq" column="qq" type="java.lang.String" />
- <property name="company" column="company" type="java.lang.String" />
- <property name="address" column="address" type="java.lang.String" />
- <property name="postcode" column="postcode" type="java.lang.String" />
- </class>
-
- </hibernate-mapping>
[2]在applicationContext.xml中注册映射文件Address.hbm.xml
要让持久化类Address.java与映射文件Address.hbm.xml能够相互匹配 需要在applicationContext.xml中注册映射文件Address.hbm.xml 只需要在名为sessionFactory的元素中添加一行即可
- <!-- 配置Hibernate -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref local="dataSource" />
- </property>
- <property name="mappingResources">
- <list>
- <value>com/demo/hibernate/beans/User.hbm.xml</value>
- <value>com/demo/hibernate/beans/Address.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- </props>
- </property>
- </bean>
阅读(1729) | 评论(0) | 转发(0) |