eclipse+maven搭建ssm框架

Posted symcheal

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了eclipse+maven搭建ssm框架相关的知识,希望对你有一定的参考价值。

参考地址:https://www.cnblogs.com/liangguangqiong/p/7965778.html

0、系统环境

1)Windows 10 企业版

2)JDK 1.8.0_131

3)eclipse-jee-photon-R-win32

4)Tomcat 8.5

1、maven下载及配置

maven的下载地址:http://maven.apache.org/download.cgi

技术图片

 

 

 下载后解压放在d:Java路径下

技术图片

在环境变量中,新增系统变量名:MAVEN_HOME,变量值:D:Javamaven

在Path中追加:%MAVEN_HOME%in;

在命令行窗口中输入:mvn –v,如果看见下图则说明maven安装配置完毕

技术图片

 

 

 2、maven仓库的设置

maven仓库默认位置在系统用户目录下的.m2目录中,类似下图

技术图片

 

因为众所周知的原因,直接访问maven公共仓库的速度比较慢,所以推荐使用阿里的maven仓库镜像。编辑setting.xml文件,在mirrors节点下,新增如下内容,这样从仓库中下载jar包速度上会快很多

 

<mirror>
       <id>nexus-aliyun</id>
       <mirrorOf>*</mirrorOf>
       <name>Nexus aliyun</name>
       <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

当然也可以更改默认的位置,再maven包中的conf文件夹中的settings文件中的settings标签中添加自己设置仓库的路径如下代码,这样再项目中添加的依赖包都会去阿里云仓库下载到指定的本地仓库当中。

<localRepository>D:myworks
epository</localRepository>

3、eclipse整合maven

设置eclipse自带maven整合工具,在Preferences中找到Maven节点,观察User Settings项的设置是否正确

技术图片

 

 

 点击Installations节点,添加maven runtime,再勾选刚才添加的maven

技术图片

 

 

 4、创建maven项目时设置JDK

问题描述:eclipse创建maven项目时,显示的JDK默认版本为1.5,实际使用的JDK为1.8,如何修改?

解决方案:找到本机maven仓库存放位置,比如:${user.home}/.m2/路径,编辑settings.xml文件,在profiles节点下配置

<profile>

    <id>jdk-1.8</id>

    <activation>

        <activeByDefault>true</activeByDefault>

        <jdk>1.8</jdk>

    </activation>

    <properties>

        <maven.compiler.source>1.8</maven.compiler.source>

        <maven.compiler.target>1.8</maven.compiler.target>

        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>

    </properties>

</profile>

5、使用maven创建SSM项目

 

1)选择Maven Project

技术图片

 

 

 2)点击Next,选择默认工作空间位置

技术图片

 

 

 3)选择web类型

技术图片

 

 

 4)填写GroupID、ArtifactID

技术图片

 

 

 

Group ID:相当于一个组织

Artifact ID:相当于这个组织下的一个具体项目

Packege:根据Group ID和Artifact ID生成一个默认的名称

5)创建出的maven项目如下图

技术图片

 

 

 6)问题描述:提示错误:

技术图片

 

 

 在eclipse中preference设置Server为Tomcat,注意JRE设置为安装的JDK的jre

技术图片

 

 

 在工程上右键,查看工程属性,找到Java Build Path,添加Server Runtime为Tomcat

技术图片

 

点击Finish后,项目工程变为下图

技术图片

 

 

 这里需要说明下:

技术图片

 

 

 

 技术图片

 

 

 7)在项目上右键,查看项目信息

技术图片

 

 

 

默认的Dynamic Web Module为2.3,使用Tomcat 8.5,需要修改为3.1

修改方法:

① maven工程所在目录下org.eclipse.wst.common.project.facet.core.xml

技术图片

 

 

 编辑内容,如下所示

<?xml version="1.0" encoding="UTF-8"?>

<faceted-project>

  <fixed facet="wst.jsdt.web"/>

  <installed facet="java" version="1.8"/>

  <installed facet="jst.web" version="2.3"/>

  <installed facet="wst.jsdt.web" version="1.0"/>

</faceted-project>

改为

<?xml version="1.0" encoding="UTF-8"?>

<faceted-project>

  <fixed facet="wst.jsdt.web"/>

  <installed facet="java" version="1.8"/>

  <installed facet="jst.web" version="3.1"/>

  <installed facet="wst.jsdt.web" version="1.0"/>

</faceted-project>

② maven工程下的web.xml文件修改为

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
       version="3.1" metadata-complete="true">
</web-app>

③ pom.xml文件中修改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>

④ 修改后,在项目上右键,找到Maven属性下的Update Project,点击之

技术图片

 

 

 ⑤ 选择该项目进行更新,如果怕不能强制更新,可以勾选Force Update of Snapshots/Releases

技术图片

 

 

 ⑥ 点击OK后更新maven项目,再观察项目属性,Module已经变为3.1

技术图片

6、下载ssm框架所需jar包

修改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>cn.temptation</groupId>
 <artifactId>ssm</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>ssm Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <log4j.version>1.2.17</log4j.version>
          <spring.version>4.3.8.RELEASE</spring.version>
          <mybatis.version>3.2.2</mybatis.version>
  </properties>
   <dependencies>
           <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</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-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument-tomcat</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-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-messaging</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</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-webmvc-portlet</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-websocket</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- mybatis -->
        
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        
        
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- 上传下载 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        
        <dependency>  
           <groupId>javax</groupId>  
            <artifactId>javaee-api</artifactId>  
            <version>8.0</version>  
            <scope>provided</scope>  
        </dependency> 
     
        
         <dependency>  
           <groupId>javax</groupId>  
            <artifactId>javaee-web-api</artifactId>  
            <version>8.0</version>  
            <scope>provided</scope>  
        </dependency> 
        <dependency> 
              <groupId>net.sf.json-lib</groupId>
              <artifactId>json-lib</artifactId>
              <version>2.1</version>
              <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency> 
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>
   </dependencies>
 <build>
 <finalName>ssm</finalName>
 <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>
</project>

保存后,可以看到相关jar包被下载至本地仓库

7、完善项目结构

因为服务端maven项目的标准结构有四个子包:src/main/java、src/main/resources、src/test/java、src/test/resources,这里缺少了src/test/resources,所以手动补上。

在项目中新建Source Folder

技术图片

 

 

 创建src/test/resources目录后,工程如下图所示

技术图片

 

右键查看项目属性,点击Deployment Assembly,移除test和target

 

 技术图片

 

 

 移除后如下图

技术图片

 

 

 8、创建项目用的配置文件

技术图片

 

创建log4j.properties文件,内容如下:

#USE THIS SETTING FOR OUTPUT MYBATIS`s SQL ON THE CONSOLE
log4j.rootLogger=DEBUG, Console

#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n

log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEB

创建spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
    <!-- 自动扫描包 -->
    <context:component-scan base-package="com.cqust.*" />
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
        <property name="url" value="jdbc:mysql://localhost:3306/performancesystem"></property> 
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    <!-- mybatis配置 -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage" value="com.supermanager.entity"></property>

    </bean>
    <!-- 映射配置器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cqust.mapper"></property>
    </bean>
    <!-- 事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 开启事务注解 -->
    <tx:annotation-driven />

</beans>

创建springmvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
<context:component-scan base-package="com.cqust.*"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>

<!-- json中文乱码 -->
<!-- <mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supporteMediaTypes">
                <list>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>    
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8" />
        <property name="maxUploadSize" value="500000000" />
</bean>

</beans>

最后就是web.xml

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mybatis.xml</param-value>
  </context-param>
   <!-- 监听配置 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- springmybatis配置 -->
  <!-- springmvc -->
  <servlet>
      <servlet-name>dispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>dispatcherServlet</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- 过滤器 -->
  <filter>
      <filter-name>characterEncoding</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>characterEncoding</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
   <!-- log4j配置 -->
 
 <!--  <listener>
      <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <context-param>
      <param-name>log4jConfigLocation</param-name>
      <param-value>classpath:log4j.properties</param-value>
  </context-param>
 -->
</web-app>

 

以上是关于eclipse+maven搭建ssm框架的主要内容,如果未能解决你的问题,请参考以下文章

eclipse + maven搭建SSM框架

sts(eclipse)+maven搭建ssm框架

SSM框架搭建总结

在Eclipse中利用maven整合搭建ssm框架

eclipse搭建maven的web项目(SSM框架)

搭建maven版SSM框架