SSH整合方式二:将hibernate的配置交给Spring管理

SSH整合方式二:将hibernate的配置交给Spring管理

SSH整合方式二:不带hibernate配置文件

1、hibernate配置文件中有哪些内容:
数据库连接的基本信息
Hibernate的相关的属性的配置
方言
显示SQL
格式化SQL

C3P0连接池
映射文件


2、编写一个数据库基本信息的属性配置文件:jdbc.properties

jdbc.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/study_test
jdbc.user=root
jdbc.password=123456

3、其他地方不变,参考SSH整合方式一。

只需把hibernate.cfg.xml文件交给Spring管理

applicationContext.xml文件:

<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 引入外部jdbc属性配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />

	<!-- 配置C3P0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.className}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.user}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- 配置Action -->
	<bean id="customerAction" class="com.pipi.ssh.web.action.CustomerAction" scope="prototype">
		<property name="customerService" ref="customerService"/>
	</bean>

	<!-- 配置Service -->
	<bean id="customerService" class="com.pipi.ssh.service.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao"/>
	</bean>

	<!-- 配置DAO -->
	<bean id="customerDao" class="com.pipi.ssh.dao.CustomerDaoImpl">
		<property name="sessionFactory" ref="sessionFactoryBean"/>
	</bean>

	<!-- Spring整合Hibernate -->
	<!-- 引入Hibernate的配置的信息 -->
	<bean id="sessionFactoryBean" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 注入连接池 -->
		<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/pipi/ssh/domain/Customer.hbm.xml</value>
			</list>
		</property>
	</bean>


	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactoryBean"/>
	</bean>

	<!-- 开启注解事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

4、Hibernate模板的常用的方法

(1)保存操作
save(Object obj);

public void save(Customer customer) {
    this.getHibernateTemplate().save(customer);
}

(2)修改操作
update(Object obj);

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

(3)删除操作
delete(Object obj);

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

(4)查询操作
查询一个:
get(Class c,Serializable id);
load(Class c,Serializable id);

public Customer findById(Long id) {
    //return this.getHibernateTemplate().get(Customer.class, id);
    return this.getHibernateTemplate().load(Customer.class, id);
}

查询多个:
List find(String hql,Object… args);
List findByCriteria(DetachedCriteria dc);
List findByCriteria(DetachedCriteria dc,int firstResult,int maxResults);
List findByNamedQuery(String name,Object… args);

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

5、Spring提供了延迟加载的解决方案

在SSH整合开发中哪些地方会出现延迟加载:
使用load方法查询某一个对象的时候(不常用)
查询到某个对象以后,显示其关联对象。

只需在web.xml中配置一个过滤器即可,在核心过滤器之前配置:

web.xml中配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <!-- 配置Spring的核心监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 加载Spring的配置文件的路径的,默认加载的/WEB-INF/applicationContext.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- 解决延迟加载的过滤器,需要在Struts2的核心过滤器之前编写 -->
    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

    <!-- 配置Struts2的核心过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值