1. 程式人生 > >二 SSH整合方式二:將Hibernate配置檔案交給Spring,Hibernate模版常用方法

二 SSH整合方式二:將Hibernate配置檔案交給Spring,Hibernate模版常用方法

重建一個SSH專案

java專案可以直接複製,但是web專案除了改名字還要該配置,如下:

 

Hibernate配置檔案中有哪些內容:

  • 資料庫連線的配置
  • Hibernate屬性的配置:方言,顯示sql,格式化sql。。。。
  • C3P0連線池
  • 對映

將Hibernate的核心配置交給Spring:

<!-- 引入外部屬性檔案 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置C3P0連線池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password"
value="${jdbc.password}"/> </bean> <!-- Spring整合Hibernate --> <!-- 引入Hibernate的配置資訊 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 方式二:Hibernate核心配置已去掉 --> <!-- 注入連線池 --> <
property name="dataSource" ref="dataSource"/> <!-- 配置Hibernate的相關屬性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 設定對映檔案 --> <property name="mappingResources"> <list> <value>com/itheima/ssh/domain/Customer.hbm.xml</value> </list> </property> </bean>

 

 測試:

 

 

Hibernate模版常用方法:

  • 查 一個: get(Class c , Serializable id)   load(Class c , Serializable id)
  • 查多個:list find(String hql,Object...args)   list findByCriteria(DetachedCriteria dc)   分頁:list findByCriteria(DetachedCriteria dc,int firstResult,int maxResult)  命名查詢(瞭解)

 

package com.itheima.ssh.dao.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itheima.ssh.dao.CustomerDao;
import com.itheima.ssh.domain.Customer;
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
    @Override
    public void save(Customer customer) {
        System.out.println("Dao的save方法執行了");
        this.getHibernateTemplate().save(customer);    
    }

    @Override
    public void update(Customer customer) {
        this.getHibernateTemplate().update(customer);
    }

    @Override
    public void delete(Customer customer) {
        this.getHibernateTemplate().delete(customer);
    }

    @Override
    public Customer findById(Long cust_id) {
        return this.getHibernateTemplate().get(Customer.class, cust_id);
    }

    @Override
    public List<Customer> findAllByHQL() {
        List<Customer> list = (List<Customer>) this.getHibernateTemplate().find("from Customer");
        return list;
    }
     
    @Override
    public List<Customer> findAllByQBC() {
        DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
        List<Customer> list = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria);
        return list;
    }

    @Override
    public List<Customer> findByNamedQuery() {
        
        return (List<Customer>) this.getHibernateTemplate().findByNamedQuery("queryAll");
    }
}

 

package com.itheima.ssh.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.itheima.ssh.dao.CustomerDao;
import com.itheima.ssh.domain.Customer;
import com.itheima.ssh.service.CustomerService;
/**
 * 客戶端的業務層的實現類
 */
@Transactional
public class CustomerServiceImpl implements CustomerService {
   //注入Dao
    private CustomerDao customerDao;        
    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }
    
    @Override
    public void save(Customer customer) {
        System.out.println("service中的save方法執行了");
        customerDao.save(customer);
    }

    @Override
    public void update(Customer customer) {
        customerDao.update(customer);
    }

    @Override
    public void delete(Customer customer) {
        customerDao.delete(customer);
    }

    @Override
    public Customer findById(Long cust_id) {
        return customerDao.findById(cust_id);
    }

    @Override
    public List<Customer> findAllByHQL() {
        return customerDao.findAllByHQL();
    }

    @Override
    public List<Customer> findAllByQBC() {
        return customerDao.findAllByQBC();
    }

    @Override
    public List<Customer> findByNamedQuery() {
        return customerDao.findByNamedQuery();
    }

    
}