基础的SSH框架,只有一个测试的index.jsp页面。
运行环境:Eclipse4.8.0 + Tomcat7.0 + jdk1.8(1.8以下版本没有测试)
注:架包通过maven
管理
首先来看一下整个项目的结构:
控制层:com.fyc.action
Service层:com.fyc.service
、com.fyc.serviceImpl
Dao层:com.fyc.dao
、com.fyc.daoImpl
实体类:com.fyc.entity
工具类:com.fyc.util
资源文件:src/main/resources
架包管理:pom.xml
其中remarks.txt
是个人记录笔记文件,无需理会。
数据库建成之后,只需更改jdbc.properties
文件中的属性即可。
其中Dao层采用通用Dao层的形式,因此整个项目只需要一个Dao类即可。
话不多说,直接上代码:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Struts2过滤器 -->
<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>
</filter-mapping>
<!-- 配置Spring的监听器 -->
<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>
</web-app>
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: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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 开启包扫描 -->
<context:component-scan base-package="com.fyc.*"></context:component-scan>
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库连接相关信息 -->
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate属性 -->
<property name="hibernateProperties">
<props>
<!-- 是否展示SQ语句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 是否自动创建表结构 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
</props>
</property>
<!-- 扫描并加载实体类 -->
<property name="packagesToScan" value="com.fyc.entity"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<!-- 注入SessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置需要进行事务管理的方法和事物传播行为 -->
<tx:method name="save*" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="query*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* com.fyc.service..*.*(..))" id="pointcut"/>
<!-- 适配切入点和事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
struts.xml:
<?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.custom.i18n.resources" value="messageResource"></constant>
<!-- 配置Struts2应用的编码集 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.multipart.maxSize" value="10000000000" />
<!-- 默认访问页面 -->
<package name="default" extends="struts-default" namespace="/">
<default-action-ref name="default"></default-action-ref>
<action name="default">
<result>default.jsp</result>
</action>
</package>
<!-- 包含分文件示例 -->
<!-- <include file="fileName.xml"></include> -->
<!-- 方法链接 -->
<package name="all" namespace="/" extends="json-default">
<!-- 方法示例 -->
<!-- <action name="MethodName" class="com.fyc.action.fileName" method="MethodName">
<result type="json">
<param name="root">param</param>
</result>
</action>
<action name="MethodName" class="com.fyc.action.fileName" method="MethodName">
<result>URL</result>
</action> -->
<!-- 注册测试 -->
<action name="saveUser" class="com.fyc.action.UserAction" method="saveUser">
<result type="json">
<param name="root">state</param>
</result>
</action>
<!-- 删除测试 -->
<action name="delUser" class="com.fyc.action.UserAction" method="delUser">
<result type="json">
<param name="root">state</param>
</result>
</action>
<!-- HQL语句查询测试 -->
<action name="queryUserByHql" class="com.fyc.action.UserAction" method="queryUserByHql">
<result type="json">
<param name="root">state</param>
</result>
</action>
</package>
</struts>
dao层通用接口:
package com.fyc.dao;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
public interface CommonDao<T> {
/**
* 存储对象
* @date 2018-6-19 下午5:06:07
* @author live
* @param t
*/
public void save(T t);
/**
* 删除对象
* @date 2018-6-28 上午11:42:53
* @author live
* @param t
*/
public void delete(T t);
/**
* 查询对象(根据类和主键id查询)
* @date 2018-6-28 上午11:48:22
* @author live
* @param t
*/
public void query(T t, Integer id);
/**
* 更新对象(根据对象的主键进行更新)
* @date 2018-6-28 上午11:51:53
* @author live
* @param t
*/
public void update(T t);
/**
* 执行SQL语句
* @date 2018-6-28 上午11:54:07
* @author live
* @return
*/
public int executeBySql(String sql);
/**
* 执行hql语句
* @date 2018-6-28 上午11:59:46
* @author live
* @param hql
* @return
*/
public List executeByHql(String hql);
/*********************************************************************************************分割线*********************************************************************************************/
/**
* 存储或更新(根据主键判定)
* @date 2018-6-28 下午2:13:17
* @author live
*/
public void saveOrUpdate(T t);
}
导入架包-pom.xml:
<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>mmic</groupId>
<artifactId>mmic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name/>
<description/>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 不能连接到maven仓库 -->
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo2.maven.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.annotation</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.ejb</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-osgi-bundle</artifactId>
<version>1.0.1-SP3</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- 添加SSH依赖 -->
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24.1</version>
</dependency>
<!-- 继承json-default -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.16.1</version>
</dependency>
<!-- 添加Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.2.Final</version>
</dependency>
<!-- spring框架依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- 包含了大量注解@Autowired等。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- 包含SessionFactory org.springframework.orm.hibernate4.LocalSessionFactoryBean -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- 包含ContextLoaderListener 监听器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- ApplicationContextInitializer -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!-- c3p0数据源 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- 添加Log4J依赖 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.6.4</version>
</dependency>
<!-- 添加javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.11.0.GA</version>
</dependency>
<!-- 错误解决 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
其他类暂不赘述,完整资源包下载传送:https://download.csdn.net/download/qq_38084142/10600235
框架也是自己在网上搜罗的资源拼合而成,如有bug,敬请指教!!!