美文网首页
Spring MVC 整合 Mybaits

Spring MVC 整合 Mybaits

作者: 林皮皮s | 来源:发表于2018-11-13 10:17 被阅读0次

整合目标 | 控制层采用 spring mvc、持久层使用 mybatis 实现

一、整合需要的jar包

spring(包括spring mvc)包、mybatis包、mybatis-spring整合包、数据库驱动包、第三方连接池包

二、整合思路

  • dao 层

      1. SqlMapConfig.xml,空文件即可,但需要文件头
      1. applicationContext-dao.xml
      • a. 数据库连接池
      • b. SqlSessionFactory对象,需要spring和mybatis整合包下的。
      • c. 配置mapper文件扫描器。
  • service 层

      1. applicationContext-service.xml包扫描器,扫描@service注解的类。
      1. applicationContext-trans.xml配置事务。
  • controller 层

      1. Springmvc.xml
      • a. 包扫描器,扫描@Controller注解的类。
      • b. 配置注解驱动(代替配置处理器映射器和处理器适配器)
      • c. 配置视图解析器
  • web.xml 文件

      1. 配置spring容器随项目启动而启动。
      1. 配置前端控制器。
      1. 配置过滤器,解决post方式提交乱码问题。

三、整合步骤

1、创建动态web工程并导入必需的jar包
2、加入配置文件
创建资源文件夹config,在其下创建mybatis和spring文件夹, 用来存放配置文件

  • SqlMapConfig.xml
    使用逆向工程来生成Mapper相关代码,不需要配置别名。在config/mybatis下创建SqlMapConfig.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>

</configuration>
  • applicationContext-dao.xml
    配置数据源、配置SqlSessionFactory、mapper扫描器。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    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-4.0.xsd http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
       destroy-method="close">
       <property name="driverClassName" value="${jdbc.driver}" />
       <property name="url" value="${jdbc.url}" />
       <property name="username" value="${jdbc.username}" />
       <property name="password" value="${jdbc.password}" />
       <property name="maxActive" value="10" />
       <property name="maxIdle" value="5" />
    </bean>

    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 数据库连接池 -->
       <property name="dataSource" ref="dataSource" />
       <!-- 加载mybatis的全局配置文件 -->
       <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>

    <!-- 配置Mapper扫描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <!-- 配置Mapper扫描包 -->
       <property name="basePackage" value="cn.itcast.ssm.mapper" />
    </bean>
</beans>
  • db.properties
    配置数据库相关信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
  • applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- 配置service扫描 -->
    <context:component-scan base-package="cn.itcast.ssm.service" />
</beans>
  • applicationContext-trans.xml (配置事务)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- 事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <!-- 数据源 -->
       <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
           <!-- 传播行为 -->
           <tx:method name="save*" propagation="REQUIRED" />
           <tx:method name="insert*" propagation="REQUIRED" />
           <tx:method name="delete*" propagation="REQUIRED" />
           <tx:method name="update*" propagation="REQUIRED" />
           <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
           <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
           <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
       </tx:attributes>
    </tx:advice>
    <!-- 切面 -->
    <aop:config>
       <aop:advisor advice-ref="txAdvice"
           pointcut="execution(* cn.itcast.ssm.service.*.*(..))" />
    </aop:config>
</beans>
  • 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: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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- 配置controller扫描包 -->
    <context:component-scan base-package="cn.itcast.ssm.controller" />
    <!-- 注解驱动 -->
    <mvc:annotation-driven />
    <!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" ->
       "/WEB-INF/jsp/test.jsp" -->
    <!-- 配置视图解析器 -->
    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <!-- 配置逻辑视图的前缀 -->
       <property name="prefix" value="/WEB-INF/jsp/" />
       <!-- 配置逻辑视图的后缀 -->
       <property name="suffix" value=".jsp" />
    </bean>
</beans>
  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>springmvc-web</display-name>
    <welcome-file-list>
       <welcome-file>index.html</welcome-file>
       <welcome-file>index.htm</welcome-file>
       <welcome-file>index.jsp</welcome-file>
       <welcome-file>default.html</welcome-file>
       <welcome-file>default.htm</welcome-file>
       <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <!-- 配置spring -->
    <context-param>
       <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext*.xml</param-value>
    </context-param>
    <!-- 使用监听器加载Spring配置文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 配置SrpingMVC的前端控制器 -->
    <servlet>
       <servlet-name>springmvc-web</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:spring/springmvc.xml</param-value>
       </init-param>
    </servlet>
    <servlet-mapping>
       <servlet-name>springmvc-web</servlet-name>
       <!-- 配置所有以action结尾的请求进入SpringMVC -->
       <url-pattern>*.action</url-pattern>
    </servlet-mapping>
</web-app>
  • 导入静态页面与资源
    配置完工程目录如下:
仅供参考
  • demo 示例
    • 需求

      • 实现商品查询列表,从mysql数据库查询商品信息。
    • dao 层开发

      • 使用 mybatis 逆向工程生成代码,参考 逆向工程
    • service 层开发

      • 接口
      public interface ItemService {
          /**
           * 查询商品列表
           * @return
           */
          List<Item> queryItemList();
      }
      
      • 实现类
      @Service
      public class ItemServiceImpl implements ItemService {
          @Autowired
          private ItemMapper itemMapper;
      
          @Override
          public List<Item> queryItemList() {
             // 从数据库查询商品数据
             List<Item> list = this.itemMapper.selectByExample(null);
             return list;
          }
      }
      
    • controller 层开发

      @Controller
      public class ItemController {
          @Autowired
          private ItemService itemService;
      
          /**
           * 显示商品列表
           * @return
           */
          @RequestMapping("/itemList")
          public ModelAndView queryItemList() {
             // 获取商品数据
             List<Item> list = this.itemService.queryItemList();
             ModelAndView modelAndView = new ModelAndView();
             // 把商品数据放到模型中
             modelAndView.addObject("itemList", list);
             // 设置逻辑视图
             modelAndView.setViewName("itemList");
             return modelAndView;
          }
      }
      
    • 測試
      浏览器访问:http://127.0.0.1:8080/springmvc-web/itemList.action

相关文章

  • Spring MVC 整合 Mybaits

    整合目标 | 控制层采用 spring mvc、持久层使用 mybatis 实现 一、整合需要的jar包 spri...

  • Spring整合MyBaits的相关知识

    Spring整合myBaits思路 SqlSessionFactory -> SqlSession -> Mapp...

  • SSM三大框架整合

    框架整合思路 springmvc+mybaits的系统架构 spring将各层进行整合 通过spring管理持久层...

  • SSM三大框架整合

    框架整合思路 springmvc+mybaits的系统架构 spring将各层进行整合 通过spring管理持久层...

  • SSM三大框架整合

    框架整合思路 springmvc+mybaits的系统架构 spring将各层进行整合 通过spring管理持久层...

  • 2019-06-11SSM整合

    框架整合思路 springmvc+mybaits的系统架构 spring将各层进行整合 通过spring管理持久层...

  • SSM整合

    框架整合思路 springmvc+mybaits的系统架构 spring将各层进行整合通过spring管理持久层的...

  • SSM整合

    框架整合思路 springmvc+mybaits的系统架构 spring将各层进行整合通过spring管理持久层的...

  • SSM整合

    框架整合思路 springmvc+mybaits的系统架构 spring将各层进行整合通过spring管理持久层的...

  • Spring和Spring Mvc整合详解

    Spring和Spring Mvc整合详解 官方主页 Spring Spring Mvc 概述 Spring Mv...

网友评论

      本文标题:Spring MVC 整合 Mybaits

      本文链接:https://www.haomeiwen.com/subject/smscfqtx.html