SSM框架整合

Web相关

Posted by YD Blog on November 4, 2022

SSM框架整合

基础整合

创建项目并添加依赖

  <dependencies>

    <!--Spring核心框架-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <!--数据库相关-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.5</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.0</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.6</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.17</version>
    </dependency>

    <!--测试相关-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <!--AOP相关-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.9</version>
    </dependency>

    <!--SpringMVC相关-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <scope>provided</scope>
      <version>4.0.1</version>
    </dependency>

    <!--JSON解析-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.9</version>
    </dependency>

  </dependencies>

  <build>

      <plugins>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
            <!--端口控制-->
            <port>80</port>
            <!--项目路径控制意味着http://localhost/-->
            <path>/</path>
            <!--编码-->
            <uriEncoding>UTF-8</uriEncoding>
          </configuration>
        </plugin>
      </plugins>

  </build>

创建.properties文件用于存放数据库相关数据。

# 驱动配置,可根据url自动识别,可有可无
druid.driverClassName=com.mysql.cj.jdbc.Driver
# 连接数据库url
druid.url=jdbc:mysql://localhost:3306/${数据库名称}?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
# 用户名
druid.username=${username}
# 密码
druid.password=${password}
# 初始化连接数量
druid.initialSize=5
# 最大连接数量
druid.maxActive=10
# 最长等待时间
druid.maxWait=3000

创建druid配置类

public class DruidConfig {
    @Value("${druid.driverClassName}")
    private String driverClassName;

    @Value("${druid.url}")
    private String url;

    @Value("${druid.username}")
    private String username;

    @Value("${druid.password}")
    private String password;

    @Value("${druid.initialSize}")
    private int initSize;

    @Value("${druid.maxActive}")
    private int maxActive;

    @Value("${druid.maxWait}")
    private int maxWait;

    /**
     * 创建数据库连接池对象
     * @return 数据库连接池对象
     */
    @Bean
    public DataSource dataSource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driverClassName);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setInitialSize(initSize);
        ds.setMaxActive(maxActive);
        ds.setMaxWait(maxWait);

        return ds;
    }

    /**
     * 创建数据库连接池事务管理器
     * @return 数据库连接池事务管理器对象
     */
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);

        return transactionManager;
    }
}

创建MyBatis配置类

public class MyBatisConfig {

    /**
     * 创建MyBatis的工厂类对象
     * @param dataSource
     * @return SqlSessionFactoryBean
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setDataSource(dataSource);
        ssfb.setTypeAliasesPackage("org.YD");

        return ssfb;
    }


    /**
     * 创建Mapper代理扫描器对象
     * 将mapper接口文件都自动注入到IoC容器中,
     * 实现类Bean的名称默认为接口类名的首字母小写
     * @return MapperScannerConfigurer
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("org.YD.dao");

        return msc;
    }
}

创建Spring核心框架配置类

@Configuration
@ComponentScan({"org.YD.service"})
@PropertySource({"classpath:druid.properties"})
@EnableTransactionManagement
@Import({DruidConfig.class, MyBatisConfig.class})
public class SpringConfig {
}

创建SpringMvc配置类

@Configuration
@EnableWebMvc
@ComponentScan("org.YD.controller")
public class SpringMvcConfig {
}

创建Tomcat初始化类

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * 配置Spring核心容器
     * @return Spring核心容器配置类数组
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    /**
     * 配置SpringMVC容器
     * @return SpringMVC容器配置类数组
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    /**
     * 配置哪些请求由SpringMVC接管
     * @return 接管路径的字符串数组
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 配置过滤器
     * @return 过滤器数组
     */
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("utf-8");
        return new Filter[]{filter};
    }
}

如上就能完成SSM框架的基础整合了。