首页 > 其他分享 >完成全查询商品(ssm框架)

完成全查询商品(ssm框架)

时间:2022-10-31 14:04:20浏览次数:49  
标签:框架 List 查询 ssm public org import com yanlong


目录结构:

完成全查询商品(ssm框架)_xml

   注意:  标记的数字,是本次需要修改的地方,分为(1,2,3,4,5,6)。​​ssm框架的搭建​​,我在上一篇博客有详细的介绍,这里就不详细介绍了。

实现功能:

思路分析

完成全查询商品(ssm框架)_java_02

数据库准备:

CREATE DATABASE yanlong DEFAULT CHARSET='utf8';
USE yanlong;
DROP TABLE IF EXISTS `item`;
CREATE TABLE `item` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL COMMENT '商品名称',
`price` FLOAT(10,1) NOT NULL COMMENT '商品定价',
`detail` VARCHAR(5000) COMMENT '商品描述',
`pic` VARCHAR(64) DEFAULT NULL COMMENT '商品图片',
`createtime` DATETIME NOT NULL COMMENT '生产日期',
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of items
-- ----------------------------
INSERT INTO `item` VALUES ('1', '台式机', '3000.0', '该电脑质量非常好!!!!', NULL, '2016-02-03 13:22:53');
INSERT INTO `item` VALUES ('2', '笔记本', '6000.0', '笔记本性能好,质量好!!!!!', NULL, '2015-02-09 13:22:57');
INSERT INTO `item` VALUES ('3', '背包', '200.0', '名牌背包,容量大质量好!!!!', NULL, '2015-02-06 13:23:02');

完成全查询商品(ssm框架)_xml_03

 1.实体类Item:

package com.yanlong.pojo;

import lombok.Data;

import java.util.Date;

/**
* @Version 1.0
* @Author:cyl
* @Date:2019/7/6 0006
* @Content:商品类
*/
@Data
public class Item {
private int id;
private String name;
private double price;
private Date createtime;
private String detail;

public Item() {
}

public Item(int id, String name, double price, Date createtime, String detail) {
this.id = id;
this.name = name;
this.price = price;
this.createtime = createtime;
this.detail = detail;
}
}

ItemMapper:

package com.yanlong.mapper;

import com.yanlong.pojo.Item;

import java.util.List;

/**
* @Version 1.0
* @Author:cyl
* @Date:2019/7/6 0006
* @Content:
*/
public interface ItemMapper {
//查询所有商品信息
public List<Item> queryItems();
}

ItemMapper.xml:

<?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.yanlong.mapper.ItemMapper">
<select id="queryItems" resultType="item">
select * from item
</select>
</mapper>

ItemService:

package com.yanlong.service;

import com.yanlong.pojo.Item;

import java.util.List;

/**
* @Version 1.0
* @Author:cyl
* @Date:2019/7/6 0006
* @Content:
*/
public interface ItemService {
public List<Item> findItems();
}

ItemServiceImpl:

package com.yanlong.service;

import com.yanlong.mapper.ItemMapper;
import com.yanlong.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Version 1.0
* @Author:cyl
* @Date:2019/7/6 0006
* @Content:
*/
@Service
public class ItemServiceImpl implements ItemService {

@Autowired
private ItemMapper itemMapper;

public List<Item> findItems() {
List<Item> list = itemMapper.queryItems();
return list;
}
}

ItemController:

package com.yanlong.controller;

import com.yanlong.pojo.Item;
import com.yanlong.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
* @Version 1.0
* @Author:cyl
* @Date:2019/7/6 0006
* @Content:
*/
@Controller
public class ItemController {
@Autowired
private ItemService service;

//处理器接收全查询
@RequestMapping("itheima.action")
public ModelAndView itemList(){
//1.ModelAndView对象创建
ModelAndView modelAndView = new ModelAndView();

//2.调用service查询得到list
List<Item> items = service.findItems();

//3.把数据设置到ModelAndView
modelAndView.addObject("itemList",items );

//4.设置ModelAndView的跳转路径
modelAndView.setViewName("itemList");

//5.返回modelAndView
return modelAndView;
}
}

测试:

启动服务:

完成全查询商品(ssm框架)_java_04

输入网址:

 ​​http://localhost:8080/itheima.action​​

完成全查询商品(ssm框架)_spring_05

 

标签:框架,List,查询,ssm,public,org,import,com,yanlong
From: https://blog.51cto.com/u_12277263/5809517

相关文章