使用maven快速构建SSM项目

这里我就假设大家都已经配置好Maven,就不在重复说明了。若没有配置可以参考《首次使用maven的配置过程》。

1.新建Maven项目

工作区–右键—new—other–搜索maven
使用maven快速构建SSM项目_第1张图片

使用maven快速构建SSM项目_第2张图片

使用maven快速构建SSM项目_第3张图片

使用maven快速构建SSM项目_第4张图片

使用maven快速构建SSM项目_第5张图片

现在maven项目新建完毕,需要将其转化为一个动态web项目。

2.将Maven项目转为动态web项目

使用maven快速构建SSM项目_第6张图片

选中项目—右键–properties—project facets—Dynamic Web Module—(RunTime)Appache Tomcat—…
使用maven快速构建SSM项目_第7张图片

使用maven快速构建SSM项目_第8张图片

使用maven快速构建SSM项目_第9张图片

上面的步骤一般首次都会报如下错误,我们重复刚刚的操作就行
使用maven快速构建SSM项目_第10张图片

重复步骤如下:注意依然要再次选择Dynamic Web Module ,然后选择 Appache Tomcat,再重复下面的
使用maven快速构建SSM项目_第11张图片

使用maven快速构建SSM项目_第12张图片

这样Maven转为Web项目就成功了。现在来配置一下SSM架构了。

3.构建SSM框架

3.1配置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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>com.javagroupId>
  <artifactId>Maven_SSM_DemoartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>

  <name>Maven_SSM_Demoname>
  <url>http://maven.apache.orgurl>

  <properties>   
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    
    <spring-version>4.3.8.RELEASEspring-version>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>3.8.1version>
      <scope>testscope>
    dependency>

    <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.3.8.RELEASEversion>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring-version}version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.1version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.0version>
        dependency>

        
        <dependency>
            <groupId>com.mchangegroupId>
            <artifactId>c3p0artifactId>
            <version>0.9.5.2version>
        dependency>

        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.8.7version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.38version>
        dependency>
  dependencies>
project>

3.2配置web.xml


<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Maven_SSM_Demodisplay-name>
  <filter>
    <filter-name>characterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
    <init-param>
      <param-name>forceEncodingparam-name>
      <param-value>trueparam-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>characterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  <servlet>
    <servlet-name>SpringMVCservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:applicationContext.xmlparam-value>
    init-param>
  servlet>
  <servlet-mapping>
    <servlet-name>SpringMVCservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
  <filter>
    <filter-name>HiddenHttpMethodfilter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
  filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
web-app>

3.3有关Maven与Web基础的配置

选中项目—右键—properties—deployment Assembly–add(目的:让maven的jar映射到web的lib下)
使用maven快速构建SSM项目_第13张图片

使用maven快速构建SSM项目_第14张图片

最后,apply —ok保存即可

3.4新建Source Folder

在资源夹是专门用来放置配置文件的

使用maven快速构建SSM项目_第15张图片

使用maven快速构建SSM项目_第16张图片

3.5 applicationContext.xml配置


<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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/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">

    
    <mvc:default-servlet-handler />
    <mvc:annotation-driven>mvc:annotation-driven>

    <context:component-scan base-package="com.java">context:component-scan>

    
    <context:property-placeholder location="classpath:jdbc.properties" />
    
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc_driver}">property>
        <property name="jdbcUrl" value="${jdbc_url}">property>
        <property name="password" value="${jdbc_password}">property>
        <property name="user" value="${jdbc_user}">property>
    bean>

    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource">property>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource">property>
        
        <property name="mapperLocations" value="classpath:com/java/dao/*.xml">property>
        
        <property name="typeAliasesPackage" value="com.java.bean">property>
    bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory">constructor-arg>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.java.dao">property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
    bean>

beans>

3.6 jdbc.properties配置

# remark
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:orcl
#user=test1
#password=test1

#mysql connection
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/mybatis
jdbc_user=root
jdbc_password=123

这样使用Maven构建SSM项目就成功来。可以自己可以测试一下。

我的小demo项目–《Maven_SSM_Demo》,项目做了测试,有相关的sql文件,直接可以直接测试。

你可能感兴趣的:(maven)