重建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); } } }