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>

未完待续。。。

原文地址:https://www.cnblogs.com/alsf/p/8232848.html