SSH框架搭建主要流程

1.创建一个动态web项目

接着点击next,勾选自动生成web.xml选项,最后点击finish。

2.在WEB-INF的lib文件夹中导入必备的jar包,包括spring、struts以及hibernate的jar包:

spring相关jar包:

struts相关jar包:

hibernate相关jar包:

其中尤其注意不能忘记classmate-1.3.4.jar、byte-buddy-1.8.12.jar、aspectjweaver-1.8.13jar的导入。

3.jar导入后,开始搭建框架,完成配置文件

(1)首先第一步加载数据库配置文件,配置数据源

<!-- 加载数据库配置文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<!-- 包扫描器 -->
	<context:component-scan base-package="com.jiangnan.ssh"/>
	<!-- 配置C3P0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${db.username}"/>
		<property name="password" value="${db.password}"/>
		<property name="driverClass" value="${db.driverClass}"/>
		<property name="jdbcUrl" value="${db.url}"/>
		<property name="initialPoolSize" value="${db.initialPoolSize}"/>
		<property name="maxPoolSize" value="${db.maxPoolSize}"/>		
	</bean>

 (2)第二步创建数据库操作对象

<!-- 创建数据库的操作对象 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="hibernateProperties" >
			<props>
				<!-- 数据库方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- 显示Hibernate持久化操作生成的预编译的SQL -->
				<prop key="hibernate.show_sql">true</prop>
				<!-- 将SQL格式化后再输出 -->
				<prop key="hibernate.format_sql">true</prop>
				<!-- 是否根据需要每次自动创建数据库 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>			
			</props>
		</property>
		<!-- Hibernate映射文件的目录 -->
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath:com/jiangnan/ssh/pojo</value>
			</list>
		</property>
	</bean>

(3)第三步配置事务

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

(4)第四步配置通知

<!-- 配置通知 -->
	<tx:advice transaction-manager="transactionManager" id ="txadvice">
		<tx:attributes>
			<tx:method name="add" propagation="REQUIRED"/>
			<tx:method name="delete" propagation="REQUIRED"/>
			<tx:method name="update" propagation="REQUIRED"/>
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>

(5)第五步配置切面

<!-- 配置切面 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.jiangnan.ssh.service.*.*(..))" id="txpoint"/>
		<aop:advisor advice-ref="txadvice" pointcut-ref="txpoint"/>
	</aop:config>

(6)最后配置一下web.xml

<?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>TestSSH</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
  	<filter-name>struts</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

以上基本完成了SSH框架的主要搭建,如有不当之处,敬请各位批评指正。

猜你喜欢

转载自blog.csdn.net/qq_36235275/article/details/81089709