SSH配置文件
程序员文章站
2022-04-30 09:42:46
...
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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- 加载Hibernate配置 -->
<!-- <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean> -->
<!-- 定义dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 指定JDBC驱动类 -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>
<!-- 提供连接数据库的URL地址 -->
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe">
</property>
<!-- 提供连接数据库的用户名和密码 -->
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<!-- 定义SessionFactory Bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 为LocalSessionFactoryBean(AnnotationSessionFactoryBean)注入定义好的数据源 -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 添加Hibernate配置参数 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 添加注解映射 -->
<!-- <property name="annotatedClasses">
<list>
<value>cn.houserent.entity.House</value>
<value>cn.houserent.entity.Street</value>
<value>cn.houserent.entity.District</value>
<value>cn.houserent.entity.Type</value>
<value>cn.houserent.entity.User</value>
</list>
</property> -->
<!-- 添加对象关系映射文件 -->
<!-- <property name="mappingResources">
<list>
<value>cn/houserent/entity/Type.hbm.xml</value>
<value>cn/houserent/entity/District.hbm.xml</value>
<value>cn/houserent/entity/Street.hbm.xml</value>
<value>cn/houserent/entity/House.hbm.xml</value>
<value>cn/houserent/entity/User.hbm.xml</value>
</list>
</property> -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:cn/houserent/entity/</value>
</list>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDao" class="cn.houserent.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="houseDao" class="cn.houserent.dao.impl.HouseDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="typeDao" class="cn.houserent.dao.impl.TypeDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="streetDao" class="cn.houserent.dao.impl.StreetDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="districtDao" class="cn.houserent.dao.impl.DistrictDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置业务层 -->
<bean id="userBiz" class="cn.houserent.service.impl.UserBizImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="houseBiz" class="cn.houserent.service.impl.HouseBizImpl">
<property name="houseDao" ref="houseDao"></property>
</bean>
<bean id="typeBiz" class="cn.houserent.service.impl.TypeBizImpl">
<property name="typeDao" ref="typeDao"></property>
</bean>
<bean id="streetBiz" class="cn.houserent.service.impl.StreetBizImpl">
<property name="streetDao" ref="streetDao"></property>
</bean>
<!-- 定义事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 定义事务属性,声明事务规则 -->
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="register" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 定义切入点 -->
<aop:pointcut id="serviceMethod"
expression="execution(* cn.houserent.service.*.*(..))" />
<!-- 将事务通知与切入点组合 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
<!-- 控制层 (id为struts.xml中的class) 以下每个bean必须都要增加scope="prototype"属性 -->
<bean id="userAction" class="cn.houserent.action.UserAction"
scope="prototype">
<property name="userBiz" ref="userBiz"></property>
</bean>
</beans>
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">scott</property>
<property name="connection.url">jdbc:oracle:thin:@10.0.0.35:1521:xe</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">true</property>
<property name="cache.query_cache_factory">true</property>
<mapping resource="cn/houserent/entity/Type.hbm.xml" />
<mapping resource="cn/houserent/entity/District.hbm.xml" />
<mapping resource="cn/houserent/entity/Street.hbm.xml" />
<mapping resource="cn/houserent/entity/House.hbm.xml" />
<mapping resource="cn/houserent/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.custom.i18n.resources" value="message"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.ui.theme" value="simple"/>
<package name="renthouse" extends="struts-default">
<default-action-ref name="defaultAction" />
<action name="defaultAction" class="cn.houserent.action.Default">
<result name="fail">/page/fail.jsp</result>
</action>
<action name="manage" class="cn.houserent.action.Manage" method="list">
<result name="list">/page/manage.jsp</result>
<result name="ajaxlist">/page/result.jsp</result>
</action>
<!-- 使用动态方法方法实现用户登录(login)、注册 (register)
<action name="user" class="cn.houserent.action.UserAction">
<result name="register_success">/page/success.jsp</result>
<result name="register_input">/page/register.jsp</result>
<result name="login_success" type="redirectAction">manage</result>
<result name="login_input">/page/login_struts2.jsp</result>
</action> -->
<!-- class属性的值不再是Action类的全限定名,而是Spring配置文件中相应的Action
Bean的名称
-->
<action name="user" class="userAction">
<result name="register_success">/page/success.jsp</result>
<result name="register_input">/page/register.jsp</result>
<result name="login_success" type="redirectAction">manage</result>
<result name="login_input">/page/login_struts2.jsp</result>
</action>
<action name="add" class="cn.houserent.action.Add">
<result name="success">/page/success2.jsp</result>
<result name="input">/page/add.jsp</result>
</action>
<action name="index" class="cn.houserent.action.HouseAction" method="index">
<result name="success">/page/house_list.jsp</result>
<result name="error">/page/fail.jsp</result>
</action>
<action name="search" class="cn.houserent.action.HouseAction" method="lookup">
<result name="success">/page/search_list.jsp</result>
<result name="error">/page/fail.jsp</result>
</action>
<action name="show" class="cn.houserent.action.DetailAction">
<result name="success">/page/show.jsp</result>
</action>
</package>
</struts>
User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="cn.houserent.entity.User" table="users" lazy="false" schema="scott">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="sequence" >
<param name="sequence">SEQ_ID</param>
</generator>
</id>
<property name="name" type="java.lang.String" lazy="false">
<column name="name" length="50" not-null="true" />
</property>
<property name="password" type="java.lang.String" lazy="false">
<column name="password" length="50" not-null="true" />
</property>
<property name="telephone" type="java.lang.String" lazy="false">
<column name="telephone" length="12" />
</property>
<property name="username" type="java.lang.String" lazy="false">
<column name="username" length="50" />
</property>
<property name="isadmin" type="java.lang.String" lazy="false">
<column name="isadmin" length="2" />
</property>
</class>
<query name="loginUser">
<![CDATA[
from User user where user.name = :name and user.password = :password
]]>
</query>
<sql-query name="findUserHouse">
<![CDATA[
select {u.*},{h.*} from users u,house h where u.id = h.user_id
]]>
<return alias="u" class="cn.houserent.entity.User"/>
<return alias="h" class="cn.houserent.entity.House"/>
</sql-query>
</hibernate-mapping>
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baojiaren</groupId>
<artifactId>SSH</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SSH Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<struts2.version>2.5.26</struts2.version>
<log4j2.version>2.10.0</log4j2.version>
<spring-version>5.1.5.RELEASE</spring-version>
<hibernate.version>5.4.20.Final</hibernate.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--struts2-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-bom</artifactId>
<version>2.5.26</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--log4j-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>${log4j2.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!--spring end-->
<!--struts2 start-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
</dependency>
<!--struts2 end-->
<!--hibernate start-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--c3p0-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--hikaricp-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--hibernate end-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>SSH</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- 配置环境参数,指定Spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置Spring的ContextLoaderListener监听器,初始化Spring容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</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>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>
cn.houserent.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>