Technologie d'intégration Spring-SSH

Technologie d'intégration de ressort

Rôle : En termes d'intégration de framework, spring agit comme un conteneur d'objets, gérant divers beans dans le conteneur. Dans les applications JavaEE, il gère principalement DAO, Service, Action et Handler. Une fois Spring et Hibernte intégrés, SessionFactory est gérée pour simplifier davantage l'opération de persistance; Spring et Struts2 sont intégrés à l'hôte Action; Spring et Service layer sont intégrés pour fournir des fonctions de gestion des transactions.
Technologie d'intégration :

  • Intégration du framework Spring et du framework MVC (Struts2 et Springmvc)
  • Framework Spring et intégration du framework de couche de persistance (Hibernate et Mybatis)

Construction d'environnement intégré SSH

  1. Importez la bibliothèque d'intégration SSH,
    portez une attention particulière à l'importation du framework struts2 et des plug-ins d'intégration Spring
    Insérez la description de l'image ici

  2. Configurer Web.xml

<filter>
  	<filter-name>openSession</filter-name>
  	<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>openSession</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <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>*.action</url-pattern>
  </filter-mapping>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.1.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">
	
	<!-- 启动注解配置 -->
	<context:annotation-config/>
	
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClass}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
			</props>
		</property>
	</bean>
	
	<!-- 扫描包: 把包之下的类,创建实例,容纳到ioc容器中 -->
	<context:component-scan base-package="com.gec.login.dao.impl,com.gec.login.service.impl"/>
	
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>

Annotations courantes

  • Annotation générale @Component, la fonction principale est de créer une instance et de l'incorporer dans le conteneur à ressort
  • @Repository est
    souvent utilisé dans la couche d'accès aux données. Outre les fonctions de base de @Component, il possède également des fonctions spécifiques. Pour la couche d'accès aux données, par exemple, capturez les exceptions d'accès aux données.
  • @Service est souvent utilisé dans la couche de gestion. Remarque: si la fonction de la classe actuelle est difficile à classer, vous pouvez utiliser l'annotation @Component.

Je suppose que tu aimes

Origine blog.csdn.net/weixin_45879810/article/details/108483382
conseillé
Classement