关于通用页面跳转说明

1 业务需求

说明:当用户发起请求时其中有3个请求都是类似的功能.能否利用一个Controller方法实现通用的页面跳转.

				<ul>
	         		<li data-options="attributes:{'url':'/page/item-add'}">新增商品</li>
	         		<li data-options="attributes:{'url':'/page/item-list'}">查询商品</li>
	         		<li data-options="attributes:{'url':'/page/item-param-list'}">规格参数</li>
	         	</ul>

2 实现策略

package com.jt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class IndexController {

	/**
	 * 关于通用页面跳转的说明
	 * url地址:  /page/item-add
	 * url地址:  /page/item-list
	 * url地址:  /page/item-param-list
	 * 按照常规: 1个请求对应的1个controller方法
	 * 需求: 能否利用一个方法实行页面的通用的跳转.
	 * 想法: 能否动态的接收url中的参数呢??
	 *
	 * restFul风格实现1:
	 * 	1.参数与参数之间使用/分隔
	 * 	2.参数使用{}形式包裹
	 * 	3.@PathVariable 实现数据的转化.
	 *
	 * restFul风格实现2:
	 * 	可以利用请求的类型,指定业务功能.
	 *	TYPE="GET"   查询业务
	 * 	TYPE="POST"  新增业务
	 * 	TYPE="PUT"   更新业务
	 * 	TYPE="DELETE" 删除业务
	 *
	 * 	总结1: 如果需要获取url地址中的参数时,则可以使用RestFul风格实现.
	 * 	总结2: 可以按照类型执行特定的功能.
	 */
	//@RequestMapping(value = "/page/{moduleName}",method = RequestMethod.GET)
	@GetMapping("/page/{moduleName}")
	public String itemAdd(@PathVariable String moduleName){

		//目的:跳转页面 item-add
		return moduleName;
	}
}


4 EasyUI中表格数据展现

4.1 表格的入门案例

<table class="easyui-datagrid" style="width:500px;height:300px" data-options="url:'datagrid_data.json',method:'get',fitColumns:true,singleSelect:true,pagination:true"> 
				<thead> 
					<tr> 
						<th data-options="field:'code',width:100">Code</th> 
						<th data-options="field:'name',width:100">Name</th> 
						<th data-options="field:'price',width:100,align:'right'">Price</th>
					</tr> 
				</thead> 
			</table> 

4.2 关于表格数据展现的说明

核心知识点: 如果需要展现UI框架中特定的格式,则返回的数据必须满足其要求.框架才会自动的完成解析.
在这里插入图片描述

5 关于JSON串说明

5.1 JSON介绍

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。

5.2 Object对象格式

在这里插入图片描述

5.3 数组格式

在这里插入图片描述
JSON: “[‘1’,‘2’,‘4’]”

5.4 JSON嵌套格式

在这里插入图片描述

5.5 JSON练习

{"id":"1","name":"tomcat猫","hobby":["吃汉堡","喝果汁","玩游戏","游戏十连胜","看美女",{"身高":"172","腿长":"110","肤色":"白里透红"}]}

5 实现商品列表展现

5.1 封装VO对象

package com.jt.vo;

import com.jt.pojo.Item;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
// 当服务之间字节数组(对象)传递时,必须实现序列化接口.
// json的本质就是字符串可以直接传递.
public class EasyUITable implements Serializable{
    private Integer total;
    private List<Item> rows;

}


5.2 商品列表页面分析

<table class="easyui-datagrid" id="itemList" title="商品列表" 
       data-options="singleSelect:false,fitColumns:true,collapsible:true,pagination:true,url:'/item/query',method:'get',pageSize:20,toolbar:toolbar">
    <thead>
        <tr>
        	<th data-options="field:'ck',checkbox:true"></th>
        	<th data-options="field:'id',width:60">商品ID</th>
            <th data-options="field:'title',width:200">商品标题</th>
            <th data-options="field:'cid',width:100,align:'center',formatter:KindEditorUtil.findItemCatName">叶子类目</th>
            <th data-options="field:'sellPoint',width:100">卖点</th>
            <th data-options="field:'price',width:70,align:'right',formatter:KindEditorUtil.formatPrice">价格</th>
            <th data-options="field:'num',width:70,align:'right'">库存数量</th>
            <th data-options="field:'barcode',width:100">条形码</th>
            <th data-options="field:'status',width:60,align:'center',formatter:KindEditorUtil.formatItemStatus">状态</th>
            <th data-options="field:'created',width:130,align:'center',formatter:KindEditorUtil.formatDateTime">创建日期</th>
            <th data-options="field:'updated',width:130,align:'center',formatter:KindEditorUtil.formatDateTime">更新日期</th>
        </tr>
    </thead>
</table>

5.3 商品分页查询url地址

在这里插入图片描述

5.4 编辑ItemController

说明: 用户发起Ajax请求,之后通过ItemController返回 EasyUITable的JSON串.

package com.jt.controller;

import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jt.service.ItemService;
import org.springframework.web.bind.annotation.RestController;

@RestController	//返回值都是JSON数据
@RequestMapping("/item")
public class ItemController {
	
	@Autowired
	private ItemService itemService;

	/**
	 * 业务需求:商品信息的分页查询.
	 * url地址: http://localhost:8091/item/query?page=1&rows=50
	 * 请求参数: page 页数 , rows 行数
	 * 返回值结果: EasyUITable
	 * 开发顺序: mapper~~service~~~controller~~页面  自下而上的开发
	 * 京淘开发顺序: 分析页面需求~~~~Controller~~~~Service~~~Mapper  自上而下的开发
	 *
	 * */
	@RequestMapping("/query")
	public EasyUITable findItemByPage(Integer page,Integer rows){

		return itemService.findItemByPage(page,rows);
	}
	
	
}


5.5 编辑ItemService

package com.jt.service;

import com.jt.pojo.Item;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService { //eclipse  alt+shift+p
													  //alt +insert
	
	@Autowired
	private ItemMapper itemMapper;

	/**
	 * 自己手写分页的查询实现数据返回
	 * Sql: select * from tb_item limit 起始位置,展现行数;
	 * 查询第一页   20条
	 * Sql: select * from tb_item limit 0,20;   0-19
	 *查询第二页   20条
	 * Sql: select * from tb_item limit 20,20;  20-39
	 * 查询第三页   20条
	 * Sql: select * from tb_item limit 40,20;
	 * @param page
	 * @param rows
	 * @return
	 */
	@Override
	public EasyUITable findItemByPage(Integer page, Integer rows) {
		//1.计算起始位置
		int startIndex = (page-1)*rows;
		List<Item> itemList = itemMapper.findItemByPage(startIndex,rows);

		//2.获取总记录数
		int count = itemMapper.selectCount(null);
		return new EasyUITable(count,itemList);
	}
}


5.5 编辑ItemMapper

package com.jt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.Item;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ItemMapper extends BaseMapper<Item>{

    @Select("select * from tb_item order by updated desc limit #{startIndex},#{rows}")
    List<Item> findItemByPage(int startIndex, Integer rows);
}


6 利用MP机制实现分页

6.1 编辑ItemService

@Service
public class ItemServiceImpl implements ItemService { //eclipse  alt+shift+p
													  //alt +insert
	
	@Autowired
	private ItemMapper itemMapper;

	/**
	 * 在进行分页查询时,MP必须添加配置类
	 * 利用MP机制,实现商品查询
	 * @param page
	 * @param rows
	 * @return
	 */
	@Override
	public EasyUITable findItemByPage(Integer page, Integer rows) {
		//查询条件根据更新时间进行排序.
		QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("updated");
		//当用户将参数传递之后,MP会自己执行分页操作后,将需要的数据进行封装.
		//定义分页对象
		IPage<Item> iPage = new Page<>(page,rows);
		//根据分页对象执行数据库查询,之后获取其其他分页数据.
		iPage = itemMapper.selectPage(iPage,queryWrapper);
		//获取总记录数
		int total = (int)iPage.getTotal();
		//获取分页后的结果
		List<Item> itemList = iPage.getRecords();
		//封装返回值 返回
		return new EasyUITable(total,itemList);
	}
	}

6.2 编辑配置类


package com.jt.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//1.标识配置类  配置类一般与@Bean注解联用!!!
@Configuration
public class MybatisPlusConfig {

    //需要将对象交给容器管理Map集合  map<paginationInterceptor方法名,实例化之后的对象>
    @Bean
    public PaginationInterceptor paginationInterceptor(){

        return new PaginationInterceptor();
    }
}


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Crazy_sounding/article/details/108330865
今日推荐