zoukankan      html  css  js  c++  java
  • 21-spring学习-springMVC实现CRUD

    结合业务层实现一共完成CRUD操作

    1,定义一共IMessageServese接口

    package com.SpringMVC.Service;
    
    import java.util.Map;
    import java.util.Set;
    import com.SpringMVC.vo.Message;
    
    public interface IMessageService {
    
        public boolean insert(Message vo) throws Exception;
        public boolean update(Message vo) throws Exception;
        public boolean delete(Set<Integer> ids) throws Exception;
        public Message get(int id) throws Exception;
        public Map<String,Object> list(String column,String keyword,int currentPage,int lineSize) 
                throws Exception;
    }

    本业务层充分考虑到几乎所有可能出现的情况,而且也要涉及到参数传递问题。

    2,定义这个接口实现类,所有的操作方法都是假实现;

    package com.SpringMVC.Service.Impl;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import org.springframework.stereotype.Service;
    import com.SpringMVC.Service.IMessageService;
    import com.SpringMVC.vo.Message;
    import com.SpringMVC.vo.Type;
    
    @Service
    public class MessageServiceImpl implements IMessageService {
    
        @Override
        public boolean insert(Message vo) throws Exception {
            System.out.println("增加新消息"+vo);
            return true;
        }
     
        @Override
        public boolean update(Message vo) throws Exception {
            System.out.println("修改新消息"+vo);
            return true;
        }
    
        @Override
        public boolean delete(Set<Integer> ids) throws Exception {
            System.out.println("删除新消息"+ids);
            return true;
        }
    
        @Override
        public Message get(int id) throws Exception {
            System.out.println("根据ID查询数据");
            Message ms=new Message();
            ms.setMid(123);
            ms.setTitle("测试查询");
            ms.setPrice(88.00);
            ms.setPubdate(new Date());
            Type type=new Type();
            type.setTitle("教育新闻");
            ms.setType(type);
            return ms;
        }
    
        @Override
        public Map<String, Object> list(String column, String keyword, int currentPage, int lineSize) throws Exception {
    
            System.out.println("分页查询数据");
            Map<String,Object> map=new HashMap<String, Object>();
            List<Message> all=new ArrayList<Message>();
            for(int i=(currentPage-1)*lineSize;i<currentPage*lineSize;i++)
            {
                Message ms=new Message();
                ms.setMid(123+i);
                ms.setTitle("测试查询-"+i);
                ms.setPrice(88.00+i);
                ms.setPubdate(new Date());
                Type type=new Type();
                type.setTitle("教育新闻-"+i);
                ms.setType(type);
                all.add(ms);
            }
            map.put("allMessage", all);
            map.put("messageCount", 888); 
            return map;
        }
    }

    3,既然整个代码都在Spring的控制中,那么可以利用依赖注入的方式在Action里面注入服务层接口。

    4,随后为了更好的模拟,编写一共增加数据的表单。

    范例:定义message_insert.jsp页面。

        <form action="Pages/back/message/message_insert.action" method="post">
        
            消息编号:<input type="text" id="mid" name="mid" value="99"/><br>
            消息标题:<input type="text" id="mid" name="title" value="大家好啊"/><br>
            消息价格:<input type="text" id="mid" name="price" value="9.99"/><br>
            消息日期:<input type="text" id="mid" name="pubdate" value="2018-01-10"/><br>
            消息类型:<input type="text" id="mid" name="type.title" value="标题类型"/><br>
            <input type="submit" value="提交"/>
            <input type="reset" value="重置"/>
        </form>

    未完待续。。。

  • 相关阅读:
    #敏捷个人# 每日成长101:第72天【自我接纳】
    如何从敏捷个人架构图看出时间管理的演变
    #敏捷个人# 每日成长101:第73天【选择】
    2012年度总结:内心宁静的2012
    欢迎关注敏捷个人微信公众帐号
    敏捷个人的创立与详解Scrum会议
    学习敏捷个人卡片V1.0
    #敏捷个人# 每日认识101(11):做自己的医生
    #敏捷个人# 面向对象分析与PPT有什么关系?
    《敏捷个人-认识自我,管理自我 v0.6.pdf》配套PPT打印书籍
  • 原文地址:https://www.cnblogs.com/alsf/p/8232848.html
Copyright © 2011-2022 走看看