Spring Spring MVC和Mybatis 整合分层搭建

Spring Spring MVC和Mybatis 整合分层搭建

环境准备

  • idea
  • jdk 1.8以上版本
  • tomcat 9.0.37
  • maven 3.5.4
  • mysql 8.0

maven jar包引入

 	    <properties>
            <!-- 低版本会报错,主要是和spring的版本配套就好-->
        	<jackson.version>2.9.5</jackson.version>
    	</properties>

		<!--测试-->
		<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>
        <!-- MYSQL 驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

整合Spring MVC层

  • web.xml配置

    <!-- dispatch servlet-->
        <servlet>
            <servlet-name>spring-mvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>spring-mvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>encodingFileter</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>
        </filter>
        <filter-mapping>
            <filter-name>encodingFileter</filter-name>
            <!-- 此处注意页面也utf8要编码-->
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
  • spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 此文件为整合spring mvc 使用的-->
    
        <!-- 添加注解驱动-->
        <mvc:annotation-driven/>
        <!-- 静态资源过滤-->
        <mvc:default-servlet-handler/>
    
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
            <!--前缀-->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <!--后缀-->
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            </mvc:message-converters>
        </mvc:annotation-driven>
    
        <context:component-scan base-package="com.shadow.controller"/>
    </beans>
    

至此,spring-mvc层配置完成

整合Mybatis层

  • 编写Mybatis配置文件mybatis-config.xml

    <?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">
    <configuration>
        <!--1.数据源交给spring-->
        <!--2.起别名-->
        <typeAliases>
            <package name="com.shadow.pojo"></package>
        </typeAliases>
        <mappers>
            <mapper resource="com/shadow/dao/AccountMapper.xml"/>
        </mappers>
    </configuration>
    
    
    
  • 编写spring-dao.xml, 此处是在spring中整合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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 1.关联数据源配置文件-->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        <!-- 2.连接池-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="url" value="${url}"/>
            <property name="driverClassName" value="${driver}"/>
            <property name="username" value="mybatis"/>
            <property name="password" value="${password}"/>
            <property name="initialSize" value="1" />
            <property name="maxActive" value="50" />
            <property name="maxWait" value="30000" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
        </bean>
        <!-- 3.SqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!-- 绑定mybatis配置文件-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
        </bean>
        <!-- 4.配置dao接口扫描,让dao接口可以注入到spring容器中-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
            <!-- 要扫描的包-->
            <property name="basePackage" value="com.shadow.dao"></property>
        </bean>
    
    </beans>
    

至此,spring 整合Mybatis完成!

spring 层整合

  • 编写spring-service.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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 1.扫描service下的包-->
        <context:component-scan base-package="com.shadow.service"/>
        <!-- 2. 业务类注入spring,上面扫描也可以-->
        <!-- 3. 声明式事务-->
        <bean id = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!-- aop 支持-->
    
    </beans>
    

三层整体整合入一个applicationContext.xml

  • 编写applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        <import resource="spring-dao.xml"/>
        <import resource="spring-service.xml"/>
        <import resource="spring-mvc.xml"/>
    </beans>
    
  • mvc 层配置

    注意web.xml 中此处配置

    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    

编写业务

  1. 编写pojio 和dao以及service层

    • pojo层

      package com.shadow.pojo;
      
      public class Account {
      
          private int id;
      
          private String name;
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      }
      
    • dao层

      AccountMapper类
      package com.shadow.dao;
      
      import com.shadow.pojo.Account;
      
      import java.util.List;
      
      public interface AccountMapper {
          List<Account> getAllUser();
      }
      
      AccountMapper.xml内容
      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE mapper
              PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
              "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      <mapper namespace="com.shadow.dao.AccountMapper">
          <select id="getAllUser" resultType="com.shadow.pojo.Account">
            select * from account
          </select>
      </mapper>
      
    • service层

      AccountInterface接口
      package com.shadow.service;
      
      import com.shadow.pojo.Account;
      
      import java.util.List;
      
      public interface AccountInterface {
          public List<Account> getAllUsers();
      }
      
      
      AccountInterface 接口实现类
      package com.shadow.service;
      
      import com.shadow.dao.AccountMapper;
      import com.shadow.pojo.Account;
      import com.shadow.service.AccountInterface;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      import java.util.List;
      
      @Service
      public class AccountInterfaceImp implements AccountInterface {
      
          @Autowired
          private AccountMapper accountMapper;
      
          public List<Account> getAllUsers() {
              List<Account> allUser = accountMapper.getAllUser();
              return allUser;
          }
      }
      
  2. Controller层编写

    package com.shadow.controller;
    
    import com.shadow.pojo.Account;
    import com.shadow.service.AccountInterface;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.List;
    
    @Controller
    public class AccountController {
    
        @Autowired
        private AccountInterface accountInterface;
    
        @RequestMapping("/hello")
        @ResponseBody
        public List<Account> getAllUser(){
            return accountInterface.getAllUsers();
        }
    }
    

测试

在这里插入图片描述

结论

SSM框架中三层整合,网络上面基本上是spring层比较负责,所有的bean都整合在内,导致刚入门学习的同学比较懵。本文把service ,dao, mybatis, mvc 层都写在了不同的xml中,最后整合进applicationContext.xml中,层次比较清晰,大家可以参考!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值