MaxTryer

Spring ORM 支持

一、 Hibernate这些orm框架都实现JPA规范,所有hibernate配置可以通过xxx.hbm.xml,也可以通过JPA注释;

  orm持久化的entity类,必须有一个默认的无参构造器;

二、 对于实体类,最好把主键定为自增长的id,在实例化实体类后,orm可以通过它来判断对象的保存状态(不主动设置id的情况);

三、 hibernate配置持久类

  1)  hibernate默认配置:

Course

public class Course {

    private long id;
    private String title;
    private Date beginDate;
    private Date endDate;
    private int fee;
    
    //    Constructors,Getter and Setter
    ...
}

CourseDAO

1 public interface CourseDAO {
2 
3     public void save(Course course);
4     public void delete(long id);
5     public Course findById(long id);
6     public List<Course> findAll();
7 }

Course.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="test.springorm">
       
    <class name="Course" table="course">
        <id name="id" type="long" column="id">
            <generator class="identity"></generator>
        </id>
        <property name="title" type="string">
            <column name="title" not-null="true" length="150"></column>
        </property>
        ...
    </class>

</hibernate-mapping>

CourseDAOImpl、hibernate.cfg.xml略

  2)  hibernate结合JPA注释配置

Course

 1 @Entity
 2 @Table(name="course",catalog="ormtest")
 3 public class Course {
 4 
 5     @Id
 6     @GeneratedValue(strategy = GenerationType.IDENTITY)
 7     @Column(name="id")
 8     private long id;
 9     
10     @Column(name="title",length=100,nullable=false)
11     private String title;
12     ...
13 }

hibernate.cfg.xml的sessionFactory中添加<mapping class="test.ormtest.Course" />替换<mapping resource="test.ormtest.Course.hbm.xml">

  3)  spring管理hibernate

applicationContext.xml

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/ormtest?characterEncoding=UTF-8" />
        <property name="user" value="root" />
        <property name="password" value="***" />
        <property name="maxPoolSize" value="40" />
        <property name="minPoolSize" value="1" />
        <property name="initialPoolSize" value="1" />
        <property name="maxIdleTime" value="20" />
    </bean>
    
    <!-- 创建hibernate的sessionFactory实例
    1) 注释方式:AnnotationSessionFactory
    2) hibernate配置文件:LocaleSessionFactory
 -->
    <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLInnoDBDialect
                </prop>
                <!-- <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop> -->
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>testorm.pojo</value>
            </list>
        </property>
        <property name="annotatedClasses">
            <list>
            </list>
        </property>
    </bean>

  4)  DAO实现类

    Spring的HibernateTemplate方式:DAOImpl集成自HibernateDaoSupport类(Spring中的)

public abstract class HibernateDaoSupport extends DaoSupport {

    private HibernateTemplate hibernateTemplate;

    Hibernate的SessionFactory方式:DAOImpl不需要依赖SpringAPI

@Repository()
public class CourseDAOImpl implements CourseDAO {
    
    @Autowired
    private SessionFactory sf;

    @Override
    @Transactional
    public void save(Course course) {
    }
    
    ...
}

 

    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2013-04-17 18:37  MaxTryer  阅读(213)  评论(0)    收藏  举报

导航