配置文件
web.xml
添加spring容器监听器,加载spring容器
springmvc_mybatis01
contextConfigLocation
/WEB-INF/classes/spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
springmvc
*.action
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncodingFilter
/*
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
```
db.properties
log4j.properties
sqlMapConfig.xml
配置:别名
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
```
applicationContext-dao.xml
配置:数据源、SqlSessionFactory、mapper扫描器
```
applicationContext-service.xml
让spring管理service接口
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
```
applicationContext-transaction.xml
```
springmvc.xml
配置处理器映射器、适配器、视图解析器
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
```
Mapper接口和配置文件
po类由逆向工程生成
ItemsCustomMapper.xml
items.name like '%${itemsCustom.name}%'
```
ItemsCustomMapper.java
package cn.ztc.ssm.mapper;
import java.util.List;
import cn.ztc.ssm.po.*;
public interface ItemsCustomMapper {
public List findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}
ItemsService.java
package cn.ztc.ssm.service;
import java.util.List;
import cn.ztc.ssm.po.ItemsCustom;
import cn.ztc.ssm.po.ItemsQueryVo;
public interface ItemsService {
//商品查询列表
public List
//根据id查询商品信息
public ItemsCustom findItemsById(Integer id)throws Exception;
//修改商品信息
public void updateItems(Integer id, ItemsCustom itemsCustom)throws Exception;
}
ItemsServiceImpl.java
package cn.ztc.ssm.service.impl;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ztc.ssm.mapper.;
import cn.ztc.ssm.po.;
import cn.ztc.ssm.service.ItemsService;
public class ItemsServiceImpl implements ItemsService{
@Autowired
private ItemsCustomMapper itemsCustomMapper;
@Autowired
private ItemsMapper itemsMapper;
@Override
public List findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
// TODO Auto-generated method stub
return itemsCustomMapper.findItemsList(itemsQueryVo);
}
@Override
public ItemsCustom findItemsById(Integer id) throws Exception {
Items items = itemsMapper.selectByPrimaryKey(id);
ItemsCustom itemsCustom = new ItemsCustom();
BeanUtils.copyProperties(items, itemsCustom);
return itemsCustom;
}
@Override
public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
//添加校验
//更新商品信息,可以更新表中的所有字段,包括大文本类型
itemsCustom.setId(id);
itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
}
}
ItemsController.java
package cn.ztc.ssm.controller;
import java.util.ArrayList;
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.ztc.ssm.po.*;
import cn.ztc.ssm.service.ItemsService;
@Controller
@RequestMapping("/items")
public class ItemsController {
@Autowired
private ItemsService itemsService;
@RequestMapping("/queryItems")
public ModelAndView queryItems(HttpServletRequest request) throws Exception{
//调用service查找数据库,查询商品列表,这里使用静态数据模拟
List itemsList = itemsService.findItemsList(null);
//测试forward后request是否能共享
System.out.println(request.getParameter("id"));
ModelAndView modelAndView = new ModelAndView();
//在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList",itemsList);
//指定视图
modelAndView.setViewName("items/itemsList");
return modelAndView;
}
//商品信息修改页面
// @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
// public ModelAndView editItems() throws Exception{
// ItemsCustom itemsCustom = itemsService.findItemsById(1);
//
// ModelAndView modelAndView = new ModelAndView();
//
// modelAndView.addObject("itemsCustom", itemsCustom);
//
// modelAndView.setViewName("items/editItems");
//
// return modelAndView;
// }
//返回逻辑视图名
//jsp全路径:前缀+逻辑视图名+后缀
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
//@RequestParam里面指定request传入参数名称和形参绑定
public String editItems(Model model,@RequestParam(value="id",required=true,defaultValue="1")Integer items_id) throws Exception{
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
model.addAttribute("itemsCustom",itemsCustom);
return "items/editItems";
}
//商品信息修改提交
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom) throws Exception{
itemsService.updateItems(id, itemsCustom);
return "success";
//重定向到商品的查询列表,request不能共享
//return "redirect:queryItems.action";
//页面转发,request能共享
// return "forward:queryItems.action";
}
}
CustomDateConverter.java
package cn.ztc.ssm.controller.converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
public class CustomDateConverter implements Converter
@Override
public Date convert(String s) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return simpleDateFormat.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
页面
itemsList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
```
editItems.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
修改商品信息
```
spring参数绑定过程
从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上。springmvc中,接收页面提交的数据是通过方法形参来接收。
1.默认支持的类型:直接在controller方法形参上定义下边类型的对象,就可以使用这些对象。在参数绑定过程中,如果遇到下边类型直接进行绑定。
HttpServletRequest
通过request对象获取请求信息
HttpServletResponse
通过response处理响应信息
HttpSession
通过session对象得到session中存放的对象
Model/ModelMap
model是一个接口,modelMap是一个接口实现 。
作用:将model数据填充到request域。
2.pojo绑定
页面中input的name和controller的pojo形参中的属性名称一致,将页面中数据绑定到pojo。
3.自定义参数绑定实现日期类型绑定
对于controller形参中pojo对象,如果属性中有日期类型,需要自定义参数绑定。
将请求日期数据串传成 日期类型,要转换的日期类型和pojo中日期属性的类型保持一致。