从b站学习springcloud项目,现在进行总结,该总结除去了视频中出现的小错误,对有些易错的地方进行了提醒
b站链接:https://www.bilibili.com/video/av55629580?p=1
资料链接:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
提取码: 21ru
上一节链接:
https://blog.csdn.net/qq_40893824/article/details/103628646
下一节链接:
https://blog.csdn.net/qq_40893824/article/details/103643682
下面的内容总结:
订购:
client实体类Admin、Menu、User、Type复制至order,再新建Order→order/repository/OrderRepository→configserver/order-dev.yml添加代码→order/mapping/OrderRepository.xml→order/OrderHandler→order/pom→order/OrderApplication→测试
client实现订购:
client/Order→client/feign/OrderFeign→client/OrderHandler→检查
用户查看订单:
order/repository/2个接口→order/mapping/2个xml文件→OrderRepository.xml修改order/Order添加→order/OrderHandler添加→检查
实现细节:
在index.html有一个订购功能
table.on('tool(test)', function(obj){
var data = obj.data;
if(obj.event === 'order'){
window.location.href="/order/save/"+data.id;
}
});
可以看到订购后要使用order/save功能,其中data.id是order中的id,现在来实现之:
1.在order/southwind创包entity,把client/entity中Admin、Menu、User、Type复制至order/entity中,然后新建实体类Order.java,加入代码:
package com.southwind.entity;
import lombok.Data;
import java.util.Date;
@Data
public class Order {
private Admin admin;
private Menu menu;
private User user;
private Date date;
private int state;
}
2.在order/southwind创包repository,新建接口OrderRepository.java,加入代码:
package com.southwind.repository;
import com.southwind.entity.Order;
import java.util.List;
public interface OrderRepository {
public void save(Order order);
public List<Order> findAll(int index , int limit);
}
3.在configserver/shared中已有order-dev.yml,但是要加一些代码补全,可以把menu-dev.yml除开server.port部分,其他代码复制过来,并改name为order,完整代码:
server:
port: 8010
spring:
application:
name: order
datasource:
name: qwe
url: jdbc:mysql://localhost:3306/qwe?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
mybatis:
mapper-locations: classpath:/mapping/*.xml
type-aliases-package: com.southwind.entity
4.在order/resources中创包mapping,将menu/mapping/MenuRepository.xml复制至order/mapping中,并改名为OrderRepository.xml
修改代码,先把< resultMap>部分删除
保留save代码,删除其他
第3行的MenuRepository改为OrderRepository
save的 parameterType=“Menu” 改为 parameterType=“Order”
下一行的也修改:
这里没有加aid进去
<insert id="save" parameterType="Order">
insert into t_order(uid,mid,aid,date,state) values(#{user.id},#{menu.id},#{date},#{0})
</insert>
现在 OrderRepository.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.southwind.repository.OrderRepository">
<insert id="save" parameterType="Order">
insert into t_order(uid,mid,aid,date,state) values(#{user.id},#{menu.id},#{admin.id}, #{date},0)
</insert>
</mapper>
5.在order/controller/OrderHandler中删除port和index的两部分代码,加入代码:
package com.southwind.controller;
import com.southwind.entity.Order;
import com.southwind.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
@RestController
@RequestMapping("/order")
public class OrderHandler {
@Autowired
private OrderRepository orderRepository;
@PostMapping("/save")
public void save(@RequestBody Order order){
order.setDate(new Date());
orderRepository.save(order);
}
}
6.将account中pom文件依赖覆盖order的pom文件中的原依赖
7.在order/OrderApplication中加入扫描@MapperScan(“com.southwind.repository”)
8.重启configserver 和 启动order/OrderApplication
9.打开postman
进入 post http://localhost:8010/order/save
进入idea -mysql 数据视图的t_order,刷新一下:
添加成功!
在client实现调用order功能,先实现下单功能:
1.将order/entity中的实体类Order复制至client/entity中
2.在client/feign中新建接口OrderFeign,加入代码:
package com.southwind.feign;
import com.southwind.entity.Order;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(value = "order")
public interface OrderFeign {
@PostMapping("/order/save")
public void save(@RequestBody Order order);
}
3.在client/controller新建控制类OrderHandler.java,加入代码:
package com.southwind.controller;
import com.southwind.entity.Menu;
import com.southwind.entity.Order;
import com.southwind.entity.User;
import com.southwind.feign.OrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/order")
public class OrderHandler {
@Autowired
private OrderFeign orderFeign;
@GetMapping("/save/{mid}")
public String save(@PathVariable("mid")int mid, HttpSession session){
User user = (User)session.getAttribute("user");
Order order = new Order();
order.setUser(user);
Menu menu = new Menu();
menu.setId(mid);
order.setMenu(menu);
orderFeign.save(order);
return "index";
}
}
检查,重启client
4.进入http://localhost:8030/login.html,登录shangsan后:
订购最后一个菜
进入t_order查看:
可以看到已经加进去了,即订购成功!下单功能实现!
在实现用户查看订单功能:
1.把menu/repository中2个接口复制至order/repository中
把menu/mapping中2个xml文件复制至order/mapping中
这样的目的是是下一步中resultMap可以调用MenuRepository.xml
2.在order/mapping/OrderRepository.xml中添加代码,
要和t_menu关联,故用resultMap映射
完整代码:
其中association/select路径方法要写全,指定清除要调用的方法!
<?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.southwind.repository.OrderRepository">
<resultMap id="orderMap" type="Order">
<id column="id" property="id"></id>
<result column="date" property="date"></result>
<result column="state" property="state"></result>
<association property="menu" select="com.southwind.repository.MenuRepository.findById" column="mid"></association>
</resultMap>
<insert id="save" parameterType="Order">
insert into t_order(uid,mid,aid,date,state) values(#{user.id},#{menu.id},#{admin.id}, #{date},0)
</insert>
<select id="findAll" resultMap="orderMap">
select id,mid,date,state from t_order limit #{param1},#{param2}
</select>
</mapper>
3.在order/entity的Order加上 private long id;
4.在order/controller的OrderHandler中添加代码:
@GetMapping("/findAll/{index}/{limit}")
public List<Order> findAll(@PathVariable("index")int index, @PathVariable("limit")int limit){
return orderRepository.findAll(index, limit);
}
检查:
5.进入 http://localhost:8010/order/findAll/0/10
成功查询!
上一节链接:
https://blog.csdn.net/qq_40893824/article/details/103628646
下一节链接:
https://blog.csdn.net/qq_40893824/article/details/103643682