SpringMVC关于SSM的整合配置步骤


在这里插入图片描述

个人主页: 叶落闲庭
我的专栏:
c语言
数据结构
javaweb

石可破也,而不可夺坚;丹可磨也,而不可夺赤。


SSM整合

  • 一、创建工程
    • 1.1创建Maven工程
    • 1.2工程命名
    • 1.3检查&配置本地Maven路径
  • 二、SSM整合
    • 2.1导入相关依赖
    • 2.2添加tomcat插件(可选)
    • 2.3添加配置类
      • 2.3.1添加Spring配置类
      • 2.3.2添加Jdbc配置类
      • 2.3.3添加Mybatis配置类
      • 2.3.4创建整合类(整合Spring和SpringMVC)
      • 2.3.5创建SpringMVC配置类
  • 总结

一、创建工程

1.1创建Maven工程


SpringMVC关于SSM的整合配置步骤_第1张图片


1.2工程命名

SpringMVC关于SSM的整合配置步骤_第2张图片


1.3检查&配置本地Maven路径

SpringMVC关于SSM的整合配置步骤_第3张图片


二、SSM整合

2.1导入相关依赖

<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.13version>
    dependency>
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.3.3version>
    dependency>
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>8.0.32version>
    dependency>
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>1.2.16version>
    dependency>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.13version>
      <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.15.0version>
    dependency>
  dependencies>

2.2添加tomcat插件(可选)

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.mavengroupId>
        <artifactId>tomcat7-maven-pluginartifactId>
        <version>2.2version>
        <configuration>
        
          <port>80port>
          <path>/path>
        configuration>
      plugin>plugins>
  build>

2.3添加配置类

  • 项目包结构:

SpringMVC关于SSM的整合配置步骤_第4张图片


2.3.1添加Spring配置类

  • 创建包结构com.practice.config,创建SpringConfig
package com.practice.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;

/**
 * @Author YJ
 * @Date 2023/8/13 16:50
 * Description:Spring配置类
 */
@Configuration//表明配置类
@ComponentScan({"com.practice.service"})//扫描包路径
@PropertySource("jdbc.properties")//获取资源路径
@Import({JdbcConfig.class,MybatisConfig.class})//spring与mybatis整合
public class SpringConfig {
}

2.3.2添加Jdbc配置类

  • 创建jdbc属性文件并添加如下代码表示连接数据库信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=123456
  • com.practice.config下创建JdbcConfig配置类
package com.practice.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * @Author YJ
 * @Date 2023/8/13 16:51
 * Description:Jdbc配置类
 */
public class JdbcConfig {

    /*jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/mybatis
    jdbc.username=root
    jdbc.password=123456*/

	//通过注解,将jdbc属性文件中的属性获取到
    @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;
    }
}

2.3.3添加Mybatis配置类

package com.practice.config;

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

import javax.sql.DataSource;

/**
 * @Author YJ
 * @Date 2023/8/13 16:52
 * Description:Mybatis配置类
 */
public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.practice.domain");
        return factoryBean;
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.practice.dao");
        return msc;
    }
}

2.3.4创建整合类(整合Spring和SpringMVC)

package com.practice.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * @Author YJ
 * @Date 2023/8/13 17:12
 * Description:整合Spring和SpringMVC
 */
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[]{"/"};
    }
}

2.3.5创建SpringMVC配置类

package com.practice.config;

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

/**
 * @Author YJ
 * @Date 2023/8/13 17:13
 * Description:SpringMVC配置类
 */
@Configuration
@ComponentScan("com.practice.controller")
@EnableWebMvc
public class SpringMVCConfig {
}

总结

这篇文章主要是关于Spring的SSM整合的相关内容,基本步骤就已经总结完毕,欢迎各位小伙伴点赞+关注!!!

你可能感兴趣的:(#,JavaEE,spring,mybatis,maven,java-ee)