重建SSH項目
java項目可以直接復制,但是web項目除了改名字還要該配置,如下:
方式一:無障礙整合:帶Hibernate配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Spring整合Hibernate --> <!-- 引入Hibernate配置信息===== --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 引入Hibernate配置文件 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> </bean> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 開啟注解事務,在業務層使用注解@Transactional --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 通過context標簽引入jdbc.properties,在Hibernate核心配置文件里,免去 --> <!-- 配置C3P0連接池,在Hibernate核心配置文件里面,免去 --> <!-- 配置Action --> <bean id="customerAction" class="com.ssh.web.action.CustomerAction" scope="prototype"> <property name="customerService" ref="customerService"/> </bean> <!-- 注入CustomerService --> <bean id="customerService" class="com.ssh.service.impl.CustomerServiceImpl"> <property name="customerDao" ref="customerDao"/> </bean> <!-- 注入Dao CustomerDaoImpl繼承HibernateDaoSupport,在Dao注入SessionFactory,創建HibernateTemplate模版 --> <bean id="customerDao" class="com.ssh.dao.impl.CustomerDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
方式二:不帶Hibernate核心配置文件,將核心配置文件給Spring管理
Hibernate配置文件:
- 數據庫連接的配置
- Hibernate屬性的配置:方言,顯示sql,格式化sql。。。。
- C3P0連接池
- 映射
將Hibernate的核心配置交給Spring:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--通過context標簽引入jdbc.properties --> <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"> <!-- 注入連接池 --> <property name="dataSource" ref="dataSource" /> <!-- 配置Hibernate相關屬性 --> <property name="hibernateProperties"> <props> <!-- 配置方言,打印語句,格式化sql,自動建表 --> <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/ssh/domain/Customer.hbm.xml</value> </list> </property> </bean> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 開啟注解事務,在業務層使用注解@Transactional --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置Action --> <bean id="customerAction" class="com.ssh.web.action.CustomerAction" scope="prototype"> <property name="customerService" ref="customerService"/> </bean> <!-- 注入CustomerService --> <bean id="customerService" class="com.ssh.service.impl.CustomerServiceImpl"> <property name="customerDao" ref="customerDao"/> </bean> <!-- 注入Dao CustomerDaoImpl繼承HibernateDaoSupport,在Dao注入SessionFactory,創建HibernateTemplate模版 --> <bean id="customerDao" class="com.ssh.dao.impl.CustomerDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
測試:
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)
- 命名查詢(了解)
Dao層:
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"); } }
Service層:
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(); } }
測試類:
package com.ssh.testdemo; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.ssh.domain.Customer; import com.ssh.service.CustomerService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class demo1 { // Spring注入Service @Resource(name = "customerService") private CustomerService customerService; // public void setCustomerService(CustomerService customerService) { // this.customerService = customerService; // } @Test public void test1() { customerService.save(new Customer()); } @Test // 修改 public void test2() { Customer customer = customerService.findById(1l); customer.setCust_name("修改測試"); customerService.Update(customer); } @Test // 刪除 public void test3() { Customer customer = customerService.findById(2l); customer.setCust_name("刪除測試"); customerService.delete(customer); } @Test // 查詢所有 public void test4() { List<Customer> list = customerService.findAllByHQL(); for (Customer customer : list) { System.out.println(customer); } } @Test // 查詢所有 public void test5() { List<Customer> list = customerService.findAllByQBC(); for (Customer customer : list) { System.out.println(customer); } } }