Java绿地(~ming~)

Java 草地

常用链接

统计

最新评论

Spring整合Hibernate配置

SpringHibernate的整合:


a.properties
文件装配:<bean id="propertyConfiguer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="locations">  <list>

<value>classpath:mypack/jdbc.properties</value>

<value>classpath:mypack/hibernate.properties</value></list></property> </bean>

  b.数据源的装配:<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

      <property name="driverClassName" value="${db.driver}"></property>

      <property name="url" value="${db.url}"></property>

      <property name="username" value="${db.user}"></property>

      <property name="password" value="${db.password}"></property>   </bean>

 

 c. <bean id="sessionFactory"   //接管HibernatesessionFactory

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean   >

     <property name="dataSource" ref="dataSource"></property>

     <property name="mappingDirectoryLocations">  //加载映射文件

       <list> <value>classpath:mypack</value> </list> </property> //采取文件目录形式

     <property name="hibernateProperties">  <props>  //加载Hibernate属性配制

         <prop key="hibernate.dialect">${hibernate.dialect}</prop>   <prop key="hibernate.transaction.factory_class">${hibernate.transaction.factory_class}

</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>        

<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}

</prop>      </props>     </property>   </bean>

  

d.sessionFactory注入手动编写的DAO    // HibernateDaoSupport需要这个属性

<bean id="userDao" class="mypack.UserDaoHibernate">

     <property name="sessionFactory" ref="sessionFactory"></property>   </bean>

 

e.Spring DAO模式:专用APIß-DAO模板(Templete)ß-DAO支持类(Support)ß--手动编写DAO

  f.Model: public class User {private Integer id;   private String name;

                   private Date birthday;...getter/setter; }

  g.Dao接口: public interface UserDao {

    public void addUser(User user);  public void addUsers(List<User> users);

    public void updateUser(User user);  public void delete(Integer id);

    public User getUser(Integer userId);  public List getUsers();

public int getUserCounts();   public User getUserById(Integer id);  }

 

 h. public class UserDaoHibernate extends HibernateDaoSupport implements UserDao {

    //HibernateDaoSupport 中存在getHibernateTemplate方法进行数据库操作

public void addUser(User user) { getHibernateTemplate().saveOrUpdate(user);  }

saveOrUpdate:Spring自动判断id,存在就update()如游离状态,不存在就save()比如自由状态

 

public void addUsers(List<User> users) { getHibernateTemplate().saveOrUpdateAll(users);}//Spring自动遍历集合的方法

    public void delete(Integer id) {//User user=new User();user.setId(id);

//getHibernateTemplate().delete(user);删除单条纪录且不采用预处理方式

String hql="delete from User where id=?"; getHibernateTemplate().bulkUpdate(hql,id);}//进行批处理且采用预处理方式

 

    public User getUser(Integer userId) {  

        return (User) getHibernateTemplate().load(User.class, userId);

        //return (User) getHibernateTemplate().get(User.class, userId); }

    Load返回实体代理类类型,此时类装配必须是lazy=”false”,否则session会关闭

Get返回实体类类型,lazy=”true”默认即可!

   

    public int getUserCounts() {    String hql="select count(*) from User";

return (Integer) getHibernateTemplate().find(hql).get(0); }//从集合中得第一条

 

    public List getUsers() {

   //String hql="from User"; return getHibernateTemplate().find(hql);

        String sql="from User u where u.id=? and u.name=?";

        Object[] params={1,"nihao"};   //返回多个属性得到数组

        return getHibernateTemplate().find(sql,params);  }

  

    public void updateUser(User user) { 

        getHibernateTemplate().update(user);    }

    public User getUserById(Integer id){//预处理得到单条纪录

        String hql="from User u where u.id=?";

        return (User) getHibernateTemplate().find(hql,id);  } }

 

i.Hibernate映射文件:<hibernate-mapping package="mypack">

   <class name="User" table="t_user" lazy="false">

      <id name="id" column="id" type="integer">

          <generator class="native"></generator>      </id>

      <property name="name" column="name" type="string"></property>

      <property name="birthday" column="birthday" type="timestamp"></property>

   </class> </hibernate-mapping>

posted on 2007-08-17 19:09 mrklmxy 阅读(2882) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: