SpringMVC,mybatis,spring整合

需求:使用SpringMVC和mybatis完成商品列表查询

一 整合思路

SSM框架架构图
1 相关jar包(maven)

    
    
      org.springframework
      spring-core
      4.1.7.RELEASE
    
    
      org.springframework
      spring-beans
      4.1.7.RELEASE
    
    
      org.springframework
      spring-context
      4.1.7.RELEASE
    
    
    
      taglibs
      standard
      1.1.2
    
    
      jstl
      jstl
      1.2
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
    

    
    
      org.springframework
      spring-jdbc
      4.1.7.RELEASE
    
    
      org.springframework
      spring-tx
      4.1.7.RELEASE
    
    
    
      org.springframework
      spring-web
      4.1.7.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.1.7.RELEASE
    
    
    
      org.springframework
      spring-test
      4.1.7.RELEASE
    

    
    
      mysql
      mysql-connector-java
      5.1.21
    
    
      c3p0
      c3p0
      0.9.1.2
    

    
    
    
      org.mybatis
      mybatis
      3.3.0
    
    
      org.mybatis
      mybatis-spring
      1.2.3
    


    
      log4j
      log4j
      1.2.17
    

    
      org.slf4j
      slf4j-api
      1.7.18
    

    
      commons-dbcp
      commons-dbcp
      1.4
    
    
      org.aspectj
      aspectjweaver
      1.8.9
    
    
    
      org.apache.commons
      commons-lang3
      3.4
    
    
      commons-fileupload
      commons-fileupload
      1.3.1
    
    
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
    
    
      javax.servlet.jsp
      jsp-api
      2.2
    
    
      javax.servlet
      jstl
      1.2
    
    
      taglibs
      standard
      1.1.2
    

    
    
      aopalliance
      aopalliance
      1.0
    
    
    
      org.springframework
      spring-aspects
      4.2.4.RELEASE
    

    
    
      org.mybatis.generator
      mybatis-generator-core
      1.3.2
    
    
    
      org.hibernate
      hibernate-validator
      5.2.4.Final
    
    
    
      commons-fileupload
      commons-fileupload
      1.3.1
    
    
      org.apache.poi
      poi
      3.16
    
    
      junit
      junit
      RELEASE
    

    
    
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.7.2
    
    
      org.codehaus.jackson
      jackson-mapper-asl
      1.9.13
    
2 整合dao层

mybatis和springmvc整合,通过spring管理mapper接口
使用mapper的扫描器自动扫描mapper接口在spring中注册
参照: 整合spring和mybatis

applicationContext-dao.xml



    
    
    

    
    
        
        
        
        
        

        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root

sqlMapConfig.xml




    


3 整合service层

通过spring管理servic接口
使用配置方式将service接口配置在spring配置文件中,实现事物控制

applicationContext-service.xml




    
    

4 整合springmvc

由于springmvc是spring的模块,不需要整合

springmvc.xml



    
    

    
    
    
    
        
        
        
        
    

web.xml



  Archetype Created Web Application
  
 
    contextConfigLocation
    classpath:spring/applicationContext-*.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
      
      springmvc
      org.springframework.web.servlet.DispatcherServlet
      
          
          contextConfigLocation
          classpath:spring/springmvc.xml
      
  
    
    
        springmvc
        *.action
    


5 事务管理

applicationContext-transaction.xml




    
    
        
        
    
    
    

6 其他相关配置

log4j.properties

log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

二 需求实现

1 dao层实现

使用mapper动态代理,mapper文件和mapper.xml文件名必须相同

Interface ItemsMapperCustom.java

package com.dao;

import com.entity.ItemsCustom;
import com.entity.ItemsQueryVo;

import java.util.List;

/**
 * @author:Yang
 * @date:2018/4/24
 */

public interface ItemsMapperCustom {
    /**
     *
     * @param itemsQueryVo
     * @return
     * @throws Exception
     */
    List findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}

ItemsMapperCustom.xml




    
        
            
               items.name like %${itemsCustom.name}%;
            
        
    

    

2 service层实现

Interface ItemsService.java

package com.service;

import com.entity.ItemsCustom;
import com.entity.ItemsQueryVo;

import java.util.List;

/**
 * @author:Yang
 * @date:2018/4/24
 */
public interface ItemsService {
    /**
     *
     * @param itemsQueryVo
     * @return
     * @throws Exception
     */
    List findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}

ItemsServiceImpl.java

package com.service.impl;

import com.dao.ItemsMapperCustom;
import com.entity.Items;
import com.entity.ItemsCustom;
import com.entity.ItemsQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.service.ItemsService;

import java.util.List;

/**
 * @author:Yang
 * @date:2018/4/24
 */
@Service
public class ItemsServiceImpl implements ItemsService {

    @Autowired
    ItemsMapperCustom itemsMapperCustom;

    @Override
    public List findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
        return itemsMapperCustom.findItemsList(itemsQueryVo);
    }
}

3 controller层实现

package com.controller;

import com.entity.Items;
import com.entity.ItemsCustom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.service.ItemsService;

import java.util.List;

/**
 * @author:Yang
 * @date:2018/4/24
 */
@Controller
public class ItemsController {
    @Autowired
    private ItemsService itemsService;
   @RequestMapping("/queryItems")
   public ModelAndView queryItems() throws Exception{
        List list = itemsService.findItemsList(null);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("items",list);
        modelAndView.setViewName("index");
        return modelAndView;
    }

}

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Y
  Date: 2018/4/6
  Time: 19:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@page isELIgnored="false" %>


    
    查询商品列表


查询条件:
商品列表:
商品名称 商品价格 生产日期 商品描述 操作
${item.name } ${item.price } ${item.detail } 修改
运行结果

三 遇到问题

1 在配置监听器时,导入文件错误,应该倒入

正确导入

自己错误导入成org.springframework.web.context.ContextCleanupListener
2 在springmvc配置文件中,可以使用代替映射器和适配器配置,自己在配置过程中导入错误的头文件,导致配置无法识别
3 jdk版本不要过高

你可能感兴趣的:(SpringMVC,mybatis,spring整合)