SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤

废话不多说,直接上图,上代码,看着图,跟着步骤,每一步都有注释代码
因为不是很熟悉,这个博客,所以很多字体都没设置样式,看起来可能比较累

那里有错可以加我微信或者QQ告诉下 共同学习
SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第1张图片
SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第2张图片

————–分割线—————

开始SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离–超详细步骤

SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第3张图片

SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第4张图片

创建父工程:

SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第5张图片

1. 打包方式选择packaging->pom 【pom包】
2. Maven创建时可以选择骨架site或者site-simple骨架 , 建议最好不用骨架
3. 导入pom.xml坐标 (子工程可以继承父工程的坐标) 子模块需要的依赖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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.javaee.mavengroupId>
    <artifactId>SSM_parentartifactId>
    
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>

    
    <modules>
        <module>SSM_daomodule>
        <module>SSM_servicemodule>
        <module>SSM_webmodule>
    modules>

    <properties>
        <spring.version>5.0.2.RELEASEspring.version>
        <slf4j.version>1.6.6slf4j.version>
        <log4j.version>1.2.12log4j.version>
        <mysql.version>5.1.6mysql.version>
        <mybatis.version>3.4.5mybatis.version>
        <aspectjweaver.version>1.6.8aspectjweaver.version>
        <junit.version>4.12junit.version>
        <jsp-api.version>2.0jsp-api.version>
        <servlet-api.version>2.5servlet-api.version>
        <jstl.version>1.2jstl.version>
        <mybatis-spring.version>1.3.0mybatis-spring.version>
        <druid.version>1.0.9druid.version>
        
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
    properties>

    
    
    <dependencies>
        
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>${aspectjweaver.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>

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

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>${servlet-api.version}version>
            <scope>providedscope>
        dependency>

        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>${jsp-api.version}version>
            <scope>providedscope>
        dependency>

        
        <dependency>
            <groupId>jstlgroupId>
            <artifactId>jstlartifactId>
            <version>${jstl.version}version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>${junit.version}version>
            <scope>compilescope>
        dependency>


        
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>${log4j.version}version>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>${slf4j.version}version>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>${slf4j.version}version>
        dependency>
        

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>${mybatis.version}version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>${mybatis-spring.version}version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>${druid.version}version>
        dependency>
    dependencies>
    <build>
        
        <plugins>
            
            <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <version>2.2version>
                
                <configuration>
                    
                    <port>18081port>
                    
                    <path>/path>
                    
                    <uriEncoding>UTF-8uriEncoding>
                configuration>
            plugin>
        plugins>
    build>
project>

创建子模块: SSM_dao 持久层

SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第6张图片
SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第7张图片

1.创建实体类 com.itcast.bean -> Items.class
2.创建接口类 com.itcast.mapper -> Items.interface
3.创建接口类的映射文件(注册sql语句) ->ItemsMapper.xml
4.创建applicationContext-dao.xml ,把mybatis交给spring管理 –>1.注册数据库 2.注册SqlSessionFactoryBean 3.配置MapprScannerConfigurer接口扫描包

Items.class–>

package com.itcast.bean;

import java.io.Serializable;
import java.util.Date;

public class Items  implements Serializable {
    /**
     * pojo
     */
        private Integer id;
        private String name;
        private Float price;
        private String pic;
        private Date createtime;
        private String detail;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Float getPrice() {
            return price;
        }

        public void setPrice(Float price) {
            this.price = price;
        }

        public String getPic() {
            return pic;
        }

        public void setPic(String pic) {
            this.pic = pic;
        }

        public Date getCreatetime() {
            return createtime;
        }

        public void setCreatetime(Date createtime) {
            this.createtime = createtime;
        }

        public String getDetail() {
            return detail;
        }

        public void setDetail(String detail) {
            this.detail = detail;
        }

    @Override
    public String toString() {
        return "Items{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", pic='" + pic + '\'' +
                ", createtime=" + createtime +
                ", detail='" + detail + '\'' +
                '}';
    }
}

Items.interface–>

package com.itcast.mapper;


import com.itcast.bean.Items;

public interface ItemsMapper {

    /**
     * 根据ID查询用户
     * @param id
     * @return
     */
    public Items findById(Integer id);
}

ItemsMapper.xml –>





<mapper namespace="com.itcast.mapper.ItemsMapper" >
    
    <select id="findById" parameterType="Integer" resultType="com.itcast.bean.Items">
        select * from items where id=#{id}
    select>
mapper>

applicationContext-dao.xml –>


<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" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
       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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd">

    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="luokangtao" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    bean>

    
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="typeAliasesPackage" value="com.itcast.bean"/>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="com.itcast.mapper"/>
    bean>


beans>

创建子模块: SSM_service 业务层

SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第8张图片

SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第9张图片

1.创建业务层的接口类 ItemsService.interface
2.创建业务层的接口实现类 ItemsServiceImpl.class (实现了: ItemsService.interface )
3.创建spring配置文件 –>applicationContext-service.xml –>1. 导入dao层的applicationContext-dao.xml 2.开启扫描组件Conponent-scan 3.配置事务管理器 4.配置事务规则tx:advice 5.配置切面 aop:config

ItemsService.interface—>

package com.itcast.service;

import com.itcast.bean.Items;

public interface ItemsService {

    /**
     * 根据ID查询用户
     * @param id
     * @return
     */
    public Items findById(Integer id);
}

ItemsServiceImpl.class (实现了: ItemsService.interface )

package com.itcast.service.Impl;

import com.itcast.bean.Items;
import com.itcast.mapper.ItemsMapper;
import com.itcast.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ItemsServiceImpl implements ItemsService {

    @Autowired
    private  ItemsMapper itemsMapper;
    @Override
    public Items findById(Integer id) {
        Items items = itemsMapper.findById(id);
        System.out.println("ItemsServiceImpl方法里面");
        return items;
    }
}

applicationContext-service.xml–>


<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" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
       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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd">

    
    <import resource="classpath:spring/applicationContext-dao.xml">import>
    
    
    <context:component-scan base-package="com.itcast.service"/>
    
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>
    
    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="modify*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        tx:attributes>
    tx:advice>
    
    <aop:config>
        <aop:pointcut expression="execution(* com.itcast.service.Impl.*.*(..))" id="tranpointcut"/>
        
        <aop:advisor advice-ref="txAdvice" pointcut-ref="tranpointcut"/>
    aop:config>
    
beans>

创建子模块: SSM_web 控制层

SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第10张图片

SSM(Spring5MVC/Spring5/Mybatis)分拆工程-前后端分离--超详细步骤_第11张图片

1.创建控制层Comtorller类 –> ItemsContorller.class
2.创建pages/Items.Jsp –> 静态网页
3.创建applicationContext.xml –> 1.导入service层的application-service.xml 配置文件
4.创建SpringMvc.xml –> 1.开启组件包扫描component-scan 2.开启注解驱动 mvc:annotation-driven 3.视图解析view.InternalResourceViewResolver 4.静态资源过滤
5.创建log4j日志–>log4J.properties
6.创建web.xml –>1.Spring核心监听器(配置Spring初始化) 2.配置前端控制器(SpringMVC) 3.Post的编码过滤器(防止中文乱码)

ItemsContorller.class–>

package com.itcast.web;

import com.itcast.bean.Items;
import com.itcast.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/ItemsContorller")
public class ItemsContorller {

    @Autowired
    private ItemsService itemsService;

    @RequestMapping(value = "/getById")
    public String getById(Model model){
        Items items = itemsService.findById(1);
        model.addAttribute("item",items);
        return "Items";
    }
}

pages/Items.Jsp 映射网页–>

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title heretitle>
head>
<body>
<form>
    <table width="100%" border=1>
        <tr>
            <td>商品名称td>
            <td> ${item.name } td>
        tr>
        <tr>
            <td>商品价格td>
            <td> ${item.price } td>
        tr>
        <tr>
            <td>生成日期td>
            <td> <fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/> td>
        tr>
        <tr>
            <td>商品简介td>
            <td>${item.detail} td>
        tr>
    table>
form>
body>
html>

applicationContext.xml –>


<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" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
       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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd">

    
    <import resource="classpath:spring/applicationContext-service.xml">import>
beans>

SpringMvc.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
       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.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <context:component-scan base-package="com.itcast.web"/>
    
    <mvc:annotation-driven/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/WEB-INF/pages/"/>
        
        <property name="suffix" value=".jsp"/>
    bean>
    
    <mvc:default-servlet-handler/>
beans>

log4J.properties–>

##设置日志记录到控制台的方式
log4j.appender.std=org.apache.log4j.ConsoleAppender
log4j.appender.std.Target=System.err
log4j.appender.std.layout=org.apache.log4j.PatternLayout
log4j.appender.std.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss}%5p%c{1}:%L-%m%n

##设置日志记录到文件的方式
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=mylog.txt
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE}%5p%c{1}:%L-%m%n

##日志输出的级别,以及配置记录方案 debug级别 std控制台输出 file文件输出
log4j.rootLogger=debug,std,file

web.xml–>



<web-app 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"
         version="3.0">
  
  <filter>
    <filter-name>characterEncodingfilter-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>characterEncodingfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>


  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>

  
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>
    
    <load-on-startup>1load-on-startup>
  servlet>
  
  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

最后一步就是数据库mysql数据库

根据需求来修改接口类,实体类,数据库

CREATE TABLE `items` (
  `id` INT(10) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(20) DEFAULT NULL,
  `price` FLOAT(10,0) DEFAULT NULL,
  `pic` VARCHAR(40) DEFAULT NULL,
  `createtime` DATETIME DEFAULT NULL,
  `detail` VARCHAR(200) DEFAULT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=INNODB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

总结:依赖整合和运行调试

1.每个模块都需要 spring 或者 junit 的 jar,况且最终 package 打完包最后生成的项目中的
jar 就是各个模块依赖的整合,所以我们可以把项目中所需的依赖都可以放到父工程中,模块
中只留模块和模块之间的依赖

2.运行调试:
方法 1:在 ssm_web 工程的 pom.xml 中配置 tomcat 插件运行
运行 ssm_web 工程它会从本地仓库下载依赖的 jar 包,所以当 ssm_web 依赖的 jar 包内容修
改了必须及时发布到本地仓库,比如:ssm_web 依赖的 ssm_service 修改了,需要及时将ssm_service 发布到本地仓库。

方法 2:在父工程的 pom.xml 中配置 tomcat 插件运行,自动聚合并执行
推荐方法 2,如果子工程都在本地,采用方法 2则不需要子工程修改就立即发布到本地仓库,
父工程会自动聚合并使用最新代码执行。

注意:如果子工程和父工程中都配置了 tomcat 插件,运行的端口和路径以子工程为准。

你可能感兴趣的:(javaEE,SSM)