springmvc_mybatis1213项目

来源:互联网 发布:松下网络摄像机软件 编辑:程序博客网 时间:2024/06/16 18:05

1.项目结构图



2.POJO类

public class Items {    private Integer id;       // 商品ID      private String name;      // 商品姓名      private Float price;      // 商品价格      private String pic;       // 商品图片      private Date createtime;  // 商品时间      private String detail;    // 商品明细
public class ItemsCustom extends Items {}
import java.util.ArrayList;import java.util.Date;import java.util.List;public class ItemsExample {    protected String orderByClause;    protected boolean distinct;    protected List<Criteria> oredCriteria;
public class ItemsQueryVo {Items items;ItemsCustom itemsCustom;





3.持久层

接口

public interface ItemsMapper {    // 根据id查询    Items selectByPrimaryKey(Integer id);    // 更新    int updateByPrimaryKeyWithBLOBs(Items record);    }
实现
<?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.wyz.ssm.mapper.ItemsMapper" >  <resultMap id="BaseResultMap" type="com.wyz.ssm.po.Items" >    <id column="id" property="id" jdbcType="INTEGER" />    <result column="name" property="name" jdbcType="VARCHAR" />    <result column="price" property="price" jdbcType="REAL" />    <result column="pic" property="pic" jdbcType="VARCHAR" />    <result column="createtime" property="createtime" jdbcType="TIMESTAMP" />  </resultMap>  <resultMap id="ResultMapWithBLOBs" type="com.wyz.ssm.po.Items" extends="BaseResultMap" >    <result column="detail" property="detail" jdbcType="LONGVARCHAR" />  </resultMap>  <sql id="Base_Column_List" >    id, name, price, pic, createtime  </sql>  <sql id="Blob_Column_List" >    detail  </sql>  <!-- 根据id查询 -->  <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >    select     <include refid="Base_Column_List" />    ,    <include refid="Blob_Column_List" />    from items    where id = #{id,jdbcType=INTEGER}  </select>  <!-- 更新 -->  <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.wyz.ssm.po.Items" >    update items    set name = #{name,jdbcType=VARCHAR},      price = #{price,jdbcType=REAL},      pic = #{pic,jdbcType=VARCHAR},      createtime = #{createtime,jdbcType=TIMESTAMP},      detail = #{detail,jdbcType=LONGVARCHAR}    where id = #{id,jdbcType=INTEGER}  </update></mapper>

接口

public interface ItemsMapperCustom {// 商品查询列表public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;}

实现

<?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.wyz.ssm.mapper.ItemsMapperCustom" >   <!-- 定义商品查询的sql片段,就是商品查询条件 -->   <sql id="query_items_where">   <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->   <!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 -->   <if test="itemsCustom!=null">   <if test="itemsCustom.name!=null and itemsCustom.name!=''">   items.name LIKE '%${itemsCustom.name}%'   </if>   </if>   </sql>    <!-- 商品列表查询 -->  <!-- parameterType传入包装对象(包装了查询条件)  resultType建议使用扩展对象   -->  <select id="findItemsList" parameterType="com.wyz.ssm.po.ItemsQueryVo"   resultType="com.wyz.ssm.po.ItemsCustom">  SELECT items.* FROM items    <where>  <include refid="query_items_where"></include>  </where>  </select>  </mapper>


4.业务层

接口

public interface ItemsService {// R.商品查询列表/** *  * <p>Title: findItemsById</p> * <p>Description: </p> * @param 商品查询列表 * @return * @throws Exception */public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;// R.根据id查询商品信息/** *  * <p>Title: findItemsById</p> * <p>Description: </p> * @param id 查询商品的id * @return * @throws Exception */public ItemsCustom findItemsById(Integer id) throws Exception;// U.修改商品信息/** *  * <p>Title: updateItems</p> * <p>Description: </p> * @param id 修改商品的id * @param itemsCustom 修改的商品信息 * @throws Exception */public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception;}


实现

import java.util.List;import org.springframework.beans.BeanUtils;import org.springframework.beans.factory.annotation.Autowired;import com.wyz.ssm.mapper.ItemsMapper;import com.wyz.ssm.mapper.ItemsMapperCustom;import com.wyz.ssm.po.Items;import com.wyz.ssm.po.ItemsCustom;import com.wyz.ssm.po.ItemsQueryVo;import com.wyz.ssm.service.ItemsService;/** *  * @Title ItemsServiceImpl.java   * @description TODO   * @time 2016年9月28日 下午12:03:32   * @author wyz  * @version 1.0  * */public class ItemsServiceImpl implements ItemsService{@Autowiredprivate ItemsMapperCustom itemsMapperCustom;@Autowiredprivate ItemsMapper itemsMapper;// 列表@Overridepublic List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception {//通过ItemsMapperCustom查询数据库return itemsMapperCustom.findItemsList(itemsQueryVo);}// 根据ID查询一条记录@Overridepublic ItemsCustom findItemsById(Integer id) throws Exception {Items items = itemsMapper.selectByPrimaryKey(id);//中间对商品信息进行业务处理//....//返回ItemsCustomItemsCustom itemsCustom = new ItemsCustom();//将items的属性值拷贝到itemsCustomBeanUtils.copyProperties(items, itemsCustom);return itemsCustom;}// 更新@Overridepublic void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {//添加业务校验,通常在service接口对关键参数进行校验//校验 id是否为空,如果为空抛出异常//更新商品信息使用updateByPrimaryKeyWithBLOBs根据id更新items表中所有字段,包括 大文本类型字段//updateByPrimaryKeyWithBLOBs要求必须转入iditemsCustom.setId(id);itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);}}


5.控制器

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 com.wyz.ssm.po.ItemsCustom;import com.wyz.ssm.service.ItemsService;/** *  * @Title ItemsController.java   * @description TODO   * @time 2016年9月28日 上午11:55:01   * @author wyz  * @version 1.0  * */@Controller//为了对url进行分类管理 ,可以在这里定义根路径,最终访问url是根路径+子路径//比如:商品列表:/items/queryItems.action@RequestMapping("/items")public class ItemsController {@Autowiredprivate 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);// 返回ModelAndViewModelAndView modelAndView = new ModelAndView();// 相当 于request的setAttribut,在jsp页面中通过itemsList取数据modelAndView.addObject("itemsList", itemsList);modelAndView.setViewName("items/itemsList");return modelAndView;}// 根据id查询@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {//调用service根据商品id查询商品信息ItemsCustom itemsCustom = itemsService.findItemsById(items_id);//通过形参中的model将model数据传到页面//相当于modelAndView.addObject方法model.addAttribute("itemsCustom", itemsCustom);return "items/editItems";}//商品信息修改提交@RequestMapping("/editItemsSubmit")public String editItemsSubmit(HttpServletRequest request,Integer id, ItemsCustom itemsCustom)throws Exception {//调用service更新商品信息,页面需要将商品信息传到此方法itemsService.updateItems(id, itemsCustom);return "success";}}


6.视图层

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"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>查询商品列表</title></head><body> <form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">查询条件:<table width="100%" border=1><tr><td><input type="submit" value="查询"/></td></tr></table>商品列表:<table width="100%" border=1><tr><td>商品名称</td><td>商品价格</td><td>生产日期</td><td>商品描述</td><td>操作</td></tr><c:forEach items="${itemsList }" var="item"><tr><td>${item.name }</td><td>${item.price }</td><td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td><td>${item.detail }</td><td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td></tr></c:forEach></table></form></body></html>


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"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>修改商品信息</title></head><body> <form id="itemForm" action="${pageContext.request.contextPath }/items/editItemsSubmit.action" method="post" ><input type="hidden" name="id" value="${itemsCustom.id }"/>修改商品信息:<table width="100%" border=1><tr><td>商品名称</td><td><input type="text" name="name" value="${itemsCustom.name }"/></td></tr><tr><td>商品价格</td><td><input type="text" name="price" value="${itemsCustom.price }"/></td></tr><tr><td>商品生产日期</td><td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td></tr><tr><td>商品简介</td><td><textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea></td></tr><tr><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr></table></form></body></html>
success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>成功提示</title></head><body>操作成功!</body></html>



7

8

9

0 0
原创粉丝点击