Struts2+Spring+Hibernate整合开发(Maven多模块搭建)

Struts2+Spring+Hibernate整合开发(Maven多模块搭建)

0.项目结构

Struts2:web层

Spring:对象的容器

Hibernate:数据库持久化操作

Struts2+Spring+Hibernate整合开发(Maven多模块搭建)

1.父模块导入和管理所有需要的jar包

<?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.ssh</groupId>
<artifactId>ssh_maven</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>ssh_dao</module>
<module>ssh_service</module>
<module>ssh_web</module>
</modules> <!-- 版本管理 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<hibernate.version>5.0.7.Final</hibernate.version>
<struts.version>2.3.24</struts.version>
</properties> <!-- 锁定版本 -->
<dependencyManagement>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency> <!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency> <!-- struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency> <!-- 整合的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.version}</version>
</dependency> </dependencies>
</dependencyManagement> <!-- 依赖管理,添加依赖的jar包 -->
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency> <!-- spring相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency> <!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency> <!-- struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
</dependency> <!-- 整合的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency> <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
</dependency> <!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency> <!-- c3p0连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency> <!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency> <!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> </dependencies> <build>
<!-- 配置插件 -->
<plugins>
<!-- 配置编译版本为jdk1.7 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <!-- 配置tomcat7 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 配置工程路径 -->
<path>/</path>
<!-- 配置端口号 -->
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
</project>

父模块pom

2.Dao模块

Struts2+Spring+Hibernate整合开发(Maven多模块搭建)

1)创建Hibernate核心配置文件(hibernate.cfg.xml;文件名不能修改)

主要设置一些配置以及读取元配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<!-- 会话工厂 -->
<session-factory>
<!-- 数据库方言,根据数据库选择 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!--为了方便调试是否在运行hibernate时在日志中输出sql语句 -->
<property name="hibernate.show_sql">true</property> <!-- 是否对日志中输出的sql语句进行格式化 -->
<property name="hibernate.format_sql">true</property> <!-- 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 加载映射文件 -->
<mapping resource="com/ssh/entity/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

hibernate.cfg.xml

2)创建元配置文件(Customer.hbm.xml;命名格式不能随便取)

可以根据该配置文件自动映射为表,该配置文件对应一个实体类

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-2-26 16:58:40 by Hibernate Tools 4.3.1.Final -->
<hibernate-mapping>
<class name="com.ssh.entity.Customer" table="cst_customer" optimistic-lock="version">
<id name="custId" type="java.lang.Long">
<column name="cust_id" />
<generator class="identity" />
</id>
<property name="custName" type="string">
<column name="cust_name" length="32" not-null="true"></column>
</property>
<property name="custUserId" type="java.lang.Long">
<column name="cust_user_id"></column>
</property>
<property name="custCreateId" type="java.lang.Long">
<column name="cust_create_id"></column>
</property>
<property name="custIndustry" type="string">
<column name="cust_industry" length="32"></column>
</property>
<property name="custLevel" type="string">
<column name="cust_level" length="32"></column>
</property>
<property name="custLinkman" type="string">
<column name="cust_linkman" length="64"></column>
</property>
<property name="custPhone" type="string">
<column name="cust_phone" length="64"></column>
</property>
<property name="custMobile" type="string">
<column name="cust_mobile" length="16"></column>
</property>
</class>
</hibernate-mapping>

customer.hbm.xml

3)创建Spring核心配置文件(applicationContext-dao.xml)

主要配置数据库的数据源(数据库连接池);

Hibernate的工厂(创建SessionFactory工厂);

配置事务管理;

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh_maven" />
<property name="user" value="root" />
<property name="password" value="admins" />
</bean> <!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 依赖dataSource -->
<property name="dataSource" ref="dataSource" />
<!-- 创建工厂需要加载hibernate映射文件 -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean> <bean id="customerDao" class="com.ssh.dao.impl.CustomerImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> </beans>

application-dao.xml

4)接口和实体类

package com.ssh.dao;

import com.ssh.entity.Customer;

public interface CustomerDao {

    /**
* 通过id查询用户
* @param CustomerId
* @return
*/
public Customer findCustomerById(Long CustomerId);
}

CustomerDao

package com.ssh.dao.impl;

import com.ssh.dao.CustomerDao;
import com.ssh.entity.Customer;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport; public class CustomerImpl extends HibernateDaoSupport implements CustomerDao { @Override
public Customer findCustomerById(Long CustomerId) {
return this.getHibernateTemplate().get(Customer.class, CustomerId);
} }

CustomerImpl

package com.ssh.entity;

public class Customer {
private Long custId;
private String custName;
private Long custUserId;
private Long custCreateId;
private String custIndustry;
private String custLevel;
private String custLinkman;
private String custPhone;
private String custMobile; public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public Long getCustUserId() {
return custUserId;
}
public void setCustUserId(Long custUserId) {
this.custUserId = custUserId;
}
public Long getCustCreateId() {
return custCreateId;
}
public void setCustCreateId(Long custCreateId) {
this.custCreateId = custCreateId;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustLinkman() {
return custLinkman;
}
public void setCustLinkman(String custLinkman) {
this.custLinkman = custLinkman;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustMobile() {
return custMobile;
}
public void setCustMobile(String custMobile) {
this.custMobile = custMobile;
} }

Customer

3.Service模块

1)依赖Dao模块

<?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">
<parent>
<artifactId>ssh_maven</artifactId>
<groupId>com.ssh</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>ssh_service</artifactId> <dependencies>
<dependency>
<groupId>com.ssh</groupId>
<artifactId>ssh_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

service.pom

Struts2+Spring+Hibernate整合开发(Maven多模块搭建)

2)创建Spring核心配置文件(applicationContext-service.xml)

主要用于管理Service

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <import resource="classpath:applicationContext-dao.xml"/> <bean id="customerService" class="com.ssh.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean> </beans>

application-service.xml

3)接口与实现

CustomerService
CustomerServiceImpl

4.Web模块

1)依赖Service模块

<?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">
<parent>
<artifactId>ssh_maven</artifactId>
<groupId>com.ssh</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>ssh_web</artifactId>
<packaging>war</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>com.ssh</groupId>
<artifactId>ssh_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> </project>

web.pom

Struts2+Spring+Hibernate整合开发(Maven多模块搭建)

2)创建Spring核心配置文件(applicationContext-action.xml)

主要用于管理Action

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <import resource="classpath:applicationContext-service.xml"/> <bean id="customerAction" class="com.ssh.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"></property>
</bean> </beans>

application-action.xml

3)创建Struts2核心配置文件(struts.xml)

主要配置一些常量和请求规则

<?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>
<!-- 配置常量 -->
<!-- 字符集 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 主题 -->
<constant name="struts.ui.theme" value="simple"></constant>
<!-- 扩展名 -->
<!-- <constant name="struts.action.extension" value="action"></constant> --> <!-- 通用package -->
<package name="customer" namespace="/" extends="struts-default"> <action name="customerAction_*" class="customerAction" method="{1}">
<result name="success">/info.jsp</result>
</action> </package>
</struts>

struts.xml

4)Action

package com.ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.entity.Customer;
import com.ssh.service.CustomerService; public class CustomerAction extends ActionSupport { //注入service
private CustomerService customerService; //属性封装到custId
private Long custId;//客户id //存放到值栈(get方式)
private Customer customer;//客户信息 public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
} //action方法
public String queryCustomer(){
customer=customerService.findCustomerById(custId);
return "success";
} }

CustomerAction

5.Web.xml中的配置

配置监听器读取applicationContext-*.xml

配置Struts2的核心拦截器(过滤器)

<?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"
id="WebApp_ID" version="2.5"> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>classpath:applicationContext.xml</param-value> -->
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param> <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> <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>

web.xml

6.其他一些配置

1)返回的页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
访问网址:http://localhost:8080/ssh/customerAction_queryCustomer.action?custId=1
这是一个信息页面
${customer.custName }
</body>
</html>

info.jsp

2)数据库连接池

#加jdbc前缀的目的是为了避免后面的名字与其他地方名字冲突
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh_maven?characterEncoding=utf-8
jdbc.username=root
jdbc.password=admins

3)日志

#日志文件
log4j.rootLogger=DEBUG,stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

至此SSH分模块整合完毕

上一篇:利用Python访问Mysql数据库


下一篇:KMP算法具体解释(转)