我的架构梦:(十)手写并整合ssm框架(Spring+SpringMVC+MyBatis)

手写并整合ssm框架(Spring+SpringMVC+MyBatis)

    • 一、前言
    • 二、MyBatis整合Spring
    • 三、 整合SpringMVC
    • 四、代码仓库

一、前言

前面我们手写过Spring、SpringMVC、MyBatis框架了,没有看过的小伙伴可以点击下面的链接:

我的架构梦:(一)手写MyBatis框架

我的架构梦:(五)Spring框架中的IoC容器源码分析(上)

我的架构梦:(六)Spring框架中的IoC容器源码分析(下)

我的架构梦:(七)Spring框架中的AOP源码分析

我的架构梦:(八)手写SpringMVC框架

我们今天整合的策略如下:

SSM = Spring + SpringMVC + MyBatis = (Spring + MyBatis) + SpringMVC

先整合 Spring + MyBatis

然后再整合 SpringMVC

基于的需求:查询 Account 表的全部数据显示到前端⻚面

二、MyBatis整合Spring

1、整合目标

  • 数据库连接池以及事务管理都交给Spring容器来完成
  • SqlSessionFactory对象应该放到Spring容器中作为单例对象管理
  • Mapper动态代理对象交给Spring管理,我们从Spring容器中直接获得Mapper的代理对象

2、整合所需 Jar 分析

  • Junit测试jar(4.12版本)
  • Mybatis的jar(3.4.5)
  • Spring相关jar(spring-context、spring-test、spring-jdbc、spring-tx、spring-aop、 aspectjweaver)
  • Mybatis/Spring整合包jar(mybatis-spring-xx.jar)
  • Mysql数据库驱动jar
  • Druid数据库连接池的jar

3、整合的pom文件


<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.12version>
    <scope>testscope>
dependency>

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.4.5version>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>5.1.12.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-testartifactId>
    <version>5.1.12.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>5.1.12.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-txartifactId>
    <version>5.1.12.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-aopartifactId>
    <version>5.1.12.RELEASEversion>
dependency>
<dependency>
    <groupId>org.aspectjgroupId>
    <artifactId>aspectjweaverartifactId>
    <version>1.8.9version>
dependency>

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatis-springartifactId>
    <version>2.0.3version>
dependency>

<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.19version>
dependency>

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.1.21version>
dependency>
<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <version>1.18.12version>
    <scope>providedscope>
dependency>

4、jdbc.properties

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

5、Spring 配置文件 applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">

    
    <context:component-scan base-package="com.riemann.service"/>

    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>

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

    
    <tx:annotation-driven transaction-manager="transactionManager" />

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="typeAliasesPackage" value="com.riemann.pojo"/>
        
        <property name="dataSource" ref="dataSource"/>
    bean>

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

beans>

6、AccountMapper接口

public interface AccountMapper {

    // 定义dao层接口方法
    List<Account> queryAccountList() throws Exception;

}

7、AccountMapper.xml



<mapper namespace="com.riemann.mapper.AccountMapper">

    <select id="queryAccountList" resultType="com.riemann.pojo.Account">
        select * from account;
    select>

mapper>

8、测试程序

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:application*.xml"}) // 不用手动去启动一个容器了
public class MybatisSpringTest {

    // 希望测试ioc容器中的哪个对象你注入即可。
    @Autowired
    private AccountService accountService;

    @Test
    public void testMybatisSpring() throws Exception{
        List<Account> accounts = accountService.queryAccountList();
        System.out.println(accounts);
    }

}

我的架构梦:(十)手写并整合ssm框架(Spring+SpringMVC+MyBatis)_第1张图片

查询的信息成功,这样一来,也就说明我们MyBatis整合Spring成功了,接下来在此基础上整合SpringMVC

三、 整合SpringMVC

1、整合思路

SpringMVC的入⻔案例整合进来即可(在已有工程基础之上开发一个SpringMVC入⻔案例)

2、pom.xml依赖


<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>5.1.12.RELEASEversion>
dependency>

<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>jsp-apiartifactId>
    <version>2.0version>
    <scope>providedscope>
dependency>
<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>javax.servlet-apiartifactId>
    <version>3.1.0version>
    <scope>providedscope>
dependency>

<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-coreartifactId>
    <version>2.9.0version>
dependency>
<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
    <version>2.9.0version>
dependency>
<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-annotationsartifactId>
    <version>2.9.0version>
dependency>

3、springmvc.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
">

    
    <context:component-scan base-package="com.riemann.controller"/>

    
    <mvc:annotation-driven/>

beans>

4、Controller类

@Controller
@RequestMapping("/account")
public class AccountController {

    /**
     * Spring容器和SpringMVC容器是有层次的(父子容器)
     * Spring容器:service对象+dao对象
     * SpringMVC容器:controller对象,可以引用到Spring容器中的对象。
     */

    @Autowired
    private AccountService accountService;

    @RequestMapping("/queryAll")
    @ResponseBody
    public List<Account> queryAll() throws Exception {
        return accountService.queryAccountList();
    }

}

5、web.xml



<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>

  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath*:applicationContext.xmlparam-value>
  context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>

  
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath*:springmvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>

  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

6、测试

我的架构梦:(十)手写并整合ssm框架(Spring+SpringMVC+MyBatis)_第2张图片
插件启动成功后,访问 http://localhost:8888/account/queryAll

我的架构梦:(十)手写并整合ssm框架(Spring+SpringMVC+MyBatis)_第3张图片

前端页面显示出相应的数据,这样一来我们手写并整合ssm成功!!!

四、代码仓库

https://github.com/riemannChow/perseverance/tree/master/handwriting-framework/ssm

你可能感兴趣的:(我的架构梦)