使用maven搭建SSM环境

框架 专栏收录该内容
16 篇文章 0 订阅

maven搭建SSM

准备

  • eclipse

Eclipse Java EE IDE for Web Developers. Version: Photon Release (4.8.0)

官网

参考这里

  • maven

Apache Maven 3.3.9

maven下载地址

eclipse里自带了maven,但是建议使用外部的maven,然后设置maven远程仓库地址为阿里云的(速度快)

  • 配置外部maven
    配置外部maven

window -> preferences -> Maven -> Installation

点击 Add,在"new Maven Runtime"里,点击Directory选择maven安装目录,然后点击Finish,添加成功!

勾选刚刚添加的 apache-maven,然后点击Apply,应用

去maven的安装目录

...\apache-maven..xxx\conf\setting.xml中,修改settings.xml,将mirrors替换为以下内容

<mirrors>
     <!-- 阿里云的maven仓库地址 -->
        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
        <mirror>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <url>http://repo1.maven.org/maven2/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
        <mirror>
            <id>repo2</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo2.maven.org/maven2/</url>
        </mirror>
        <mirror>
            <id>ibiblio</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
        </mirror>
        <mirror>
            <id>jboss-public-repository-group</id>
            <mirrorOf>central</mirrorOf>
            <name>JBoss Public Repository Group</name>
            <url>http://repository.jboss.org/nexus/content/groups/public</url>
        </mirror>
        <!-- 中央仓库在中国的镜像 -->
        <mirror>
            <id>maven.net.cn</id>
            <name>oneof the central mirrors in china</name>
            <url>http://maven.net.cn/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>

    </mirrors>

然后选择 User Settings ->Browse,将maven安装目录下的/config/setting.xml选中,点击Apply应用。

修改配置文件位置

步骤

  • 新建maven项目

    new -> other搜索maven,选则maven Project
    新建maven项目

点击next
新建maven项目

点击 next

新建maven项目

在Filter中搜索web,然后选中org.apache.maven.archetypes maven-archetype-webapp,点击next

新建maven项目

填写好Group Id,和 Artifact Id,点击Finish

Group id: 组织id,域名倒写;
Artifact id:项目、模块id;
Version: 版本;
Packaging: 该元素决定了项目的打包方式,有3种,jar,war,pom(比如是一个父模块);
Maven为jar项目调用了maven-jar-plugin,为war项目调用了maven-war-plugin,换言之,packaging直接影响Maven的构建生命周期

生成的项目如下

新建maven项目

初次创建可能会比较慢,多等待一会即可。

  • 补全 Source Folder:
    src/main/resources
    src/main/java
    src/test/resources
    src/test/java

项目上右键-> new ->Source Folder

在这里插入图片描述

  • 在项目上右键 选择 properties ->java Build Path,选择JRE System Library -> Eidt,更换jre为workspace defautlt jre

在这里插入图片描述

  • 在项目上右键 选择 properties ->Deployment Assembly,去掉这两项部署(不需要)

在这里插入图片描述

最终,如果没有的需要手动填上

在这里插入图片描述

maven项目报错

在这里插入图片描述

没有找到javax.servlet.http.HttpServlet 的jar包

我们一会在pom.xml中加入即可

  • 修改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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.oracle</groupId>
      <artifactId>ssm-maven</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>ssm-maven Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <properties>
            <!-- Spring版本号 -->
            <spring.version>4.3.8.RELEASE</spring.version>
        </properties>
        <dependencies>
            <!-- 添加Servlet支持 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.1</version>
                <scope>provided</scope>
            </dependency>
    
            <!-- JSP标准标签库 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
    
            <!-- Spring相关包 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!-- AOP相关包 -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>1.8.0</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.0</version>
            </dependency>
    
            <!-- MyBatis相关包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.3.0</version>
            </dependency>
            <!-- MySQL相关包 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.26</version>
            </dependency>
            <!-- 数据库连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.20</version>
            </dependency>
    
            <!-- Spring集成MyBatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.3</version>
            </dependency>
    
            <!-- 日志相关包 -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.21</version>
            </dependency>
    
            <!-- 单元测试相关包 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
            	<groupId>commons-dbcp</groupId>
            	<artifactId>commons-dbcp</artifactId>
            	<version>1.2.2</version>
            </dependency>
            <dependency>
            	<groupId>commons-pool</groupId>
            	<artifactId>commons-pool</artifactId>
            	<version>1.3</version>
            </dependency>
        </dependencies>
      <build>
        <finalName>ssm-maven</finalName>
      </build>
    </project>
    
    
  • 建立如下包结构

在这里插入图片描述

配置文件放入 src/main/resources

分别是

  1. jdbc.properties
  2. log4j.properties
  3. spring-application.xml
  4. spring-datasource.xml
  5. spring-mvc.xml
#jdbc.properties
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/mavenTest?useUnicode=true&characterEncoding=UTF-8
jdbc_user=root
jdbc_password=123456


#log4j.properties
#OFF FATAL ERROR WARN INFO DEBUG ALL
log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%p]%-d{yyyy-MM-dd HH\:mm\:ss}[%c\:%L] %m%n
<!--   spring-application.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:context="http://www.springframework.org/schema/context"
    xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
    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://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
    
    <!-- 注解扫描路径,加载Service -->
    <context:component-scan base-package="com.oracle.model.service"/>

</beans>

<!--   spring-datasource.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:mybatis="http://mybatis.org/schema/mybatis-spring"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
	">
	
	<!-- 引用jdbc.properties -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc_driver}"/>
		<property name="url" value="${jdbc_url}"/>
		<property name="username" value="${jdbc_user}"/>
		<property name="password" value="${jdbc_password}"/>
	</bean>
	
	<!-- 配置SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
		<property name="dataSource" ref="dataSource" ></property>
		<!-- entity配置,entity以及entity的配置文件 -->
		<property name="mapperLocations" value="classpath:com/oracle/entity/*Mapper.xml" />  
	</bean>
	
	
	<!-- 配置扫描报,加载dao对象 -->
	<mybatis:scan base-package="com.oracle.model.dao" />

</beans>





<!--   spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
     
     <!-- 打开springmvc映射器和适配器 -->
     <mvc:annotation-driven/>
     
     <!-- 扫描控制器 -->
      <context:component-scan base-package="com.oracle.controller"/>
     

	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/pages/" p:suffix=".jsp"/>
     
	 
</beans>
  • 在web.xml中配置springmvc(src/main/webapp/WEB-INF/下,在工程里Deploy Resources也映射了)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>sxnd-ssm</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
    
    <!-- 配置文件spring-application.xml和spring-datasource.xml -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-application.xml,classpath:spring-datasource.xml</param-value>
	</context-param>
    
    <!-- 配置springMVC -->
	<servlet>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>/</url-pattern> <!-- 配置springMVC处理所有请求 -->
	</servlet-mapping>

</web-app>

到这里,ssm环境便搭建完毕

下面测试进行简单测试

在com.oracle.controller下建立HelloWorldController,并且注解@Controller,@RequestMapping(“hello”)

写一个方法注解@RequestMapping(“world”),方法内返回一个视图

package com.oracle.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("hello")
public class HelloWorldController {

	
	@RequestMapping("world")
	public ModelAndView helloworld() {
		
		ModelAndView view = new ModelAndView("HelloWorld");
		return view;
	}
}

我们访问 xxxx/hello/world,就会返回一个HelloWorld视图,在spring-mvc中配置了视图的前缀为"/pages/",后缀为".jsp",所以实际路径是 从网站根目录开始 /pages/HelloWorld.jsp ,我们在HelloWorld中输出一个 你好。

此次的过程为 : 视图发起请求 -》到控制器 -》 控制器返回一个视图

  • 运行项目

    访问 http://localhost:8080/ssm-maven/hello/world

在这里插入图片描述

参考链接

https://blog.csdn.net/kingyxfly/article/details/78942439

https://www.jianshu.com/p/f3c582edca2b

https://blog.csdn.net/qq_39530754/article/details/82561985

https://blog.51cto.com/85608547/2161548

https://blog.csdn.net/Romantic_321/article/details/80752331

https://blog.csdn.net/u010134382/article/details/84745304

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页

打赏作者

the_power

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值