Spring基本功训练之搭建SSM项目

为什么还要去搭建SSM

在老期的项目中,一般都是使用ssm项目做开发的,虽然现在的主流 开发是springboot来做开发,但是ssm的基本整合还是需要掌握的。必须能快速的搭建出SSM框架,这属于基本功。
下周日开始进行Spring源码学习

整合SSM框架要做哪些事情:

  • SpringMVC

    1. web.xml
      a. 前端调度器servlet
      b. 编码过滤器filter
      c. 支持rest的过滤器
    2. springmvc.xml
      a. 扫描controller包
      b. 添加
      c. 视图解析器 d. 静态资源解析
    3. 添加控制器类…
  • Spring

    1. web.xml
      a. 监听器(在启动web容器时加载)
    2. spring.xml
      a. 扫描所有除了controller包的其他包
      b. 声明式事务
  • MyBatis

    1. 需要和spring整合
      a. 将sqlSessionFactory 配置为spring的bean

       i. 数据源 配置为spring的bean
       ii. 全局配置文件
       iii. 所有mapper映射文件
      

      b. 将mapper接口的包交给spring

    2. 加入全局配置文件

项目整体图

在这里插入图片描述

POM文件

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>cn.tulingxueyuan</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <SPRING.VERSION>5.2.6.RELEASE</SPRING.VERSION>
        <ASPECTJWEAVER.VERSION>1.9.5</ASPECTJWEAVER.VERSION>
    </properties>

    <dependencies>
        <!--springmvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${SPRING.VERSION}</version>
        </dependency>
        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${SPRING.VERSION}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${ASPECTJWEAVER.VERSION}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${SPRING.VERSION}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${SPRING.VERSION}</version>
        </dependency>

        <!--spring-mybatis 适配器-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.5</version>
        </dependency>


        <!--mybatis核心依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
        <!--连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
        <!--mysql 对应版本的连接器驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!-- log start -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>


        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- log end -->


        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.10.3</version>
        </dependency>
       
       <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.3</version>
        </dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.10.3</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd"
         version="3.1">


    <!--spring 基于web应用的启动-->
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--全局参数:spring配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-core.xml</param-value>
    </context-param>


    
    <!--前端调度器servlet-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--设置配置文件的路径-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--设置启动即加载-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--编码过滤器filter-->
    <filter>
        <filter-name>characterEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--设置编码格式-->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <!---->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncoding</filter-name>
        <servlet-name>dispatcherServlet</servlet-name>
    </filter-mapping>

    <!--支持rest的过滤器-->
    <filter>
        <filter-name>hiddenHttpMethod</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>hiddenHttpMethod</filter-name>
        <servlet-name>dispatcherServlet</servlet-name>
    </filter-mapping>

</web-app>

springMvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!--扫描controller包-->
   <!-- <context:component-scan base-package="cn.tulingxueyuan.controller">
   </context:component-scan>-->

    <!--use-default-filters设置为false就是指定扫描,例如指定扫描controller-->
    <context:component-scan base-package="cn.tulingxueyuan" use-default-filters="false">
        <context:include-filter type="annotation" 	
        expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

   <!-- 添加<annotation-driver>-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
          id="viewResolver">
        <property name="prefix" value="/WEB-INF/views"></property>
    </bean>

    <!--静态资源解析-->
    <!-- <mvc:resources mapping="" location=""></mvc:resources>-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

</beans>

添加控制器类

在这里插入图片描述

spring-core.xml及Mybatis整合

<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/aop 
        https://www.springframework.org/schema/aop/spring-aop.xsd 
        http://mybatis.org/schema/mybatis-spring
        http://mybatis.org/schema/mybatis-spring.xsd">

    <!--扫描所有除了controller包的其他包-->
    <context:component-scan base-package="cn.tulingxueyuan">
        <context:exclude-filter type="annotation" 
        expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!--引入外部属性资源文件-->
    <context:property-placeholder location="classpath:db.properties">
    </context:property-placeholder>

    <!--创建Druid数据源-->
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="username" value="${mysql.username}"></property>
        <property name="password" value="${mysql.password}"></property>
        <property name="url"  value="${mysql.url}"></property>
        <property name="driverClassName" value="${mysql.driverClassName}"></property>
    </bean>

    <!--声明式事-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 
    id="transactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--基于驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>


    <!--用于声明事务切入的所有方法-->
    <aop:config>
        <aop:pointcut id="transactionCut" expression="execution(* 
        cn.tulingxueyuan.service.impl.*.*(..))"/>
    </aop:config>

    <!--用来明确切点匹配到的方法哪些方法需要使用事务-->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--可以使用通配符-->
            <tx:method name="add*"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
            <tx:method name="get*" read-only="true" propagation="SUPPORTS"></tx:method>
            <tx:method name="query*" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>

    <!--SqlSessionFactory-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <!-- 指定spring中的数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:cn/tulingxueyuan/mapper/*.xml">
        </property>
    </bean>

    <!--将mapper接口交给spring管理-->
    <mybatis:scan base-package="cn.tulingxueyuan.mapper"></mybatis:scan>

</beans>

Mybatis配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--就是DOCTYPE后面对应的根节点-->
<configuration>


    <!--mybatis的设置选项  可以改变mybatis运行时行为-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <!--类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写-->
    <typeAliases>
        <package name="cn.tulingxueyuan.pojo"/>
    </typeAliases>

</configuration>

db.properties

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

生成 pojo\mapper接口\mapper映射文件即可

最后Controller写下

@RestController
public class EmpController {
    @Autowired
    IEmpService empService;

    @RequestMapping("test")
    public  List<Emp> test(){
        List<Emp> emps = empService.selectEmp();
        return emps;
    }
}

启动
http://localhost:8080/ssm/test
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值