玩转SSH:Struts + Spring + MyBatis
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了玩转SSH:Struts + Spring + MyBatis相关的知识,希望对你有一定的参考价值。
一、创建 SSMDemo 项目
点击菜单,选择“File -> New Project” 创建新项目。选择使用 archetype 中的 maven-webapp 模版创建。
输入对应的项目坐标GroupId 和 ArtifactId。
之后在项目名称中填入项目名称,这里我填的 ProjectName 和上文的 ArtifactId 相同,都是 SSMDemo。
点击确定后,等待 Maven 帮我们构建好项目的目录结构。当控制台显示 BUILD SUCCESS 就表示初始化完成了。
初始化完成后的目录结构是这样的:
在 main 目录下创建 java 目录并将其设置为源码目录:
之后创建 com.chanshuyi.action 等包,创建完成后的目录结构如下:
二、导入依赖的包和插件
设置 POM.XML 配置文件中的全局属性:
<!-- 声明全局属性 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <struts.version>2.3.24</struts.version> <springframework.version>4.1.6.RELEASE</springframework.version> <junit.version>3.8.1</junit.version> <commonsLogging.version>1.2</commonsLogging.version> </properties>
依次引入 Struts、MyBatis、Spring、mysql、Druid、JUnit、Log4J 等的依赖 Jar 包:
<!-- ********************** Struts ********************** --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${struts.version}</version> </dependency> <!-- Struts 注解 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>${struts.version}</version> </dependency> <!-- ********************** MyBatis ********************** --> <!-- MyBatis 核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.1</version> </dependency> <!-- 添加mybatis与Spring整合的核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.1.1</version> </dependency> <!-- ********************** Spring ********************** --> <!-- 添加Spring-core包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-tx包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springframework.version}</version> </dependency> <!-- Spring ORM 相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-jdbc包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework.version}</version> </dependency> <!--添加spring-web包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframework.version}</version> </dependency> <!-- 添加spring-context包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>${commonsLogging.version}</version> </dependency> <!--添加aspectjweaver包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency> <!-- ********************** DB Connection ********************** --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.6</version> </dependency> <!-- ********************** 其他 ********************** --> <!-- 单元测试 JUnit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 日志记录 Log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.6</version> </dependency>
之后导入 Tomcat 启动插件,我们将通过 Maven 方式启动 Tomcat,这样就不必在本地配置一个 Tomcat 服务器。
<!-- Maven的Tomcat插件(支持Maven以"tomcat7:run"方式启动web项目) --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>5050</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> <finalName>mgr</finalName> <server>tomcat7</server> </configuration> </plugin>
配置完成后的 POM.XML 是这样的:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.chanshuyi.SSMDemo</groupId> 5 <artifactId>SSMDemo</artifactId> 6 <packaging>war</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>SSMDemo Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <!-- 声明全局属性 --> 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 <struts.version>2.3.24</struts.version> 15 <springframework.version>4.1.6.RELEASE</springframework.version> 16 <junit.version>3.8.1</junit.version> 17 <commonsLogging.version>1.2</commonsLogging.version> 18 </properties> 19 20 <dependencies> 21 <!-- ********************** Struts ********************** --> 22 <dependency> 23 <groupId>org.apache.struts</groupId> 24 <artifactId>struts2-core</artifactId> 25 <version>${struts.version}</version> 26 </dependency> 27 <dependency> 28 <groupId>org.apache.struts</groupId> 29 <artifactId>struts2-spring-plugin</artifactId> 30 <version>${struts.version}</version> 31 </dependency> 32 <!-- Struts 注解 --> 33 <dependency> 34 <groupId>org.apache.struts</groupId> 35 <artifactId>struts2-convention-plugin</artifactId> 36 <version>${struts.version}</version> 37 </dependency> 38 39 <!-- ********************** MyBatis ********************** --> 40 <!-- MyBatis 核心包 --> 41 <dependency> 42 <groupId>org.mybatis</groupId> 43 <artifactId>mybatis</artifactId> 44 <version>3.1.1</version> 45 </dependency> 46 <!-- 添加mybatis与Spring整合的核心包 --> 47 <dependency> 48 <groupId>org.mybatis</groupId> 49 <artifactId>mybatis-spring</artifactId> 50 <version>1.1.1</version> 51 </dependency> 52 53 <!-- ********************** Spring ********************** --> 54 <!-- 添加Spring-core包 --> 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-core</artifactId> 58 <version>${springframework.version}</version> 59 </dependency> 60 <!-- 添加spring-tx包 --> 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-tx</artifactId> 64 <version>${springframework.version}</version> 65 </dependency> 66 <!-- Spring ORM 相关--> 67 <dependency> 68 <groupId>org.springframework</groupId> 69 <artifactId>spring-orm</artifactId> 70 <version>${springframework.version}</version> 71 </dependency> 72 <!-- 添加spring-jdbc包 --> 73 <dependency> 74 <groupId>org.springframework</groupId> 75 <artifactId>spring-jdbc</artifactId> 76 <version>${springframework.version}</version> 77 </dependency> 78 <!--添加spring-web包 --> 79 <dependency> 80 <groupId>org.springframework</groupId> 81 <artifactId>spring-web</artifactId> 82 <version>${springframework.version}</version> 83 </dependency> 84 <!-- 添加spring-context包 --> 85 <dependency> 86 <groupId>org.springframework</groupId> 87 <artifactId>spring-context</artifactId> 88 <version>${springframework.version}</version> 89 </dependency> 90 <dependency> 91 <groupId>org.springframework</groupId> 92 <artifactId>spring-beans</artifactId> 93 <version>${springframework.version}</version> 94 </dependency> 95 <dependency> 96 <groupId>commons-logging</groupId> 97 <artifactId>commons-logging</artifactId> 98 <version>${commonsLogging.version}</version> 99 </dependency> 100 <!--添加aspectjweaver包 --> 101 <dependency> 102 <groupId>org.aspectj</groupId> 103 <artifactId>aspectjweaver</artifactId> 104 <version>1.8.5</version> 105 </dependency> 106 107 <!-- ********************** DB Connection ********************** --> 108 <dependency> 109 <groupId>mysql</groupId> 110 <artifactId>mysql-connector-java</artifactId> 111 <version>5.1.21</version> 112 </dependency> 113 <!-- Druid --> 114 <dependency> 115 <groupId>com.alibaba</groupId> 116 <artifactId>druid</artifactId> 117 <version>1.0.6</version> 118 </dependency> 119 120 <!-- ********************** 其他 ********************** --> 121 <!-- 单元测试 JUnit --> 122 <dependency> 123 <groupId>junit</groupId> 124 <artifactId>junit</artifactId> 125 <version>${junit.version}</version> 126 <scope>test</scope> 127 </dependency> 128 <!-- 日志记录 Log4j --> 129 <dependency> 130 <groupId>org.slf4j</groupId> 131 <artifactId>slf4j-api</artifactId> 132 <version>1.7.6</version> 133 </dependency> 134 <dependency> 135 <groupId>org.slf4j</groupId> 136 <artifactId>slf4j-log4j12</artifactId> 137 <version>1.7.6</version> 138 </dependency>
以上是关于玩转SSH:Struts + Spring + MyBatis的主要内容,如果未能解决你的问题,请参考以下文章