ssh简单整合案例(基于xml)
2018-12-20 本文已影响0人
Zak1
一个简单的ssh整合案例
最近刚学习了Spring,便写下一个小测试来练练手,将这个作为学习笔记。
这里是本项目的代码结构

使用的JAR包(事实上用不到这么多,只是因为懒得一个一个去找,便一次性导入了)

- 三大配置文件
- hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/users?serverTimezone=UTC
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">100</property>
<property name="show_sql"></property>
<property name="format_sql"></property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="com/po/UserPo.hbm.xml" />
</session-factory>
</hibernate-configuration>
- Struts:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="struts-default.xml" />
<package name="Final_SSH_TEST" extends="struts-default">
<action name="userAction_*" class="actionid" method="{1}">
<result name="success">SaveSuccess.jsp</result>
</action>
</package>
</struts>
- Spring配置文件application.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" 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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 导入数据源 -->
<bean id="sessionFactoryID"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 创建模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactoryID"></property>
</bean>
<!-- 事务管理 -->
<bean id="txManger"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryID"></property>
</bean>
<!-- 事务详情 -->
<tx:advice id="txadvice" transaction-manager="txManger">
<tx:attributes>
<tx:method name="adduser" propagation="REQUIRED" isolation="DEFAULT" />
</tx:attributes>
</tx:advice>
<!-- AOP编程 -->
<aop:config>
<aop:advisor advice-ref="txadvice"
pointcut="execution(* com.servcice.UserServiceImpl.*(..))" />
</aop:config>
<bean id="Daoid" class="com.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactoryID"></property>
</bean>
<bean id="Userserviceid" class="com.servcice.UserServiceImpl">
<property name="userdao" ref="Daoid"></property>
</bean>
<bean id="actionid" class="com.action.Action">
<property name="userservice" ref="Userserviceid"></property>
</bean>
</beans>
其中代码详情部分上传至github,会继续更新基于注解的ssh整合。
github地址:zakl's github