spring 整合mybatis 学习笔记

1.1 环境准备

java环境:

  • jdk1.7.0_72
  • eclipse indigo
  •  springmvc版本:spring3.2

 所需要的jar包:

  • 数据库驱动包:mysql5.1
  • mybatisjar
  • mybatisspring整合包
  • log4j
  • dbcp数据库连接池包
  • spring3.2所有jar
  • jstl

 2018-02遇到的坑:在html中,表单命名应该是:< form  name="itemForm".....>结果,我错写成了<form id="itemsForm".....>然后,balabala两个小时过去了。知识不牢固造成的。希望以后会牢记此坑(<form name="itemsForm"....>

遇到的标记下:
package cn.itcast.ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.itcast.ssm.po.ItemsCustom;
import cn.itcast.ssm.service.ItemsService;

/**
遇到的坑标记下:如果在类的前面,使用@RequestMapping注解,指定了新的前缀,如:@RequestMapping("/items"),那么在页面访问页面时一定要加上去,如本例就是:localhost:8080/spring2018/items/queryItems.action
因为是直接拷的代码,没有详细看注释说明,在测试时,还一直按原地址:localhost:8080/spring2018/queryItems.action 访问,一直出错,原因找来找去整整折腾了10天,从放假前就找原因,一直找到春节初六上班,才明白;
 */
@Controller
//为了对url进行分类管理 ,可以在这里定义根路径,最终访问url是根路径+子路径
//比如:商品列表:/items/queryItems.action
@RequestMapping("/items")
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    // 商品查询
    @RequestMapping("/queryItems")
    public ModelAndView queryItems(HttpServletRequest request) throws Exception {
        //测试forward后request是否可以共享
        
        System.out.println(request.getParameter("id"));

        // 调用service查找 数据库,查询商品列表
        List<ItemsCustom> itemsList = itemsService.findItemsList(null);
        
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        // 相当 于request的setAttribut,在jsp页面中通过itemsList取数据
        modelAndView.addObject("itemsList", itemsList);

        // 指定视图
        // 下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为
        // modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        // 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀
        modelAndView.setViewName("items/itemsList");

        return modelAndView;

    }




}

原文地址:https://www.cnblogs.com/lrzy/p/8430254.html