Java-SSM基础框架搭建(内含视频教学)


Java-SSM基础框架搭建(内含视频教学)_第1张图片


QQ:1755497577(备注:博客)

B站:code_ant(java相关培训视频)

Java-SSM基础框架搭建(内含视频教学)_第2张图片


##简述

今天主要讲诉一下ssm框架的搭建过程,由于springboot的广泛使用,很多人都忘记了怎么搭建ssm项目,这里做一个温习。

**demo地址:**https://github.com/LiJinHongPassion/ssmdemo2

目的

  • 温习ssm框架
  • 学习ssm框架,从而加深对springboot项目的灵活使用

准备

名称 版本
idea 2019
jdk 1.8
tomcat 8.5
maven 3.5
mysql 5.7

spring的请求方式

首先我们需要知道一个http请求是怎么经过spring的处理后得到响应的。

Java-SSM基础框架搭建(内含视频教学)_第3张图片

搭建

了解了请求的处理过程,下面开始搭建项目。

因为idea需要初始化项目索引,所以会导致很卡,会很吃内存。

配置ssm主要涉及到下面几个配置文件:

  • spring-mvc.xml mvc配置
  • spring-mybatis.xml 整合mybatis
  • jdbc.properties 数据库连接池的属性配置
  • web.xml

项目结构

这里项目结构不对,需要重新添加需要的文件夹,完成后如下:

Java-SSM基础框架搭建(内含视频教学)_第4张图片

添加依赖

修改pom.xml文件

<properties>
    
    <spring.version>4.0.2.RELEASEspring.version>
    
    <mybatis.version>3.2.6mybatis.version>
properties>


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

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

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

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

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

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${spring.version}version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>${mybatis.version}version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.2.2version>
    dependency>
    
    <dependency>
      <groupId>javaxgroupId>
      <artifactId>javaee-apiartifactId>
      <version>7.0version>
    dependency>
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.30version>
    dependency>
    
    <dependency>
      <groupId>commons-dbcpgroupId>
      <artifactId>commons-dbcpartifactId>
      <version>1.2.2version>
    dependency>

    <dependency>
      <groupId>org.codehaus.jacksongroupId>
      <artifactId>jackson-mapper-aslartifactId>
      <version>1.9.13version>
    dependency>
    
    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>1.3.1version>
    dependency>
    <dependency>
      <groupId>commons-iogroupId>
      <artifactId>commons-ioartifactId>
      <version>2.4version>
    dependency>
    <dependency>
      <groupId>commons-codecgroupId>
      <artifactId>commons-codecartifactId>
      <version>1.9version>
    dependency>

spring-mvc.xml配置

添加依赖后,开始配置项目,先配置mvc

主要配置下面几个方面:

  • 静态资源访问权限
  • 开启注解
  • 视图解析

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

    
    <mvc:resources mapping="/pic/**" location="/pic/"/>
    
    <mvc:annotation-driven>mvc:annotation-driven>


    
    <context:component-scan base-package="com.cqut.li.test_ssm.controller"/>

    
    <bean id="mappingJacksonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8value>
            list>
        property>
    bean>

    
    <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter"/>    
            list>
        property>
    bean>

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

    
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
        <property name="defaultEncoding" value="utf-8"/>
        
        <property name="maxUploadSize" value="10485760000"/>
        
        <property name="maxInMemorySize" value="40960"/>
    bean>

beans>

spring-mybatis.xml配置

该配置文件主要是用于整合mybatis,主要配置以下内容:

  • 数据库连接池
  • 事务

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    <context:component-scan base-package="com.cqut.li.test_ssm.*"/>
    
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>
    bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        
        <property name="initialSize" value="${initialSize}">property>
        
        <property name="maxActive" value="${maxActive}">property>
        
        <property name="maxIdle" value="${maxIdle}">property>
        
        <property name="minIdle" value="${minIdle}">property>
        
        <property name="maxWait" value="${maxWait}">property>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="mapperLocations" value="classpath:mapping/*.xml">property>
    bean>

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

    
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>

beans>

jdbc.properties配置

下面需要配置jdbc配置文件

driver=com.mysql.jdbc.Driver
#url=jdbc:mysql://127.0.0.1:3306/laboratory?useUnicode=true&characterEncoding=utf8
#url=jdbc:mysql://superc102.vicp.cc:3307/laboratory?useUnicode=true&characterEncoding=utf8
url=jdbc\:mysql\://127.0.0.1\:3306/financialtest?useUnicode\=true&characterEncoding\=utf8
username=root
password=root
#定义初始连接数
initialSize=1
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最大等待
maxWait=60000
#处理超时断开连接而没有销毁的情况
#验证查询语句
validationQuery=SELECT COUNT(*) FROM role
#周期性检测-半个小时
timeBetweenEvictionRunsMillis=1800000
#连接空闲时间 --半个小时
minEvictableIdleTimeMillis=1800000
#取得对象时是否进行验证,检查对象是否有效
testOnBorrow=true
#返回对象时是否进行验证,检查对象是否有效
testOnReturn=true
#空闲时是否进行验证,检查对象是否有效
testWhileIdle=true

web.xml配置

主要配置以下内容:

  • spring容器
  • 编码过滤器
  • mvc分发器
  • spring监听器
  
<context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring-*.xmlparam-value>
  context-param>

  
  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <async-supported>trueasync-supported>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>encodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>


  <listener>
    <description>spring监听器description>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
  listener>

  
  <servlet>
    <servlet-name>SpringMVCservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:spring-mvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
    <async-supported>trueasync-supported>
  servlet>
  <servlet-mapping>
    <servlet-name>SpringMVCservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

  
  <welcome-file-list>
    <welcome-file>/index.jspwelcome-file>
  welcome-file-list>

项目启动配置

Java-SSM基础框架搭建(内含视频教学)_第5张图片

项目导入&测试(视频)

视频地址:http://player.bilibili.com/player.html?aid=69732312&cid=120835662&page=1

你可能感兴趣的:(java)