orm环境搭建及其基本的demo

一 编写Dao类:继承Spring提供的    HibernateDaoSupport   类

接口

public interface Dao {

     public List query( String hql );
    
}

实现类

public class DaoImpl extends HibernateDaoSupport implements Dao {

     public List query(String hql) {

         return this.getHibernateTemplate().find( hql );
    }

}

二 配置applicationContext.xml文件

<? xml version ="1.0" encoding ="gb2312" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
    
< beans >

</ beans >

配置DaoImpl类:

     < bean id ="dao" class ="com.turing.hibernate.dao.DaoImpl" >
         < property name ="sessionFactory" >
             < ref local ="sessionFactory" />
         </ property >
     </ bean >

配置Hibernate的sessionFactory工厂:

     < bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
         < property name ="dataSource" >
             < ref local ="dataSource" />
         </ property >
         < property name ="mappingResources" >
             < list >
                 < value >com/turing/hibernate/test/entity/Post.hbm.xml </ value >
                 < value >com/turing/hibernate/test/entity/Author.hbm.xml </ value >
             </ list >
         </ property >
         < property name ="hibernateProperties" >
             < props >
                 < prop key ="hibernate.dialect" >org.hibernate.dialect.SQLServerDialect </ prop >
                 < prop key ="hibernate.show_sql" >true </ prop >
                 < prop key ="hibernate.jdbc.batch_size" >20 </ prop >    
                 < prop key ="hibernate.hbm2ddl.auto" >update </ prop >    
             </ props >
         </ property >
     </ bean >

配置连库使用的数据源:

     < bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
         < property name ="driverClassName" >
             < value >net.sourceforge.jtds.jdbc.Driver </ value >
         </ property >
         < property name ="url" >
             < value >jdbc:jtds:sqlserver://localhost:1433/student </ value >
         </ property >
         < property name ="username" >
             < value >sa </ value >
         </ property >
         < property name ="password" >
             < value >sa </ value >
         </ property >
     </ bean >

三 两个实体的配置文件

下载:entity.rar

四 测试

package com.turing.hibernate.test;

import java.util.List;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.turing.hibernate.dao.Dao;

/**
* @author lishiming
* @time 2010-2-9 上午09:04:21
* @author Administrator
*
*/

public class Main {

     public static void main(String[] args) {

        ClassPathResource res = new ClassPathResource( "spring.xml");
        XmlBeanFactory beanFactory = new XmlBeanFactory(res);

        Dao dao = (Dao) beanFactory.getBean( "dao");

        List list = dao.query( "from Author");
        
        System.out.println( list.size() );
        
    }

}

 


猜你喜欢

转载自blog.csdn.net/qq_36937005/article/details/80231795