SSM整合开发步骤

SSM整合开发步骤

SSM: SpringMVC + Spring +Mybatis
SpringMVC : 视图层,界面层,负责接收请求,处理结果
Spring :业务层,负责管理service和dao,工具类对象
Mybatis:持久层,负责访问数据库

用户发起请求---->SpringMVC—>Spring是service对象---->Mybatis

开发步骤

1、新建maven-web项目
SSM整合开发步骤_第1张图片2、添加依赖
SpringMVC、Spring、Mybatis、jackson、mysql驱动、druid连接池、jsp、Servlet

 <dependencies>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.2.1-b03version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.2.0.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>5.2.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.2.0.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.9version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.12version>
        dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>2.9.0version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.9.0version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.1version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.1version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.12version>
            <scope>providedscope>
        dependency>
    dependencies>
    <build>
    <resources>
      <resource>
        <directory>src/main/javadirectory>
        <includes>
          <include>**/*.propertiesinclude>
          <include>**/*.xmlinclude>
        includes>
        <filtering>falsefiltering>
      resource>
    resources>
  build>

3、写web.xml文件
1)注册DispatcherServlet
目的:1.创建SpringMVC容器,才能创建controller对象
           2.创建的是servlet对象,才能接收请求
2)注册spring监听器:ContextLoaderListener
目的:创建spring的容器对象,才能创建controller和dao的对象
3)注册字符集过滤器,解决post请求乱码问题


<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_4_0.xsd"
         version="4.0">

    
    <servlet>
      <servlet-name>mywebservlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
      
      <init-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:conf/dispatcherServlet.xmlparam-value>
      init-param>
      <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
      <servlet-name>mywebservlet-name>
      <url-pattern>/*.dourl-pattern>
    servlet-mapping>

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

  
  <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>forceRequestEnCodingparam-name>
    <param-value>trueparam-value>
  init-param>
    <init-param>
      <param-name>forceResponseEnCodingparam-name>
      <param-value>trueparam-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>characterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
web-app>

4.创建包, Controller包, service ,dao,实体类包名创建好
SSM整合开发步骤_第2张图片
5.写springmvc,spring,mybatis的配置文件
1)springmvc配置文件dispactherContext.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/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/context 
      https://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/mvc 
      https://www.springframework.org/schema/mvc/spring-mvc.xsd">

   
   <context:component-scan base-package="controller" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/jsp/" />
       <property name="suffix" value=".jsp" />
   bean>

   <mvc:annotation-driven />
   
beans>
 

2)数据库的属性配置文件jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/dbspring
jdbc.username=root
jdbc.password=2580
jdbc.max=30

3)spring配置文件applicationContext

 
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/context 
       https://www.springframework.org/schema/context/spring-context.xsd">
    

    
    <context:property-placeholder location="classpath:conf/jdbc.properties" />

    
    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close" >
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="${jdbc.max}" />
     bean> 
         
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource" ref="datasource" />
        <property name="configLocation" value="classpath:conf/mybatis.xml" />
    bean>

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

    
    beans>

4)mybatis主配置文件 mybatis.xml



<configuration>
    <typeAliases>
        
        <package name="domain"/>
    typeAliases>
    
    <mappers>
        <package name="dao"/>
    mappers>
configuration>

6)根据需要写代码, dao接口和mapper文件, service和实现类,controller, 实体类。

7)写jsp页面

你可能感兴趣的:(SpringMVC)