商品表
create TABLE `goods`(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`goods_name` VARCHAR(16) DEFAULT NULL,
`goods_title` VARCHAR(64) DEFAULT NULL,
`goods_img` VARCHAR(64) DEFAULT NULL,
`goods_detail` longtext ,
`goods_price` decimal(10,2) DEFAULT '0.00',
`goods_stock` int(11) DEFAULT '0',
PRIMARY KEY(`id`)
)ENGINE = INNODB AUTO_INCREMENT = 3 DEFAULT CHARSET = utf8mb4;
插入一条数据
INSERT INTO `goods` VALUES(1,'iphoneX','iphone XS 128G 土豪金','','iphone顶配','8888','2000')
秒杀商品表
CREATE TABLE `miaosha_goods`(
`id` bigint(20) NOT NULL AUTO_INCREMENT ,
`goods_id` bigint(20) DEFAULT NULL,
`miaosha_price` decimal(10,2) DEFAULT '0.00',
`stock_count` int(11) DEFAULT NULL,
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
插入数据
INSERT INTO `miaosha_goods` VALUES(1,1,0.01,4,'2020-4-29 22:22:00','2020-4-29 22:32:00'),
(2,2,0.01,9,'2020-5-1 8:30:00','2020-5-1 9:00:00');
订单表
create TABLE `order_info`(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL,
`goods_id` bigint(20) DEFAULT NULL,
`delivery_addr_id` bigint(20) DEFAULT NULL,
`goods_name` varchar(16) DEFAULT NULL,
`goods_count` int(11) DEFAULT '0',
`goods_price` decimal(10,2) DEFAULT '0.00',
`order_channel` tinyint(4) DEFAULT '0',
`status` tinyint(4) DEFAULT '0',
`create_date` datetime DEFAULT NULL,
`pay_date` datetime DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE=INNODB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4;
秒杀订单表
CREATE TABLE `miaosha_order`(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL,
`order_id`bigint(20) DEFAULT NULL,
`goods_id`bigint(20) DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
@Mapper
public interface GoodsDao {
@Select("select g.*,mg.stock_count, mg.start_date, mg.end_date,mg.miaosha_price from miaosha_goods mg left join goods g on mg.goods_id = g.id")
public List<GoodsVo> listGoodsVo();
}
GoodsVo是查询商品信息的返回对象,包含了Goods,MiaoshaGoods两个对象的信息,实现把秒杀表,商品表的数据一起拿到。
@Service
public class GoodsService {
@Autowired
GoodsDao goodsDao;
public List<GoodsVo> listGoodsVo(){
return goodsDao.listGoodsVo();
}
}
@Controller
@RequestMapping("/goods")
public class GoodsController {
@Autowired
MiaoshaUserService miaoshaUserService;
@Autowired
GoodsService goodsService;
@RequestMapping("/to_list")
public String toList(Model model,MiaoshaUser user){
//model的作用是???
/*
* model是一种概念,而不是一种具体的参数或是其他的具体的体现,MVC是软件工程中一种常用的规范的设计模式,model(模型层)-view(视图层)-controller(控制层)
简单来说,模型包括了你的数据模型(pojo或bean之类的东西)和业务模型(比如登陆,注册操作等),而controller层就是将你的model层能在view中表示出来,
假设你写的public String aa(Users user)中调用了一个简单业务是判断传入的user是否为空"return user!=null?user.tostring():new User().tostring",一般这样的逻辑代码
就写在业务层,controller返回的数据格式满足你自己的需求就行,符合了这种规范的就是一种MVC,好处代码好看,重用性好,松耦合等。
*
* model是用来从后台封装数据到页面的,比如页面上展示一个“欢迎你,{userName}",你就可以用model封装一个userName进去,model.addAttribute("useNamer”,张三"" );
* 页面就会显示”欢迎你,张三”
* */
// model.addAttribute("user",user);
List<GoodsVo> goodsList = goodsService.listGoodsVo();
//goods_list.html可以获取goodsList对象,把服务器上的数据传送到了客户端
model.addAttribute("goodsList", goodsList);
return "goods_list";//跳转到goods_list.html页面
}
}
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>商品title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" th:src="@{/js/jquery.min.js}">script>
<link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/bootstrap.min.css}" />
<script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}">script>
<script type="text/javascript" th:src="@{/jquery-validation/jquery.validate.min.js}">script>
<script type="text/javascript" th:src="@{/jquery-validation/localization/messages_zh.min.js}">script>
<script type="text/javascript" th:src="@{/layer/layer.js}">script>
<script type="text/javascript" th:src="@{/js/md5.min.js}">script>
<script type="text/javascript" th:src="@{/js/common.js}">script>
head>
<body>
<div class="panel panel-default">
<div class="panel-heading">秒杀商品列表div>
<table class="table" id="goodslist">
<tr><td>商品名称td><td>商品图片td><td>商品原价td><td>秒杀价td><td>库存数量td><td>详情td>tr>
<tr th:each="goods,goodsStat : ${goodsList}">
<td th:text="${goods.goodsName}">td>
<td ><img th:src="@{${goods.goodsImg}}" width="100" height="100" />td>
<td th:text="${goods.goodsPrice}">td>
<td th:text="${goods.miaoshaPrice}">td>
<td th:text="${goods.stockCount}">td>
<td><a th:href="'/goods/to_detail/'+${goods.id}">详情a>td>
tr>
table>
div>
body>
html>
问题解决了实体类属性于表的字段名不一致造成的,在application.properties中添加
mybatis.configuration.map-underscore-to-camel-case=true
问题解决了。
附SpringBoot+Mybatis注解方式整合教程