SSM Spring+Mybatis+SpringMVC技术整合流程

在Java工程创建以后(工程目录结构如下图所示),要实现Spring、Mybatis、SpringMVC的整合,需首先进行技术间的整合,使得三种技术能够相互配合进行工作。

SSM Spring+Mybatis+SpringMVC技术整合流程_第1张图片

相关配置文件

  • Spring
    SpringConfig
  • Mybatis
    MybatisConfig
    JdbcConfig
    jdbc.properties
  • SpringMVC
    ServletConfig
    SpringMvcConfig

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.wugroupId>
    <artifactId>ssmartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>

    <name>ssm Maven Webappname>
    
    <url>http://www.example.comurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.2.10.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.2.10.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>5.2.10.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.5version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.0version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.28version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.16version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.9.0version>
        dependency>
    dependencies>
    <build>
      <plugins>
          <plugin>
              <groupId>org.apache.tomcat.mavengroupId>
              <artifactId>tomcat7-maven-pluginartifactId>
              <version>2.2version>
              <configuration>
                  <port>80port>
                  <path>/path>
              configuration>
          plugin>
      plugins>
    build>
project>

导入所需要的依赖坐标

  • spring-webmvc
  • spring-jdbc
  • spring-test
  • mybatis
  • mybatis-spring
  • mysql-connector-java
  • druid
  • junit
  • javax.servlet-api
  • jackson-databind

SpringConfig.class
通过@Configuration注解来设置当前类为控制类;通过@CompoentScan注解来加载该控制类所控制的Bean;通过@PropertySource注解来加载properties文件,实现属性加载;通过@Import注解来加载Jdbc和Mybatis的配置类。

package com.wu.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

@Configuration  //设置当前类为控制类
@ComponentScan({"com.wu.service"})  // 加载该控制类所控制的Bean
@PropertySource("classpath:jdbc.properties")  // 去掉classpath:会报错
@Import({JdbcConfig.class, MybatisConfig.class})
public class SpringConfig {}

JdbcConfig.class
通过@Value注解来加载properties文件中的属性

package com.wu.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;

public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

MybatisConfig.class
创建SqlSessionFactoryBean和MapperScannerConfigurer扫描映射

package com.wu.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.wu.domain");
        return factoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.wu.dao");
        return msc;
    }
}

jdbc.properties
配置数据库连接信息

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db
jdbc.username=root
jdbc.password=

ServletConfig.class
配置web容器配置类,继承AbstractAnnotationConfigDispatcherServletInitializer,重写三个方法。getRootConfigClasses加载Spring核心配置,getServletConfigClasses加载SpringMvc核心配置。

package com.wu.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

SpringMvcConfig.class
通过注解设置为配置类,配置扫描包,添加@EnableWebMvc注解

package com.wu.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan("com.wu.controller")
@EnableWebMvc
public class SpringMvcConfig {
}

进行完上述操作以后,SSM框架整合工作已经结束,后续进行相关功能模块的编写即可。

你可能感兴趣的:(Java学习,java,SSM,Spring,Mybatis,SpringMVC,后端)