Integración de marco nativo ssh uno: método de anotación

Este artículo es la integración de struts2 y spring Hibernate, no la integración de maven. La transacción adopta el método de anotación, integración ssh nativa simple.

Si no lo entiende, está bien. El bloguero carga el código en csdn para su referencia y estudio .............

Enlace de descarga: https://download.csdn.net/download/qq_30764991/11293457

Use la herramienta, eclipse jdk7 struts2 spring hibernate, descargue y use

La estructura del proyecto es la siguiente:

Los pasos básicos:

Uno, cree un proyecto web, sshTest como se muestra arriba

En segundo lugar, introduzca el paquete jar integrado de los tres marcos de struts2 y spring hibernate

Aproximadamente 46 envases de frascos.

Tres: configure application.xml, este paso es muy importante: el código específico es el siguiente:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns="http://www.springframework.org/schema/beans" 
		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-4.2.xsd 
							http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
							http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
							http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	
	<!-- 读取db.properties文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	
	<!-- 开启扫描类中的注解 -->
	<context:component-scan base-package="cn.demo"></context:component-scan>
	<!-- 配置c3p0连接池 -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
		<property name="driverClass" value="${jdbc.driverClass}" ></property>
		<property name="user" value="${jdbc.user}" ></property>
		<property name="password" value="${jdbc.password}" ></property>
	</bean>
	
	<!-- 核心事务管理器 -->
	<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
		<property name="sessionFactory" ref="sessionFactory" ></property>
	</bean>
	<!-- 开启注解事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
		<property name="dataSource" ref="dataSource" ></property>
		<property name="hibernateProperties">
			<props>
			<!-- mysql数据库方言 -->
				<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
				<!--  可选配置 -->
				<!--控制台显示sql语句,格式化sql语句,自动更新  -->
				<prop key="hibernate.show_sql" >true</prop>
				<prop key="hibernate.format_sql" >true</prop>
				<prop key="hibernate.hbm2ddl.auto" >update</prop>
			</props>
		</property>
		<!-- 加载domain -->
		<property name="mappingDirectoryLocations" value="classpath:cn/demo/domain" ></property>
	</bean>
	

</beans>

Cuatro: configure el código struts.xml de la siguiente manera:

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>
	<!-- #  struts.objectFactory = spring	将action的创建交给spring容器	
			struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
			-->
	<constant name="struts.objectFactory" value="spring"></constant>

	<package name="demo" namespace="/" extends="struts-default" >
		<!--配置全局异常处理  -->
		<global-exception-mappings>
			<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
		</global-exception-mappings>
	</package>
</struts>
	

Cinco: configurar db.properties

jdbc.jdbcUrl=jdbc:mysql:///demo
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456

Seis: Configurar web.xml Este paso también es muy importante, no olvide:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>sshTest</display-name>
	<!-- 让spring随web启动而创建的监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 配置spring配置文件位置参数 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<!-- 扩大session作用范围 注意: 任何filter一定要在struts的filter之前调用 -->
	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<!-- struts2核心过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

bien, una vez completada la configuración anterior, inicie el proyecto:

como sigue:

En este punto, está listo un marco de integración nativo ssh simple. ¿Crees que la escritura del blogger es muy simple? Esta es solo una técnica necesaria para comenzar. Le sugiero que siga al blogger para configurar un artículo para fortalecer su comprensión. ¡Vamos, la revolución aún no ha tenido éxito, los camaradas todavía tienen que trabajar duro! venga................................

Si no lo entiende, está bien. El bloguero carga el código en csdn para su referencia y estudio .............

Enlace de descarga: https://download.csdn.net/download/qq_30764991/11293457

Supongo que te gusta

Origin blog.csdn.net/qq_30764991/article/details/95016166
Recomendado
Clasificación