Maven项目整合SSH

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 整合SSH 将 Hibernate.cfg.xml and struts.xml 去掉了 . . .

Maven项目整合SSH

  1. 编写数据库基本连接信息
  2. 配置 Spring 核心文件
  3. 配置 web.xml 文件
  4. 添加 struts.xml or 使用注解

编写数据库基本连接信息: database.properties

jdbc.username=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/database_db
jdbc.driver=com.mysql.jdbc.Driver

配置 Spring 核心文件

  1. 配置数据库连接池
  2. 配置 Hibernate 基本信息
  3. 配置 Hibernate 事务管理

配置数据库连接池

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    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">
    
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:database.properties" />

<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
    <property name="url" value="${jdbc.url}"></property>
</bean>
<!-- maven:阿里巴巴数据库 - 连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>

<!-- 数据库连接 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>

配置 Hibernate 基本信息

<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <!-- 将数据源引入: sessionFactory -->
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <!-- 配置Hibernate : 数据库方言 -->
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <!-- 配置Hibernate : 是否显示SQL -->
            <prop key="hibernate.show_sql">true</prop>
            <!-- 配置Hibernate : 格式化SQL -->
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
        
    <property name="mappingLocations">
        <list>
            <!-- 指定Hibernate : 对象映射文件路径 -->
            <value>classpath:com/znsd/ssh/bean/*.hbm.xml</value>
        </list>
    </property>
</bean>
<!-- hibernate -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.11.Final</version>
</dependency>

<!-- spring -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.18.RELEASE</version>
</dependency>

<!-- spring-orm -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>4.3.18.RELEASE</version>
</dependency>

配置 Hibernate 事务管理

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

<!-- 配置需要开启事务的方法 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <!-- 设置匹配方法 -->
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />

        <tx:method name="insert*" propagation="REQUIRED" />

        <tx:method name="del*" propagation="REQUIRED" />

        <tx:method name="update*" propagation="REQUIRED" />

        <tx:method name="*" propagation="REQUIRED" read-only="true" />
    </tx:attributes>
</tx:advice>

<!-- AOP切面拦截事务 -->
<aop:config>
    <aop:pointcut id="serviceMethod" 
                  expression="execution(* com.znsd.ssh.service.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
<!-- AspectJ : 配置事务管理 -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.1</version>
</dependency>
<dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>1.0</version>
</dependency>

<!-- spring-事务 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.3.18.RELEASE</version>
</dependency>

配置 web.xml 文件

  1. 添加 OpenSessionInView 过滤器
  2. 添加编码格式过滤器: 可选
  3. 配置Spring核心文件路径
  4. Struts2 核心 过滤器
  5. 防止spring内存溢出监听器
<!-- 添加 OpenSessionInView 过滤器, 将 Session 生命周期搬到视图层 -->
<filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 添加编码格式过滤器 -->
<filter>
    <description>字符集过滤器</description>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <description>字符集编码</description>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Spring核心文件路径 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-*.xml</param-value>
</context-param>
<!-- 用于初始 Spring 配置信息 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Struts2 核心 过滤器 -->
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.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.util.IntrospectorCleanupListener</listener-class>
</listener>

添加 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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default"></package>
    
</struts>

配置 maven 项目的jdk版本

<!-- 配置 maven 项目的jdk版本 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

maven:struts

<!-- struts -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.16</version>
</dependency>

<!-- struts2 集成 spring -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-spring-plugin</artifactId>
    <version>2.5.16</version>
</dependency>

整合 Struts2 非注解方式使用时应该注意

  1. Spring 配置文件中配置[ action, service, dao ]

    1. 在配置 Action 的时候需要注意我们一般需要 scope="prototype"
  2. Struts2 配置文件下的 action > class 写 Spring 下的 bean > id

Spring 配置文件中配置[ action, service, dao ]

<bean id="springAction" class="package.className" scope="prototype">
    <property name="springService" ref="springService"></property>
</bean>
 . . . 
<bean id="SpringDao" class="com.znsd.ssh.dao.impl.BookDaoImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

Struts2 配置文件下的 action > class 写 Spring 下的 bean > id

<action name="updateBook" class="springAction" method="method">
    <result>/index.jsp</result>
</action>

整合使用 Struts2 注解方式

  1. struts2 配置文件可以放弃
  2. 导入 struts2-convention-plugin.jar
  3. 使用时常用注解介绍

导入 struts2-convention-plugin.jar

<!-- struts2 注解 -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-convention-plugin</artifactId>
    <version>2.5.16</version>
</dependency>

使用时常用注解介绍

注解 介绍
@ParentPackage("json-default") 继承父包[struts-default:json-default]
@Namespace("/") 定义命名空间
@Action 定义一个请求
@Result 定义一个返回结果
@Scope("prototype") 设置 Action 为多利模式
@ParentPackage("json-default")
@Namespace("/")
public class ProductAction extends ActionSupport {
@Action(value = "url", results = {
    @Result(name = SUCCESS, location = "/index.jsp")
})
@Action(value = "url", results = {
    @Result(name = SUCCESS, type= "json" , params = {"root", "result"})
})
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
Java Maven
手把手教你搭建Maven项目
手把手教你搭建Maven项目
31 0
|
2月前
|
Java Maven
java修改当前项目的maven仓库地址为国内
修改当前项目的maven仓库地址为国内
|
3月前
|
Dubbo Java 应用服务中间件
微服务框架(十)Maven Archetype制作Dubbo项目原型
  此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。   本文为Maven Archetype的制作及使用,使用archetype插件制作Dubbo项目原型
|
28天前
|
Java Maven 开发工具
maven导入项目出现Unable to import maven project: See logs for details
maven导入项目出现Unable to import maven project: See logs for details
11 0
maven导入项目出现Unable to import maven project: See logs for details
|
27天前
|
Java Maven
maven项目导出可执行jar
maven项目导出可执行jar
28 0
|
28天前
|
Java Maven
运行maven项目出现Error:java: JDK isn‘t specified for module ‘XXX‘
运行maven项目出现Error:java: JDK isn‘t specified for module ‘XXX‘
14 0
|
1月前
|
JavaScript Java 关系型数据库
实例!使用Idea创建SSM框架的Maven项目
实例!使用Idea创建SSM框架的Maven项目
35 0
|
2月前
|
Java Maven
Maven项目模块打包引入
Maven项目模块打包引入
28 0
|
2月前
|
Java 测试技术 Maven
boot项目添加运行参数的maven插件
boot项目添加运行参数的maven插件
32 0
|
3月前
|
IDE Java Maven
解决 idea maven项目启动项目不编译target 文件问题
解决 idea maven项目启动项目不编译target 文件问题
106 2

相关实验场景

更多

推荐镜像

更多