3). DishService中增加方法saveWithFlavor
<span style="background-color:#f8f8f8"><span style="color:#333333">//新增菜品,同时插入菜品对应的口味数据,需要操作两张表:dish、dish_flavor public void saveWithFlavor(DishDto dishDto);</span></span>
4). DishServiceImpl中实现方法saveWithFlavor
页面传递的菜品口味信息,仅仅包含name 和 value属性,缺少一个非常重要的属性dishId, 所以在保存完菜品的基本信息后,我们需要获取到菜品ID,然后为菜品口味对象属性dishId赋值。
具体逻辑如下:
①. 保存菜品基本信息 ;
②. 获取保存的菜品ID ;
③. 获取菜品口味列表,遍历列表,为菜品口味对象属性dishId赋值;
④. 批量保存菜品口味列表;
代码实现如下:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#555555">@Autowired</span> <span style="color:#770088">private</span> <span style="color:#000000">DishFlavorService</span> <span style="color:#000000">dishFlavorService</span>; <span style="color:#aa5500">/**</span> <span style="color:#aa5500">* 新增菜品,同时保存对应的口味数据</span> <span style="color:#aa5500">* @param dishDto</span> <span style="color:#aa5500">*/</span> <span style="color:#555555">@Transactional</span> <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">saveWithFlavor</span>(<span style="color:#000000">DishDto</span> <span style="color:#000000">dishDto</span>) { <span style="color:#aa5500">//保存菜品的基本信息到菜品表dish</span> <span style="color:#770088">this</span>.<span style="color:#000000">save</span>(<span style="color:#000000">dishDto</span>); <span style="color:#008855">Long</span> <span style="color:#000000">dishId</span> <span style="color:#981a1a">=</span> <span style="color:#000000">dishDto</span>.<span style="color:#000000">getId</span>();<span style="color:#aa5500">//菜品id</span> <span style="color:#aa5500">//菜品口味</span> <span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#000000">DishFlavor</span><span style="color:#981a1a">></span> <span style="color:#000000">flavors</span> <span style="color:#981a1a">=</span> <span style="color:#000000">dishDto</span>.<span style="color:#000000">getFlavors</span>(); <span style="color:#000000">flavors</span> <span style="color:#981a1a">=</span> <span style="color:#000000">flavors</span>.<span style="color:#000000">stream</span>().<span style="color:#000000">map</span>((<span style="color:#000000">item</span>) <span style="color:#981a1a">-></span> { <span style="color:#000000">item</span>.<span style="color:#000000">setDishId</span>(<span style="color:#000000">dishId</span>); <span style="color:#770088">return</span> <span style="color:#000000">item</span>; }).<span style="color:#000000">collect</span>(<span style="color:#000000">Collectors</span>.<span style="color:#000000">toList</span>()); <span style="color:#aa5500">//保存菜品口味数据到菜品口味表dish_flavor</span> <span style="color:#000000">dishFlavorService</span>.<span style="color:#000000">saveBatch</span>(<span style="color:#000000">flavors</span>); }</span></span>
说明:
由于在 saveWithFlavor 方法中,进行了两次数据库的保存操作,操作了两张表,那么为了保证数据的一致性,我们需要在方法上加上注解 @Transactional来控制事务。
5). 在引导类上加注解 @EnableTransactionManagement
Service层方法上加的注解@Transactional要想生效,需要在引导类上加上注解 @EnableTransactionManagement, 开启对事务的支持。
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#555555">@Slf4j</span> <span style="color:#555555">@SpringBootApplication</span> <span style="color:#555555">@ServletComponentScan</span> <span style="color:#555555">@EnableTransactionManagement</span> <span style="color:#aa5500">//开启对事物管理的支持</span> <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">ReggieApplication</span> { <span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) { <span style="color:#000000">SpringApplication</span>.<span style="color:#000000">run</span>(<span style="color:#000000">ReggieApplication</span>.<span style="color:#770088">class</span>,<span style="color:#000000">args</span>); <span style="color:#000000">log</span>.<span style="color:#000000">info</span>(<span style="color:#aa1111">"项目启动成功..."</span>); } }</span></span>
2.6 功能测试
代码编写完毕之后,我们重新启动服务,访问项目,然后登陆到系统中,进行菜品的新增测试,在测试时,我们可以通过debug断点跟踪的形式,查看我们传输的数据,及数据的封装。
debug跟踪数据的封装情况:
3. 菜品分页查询
3.1 需求分析
系统中的菜品数据很多的时候,如果在一个页面中全部展示出来会显得比较乱,不便于查看,所以一般的系统中都会以分页的方式来展示列表数据。
在菜品列表展示时,除了菜品的基本信息(名称、售价、售卖状态、更新时间)外,还有两个字段略微特殊,第一个是图片字段 ,我们从数据库查询出来的仅仅是图片的名字,图片要想在表格中回显展示出来,就需要下载这个图片。第二个是菜品分类,这里展示的是分类名称,而不是分类ID,此时我们就需要根据菜品的分类ID,去分类表中查询分类信息,然后在页面展示。
开发菜品信息分页查询功能,其实就是在服务端编写代码去处理前端页面发送的这2次请求即可,而前端发起的请求中, 图片文件下载回显的代码,我们已经实现了。所以,我们只需要开发分页查询的功能即可,分页查询具体的请求信息如下:
请求 | 说明 |
请求方式 | GET |
请求路径 | /dish/page |
请求参数 | ?page=1&pageSize=10&name=xxx |
3.3 代码实现
3.3.1 分析
上述我们已经分析了分页查询的请求信息,那么接下来,我们就需要在 DishController中开发方法,来完成菜品的条件分页查询,在分页查询时还需要给页面返回分类的名称,而分类的名称前端在接收的时候是通过 categoryName 属性获取的,那么对应的服务端也应该封装到 categoryName 属性中。
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#117700"><</span><span style="color:#117700">el-table-column</span> <span style="color:#0000cc">prop</span>=<span style="color:#aa1111">"categoryName"</span> <span style="color:#0000cc">label</span>=<span style="color:#aa1111">"菜品分类"</span><span style="color:#117700">></</span><span style="color:#117700">el-table-column</span><span style="color:#117700">></span></span></span>
而在我们的实体类 Dish 中,仅仅包含 categoryId, 不包含 categoryName,那么我们应该如何封装查询的数据呢? 其实,这里我们可以返回DishDto对象,在该对象中我们可以拓展一个属性 categoryName,来封装菜品分类名称。
1.<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#555555">@Data</span> <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">DishDto</span> <span style="color:#770088">extends</span> <span style="color:#000000">Dish</span> { <span style="color:#770088">private</span> <span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#000000">DishFlavor</span><span style="color:#981a1a">></span> <span style="color:#000000">flavors</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">ArrayList</span><span style="color:#981a1a"><></span>(); <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">categoryName</span>; <span style="color:#aa5500">//菜品分类名称</span> <span style="color:#770088">private</span> <span style="color:#008855">Integer</span> <span style="color:#000000">copies</span>; }</span></span>
具体逻辑为:
1). 构造分页条件对象
2). 构建查询及排序条件
3). 执行分页条件查询
4). 遍历分页查询列表数据,根据分类ID查询分类信息,从而获取该菜品的分类名称
5). 封装数据并返回
3.3.2 实现
分析了具体的实现思路之后,接下来就来完成具体的代码实现。
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/**</span> <span style="color:#aa5500">* 菜品信息分页查询</span> <span style="color:#aa5500">* @param page</span> <span style="color:#aa5500">* @param pageSize</span> <span style="color:#aa5500">* @param name</span> <span style="color:#aa5500">* @return</span> <span style="color:#aa5500">*/</span> <span style="color:#555555">@GetMapping</span>(<span style="color:#aa1111">"/page"</span>) <span style="color:#770088">public</span> <span style="color:#000000">R</span><span style="color:#981a1a"><</span><span style="color:#000000">Page</span><span style="color:#981a1a">></span> <span style="color:#0000ff">page</span>(<span style="color:#008855">int</span> <span style="color:#000000">page</span>,<span style="color:#008855">int</span> <span style="color:#000000">pageSize</span>,<span style="color:#008855">String</span> <span style="color:#000000">name</span>){ <span style="color:#aa5500">//构造分页构造器对象</span> <span style="color:#000000">Page</span><span style="color:#981a1a"><</span><span style="color:#000000">Dish</span><span style="color:#981a1a">></span> <span style="color:#000000">pageInfo</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">Page</span><span style="color:#981a1a"><></span>(<span style="color:#000000">page</span>,<span style="color:#000000">pageSize</span>); <span style="color:#000000">Page</span><span style="color:#981a1a"><</span><span style="color:#000000">DishDto</span><span style="color:#981a1a">></span> <span style="color:#000000">dishDtoPage</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">Page</span><span style="color:#981a1a"><></span>(); <span style="color:#aa5500">//条件构造器</span> <span style="color:#000000">LambdaQueryWrapper</span><span style="color:#981a1a"><</span><span style="color:#000000">Dish</span><span style="color:#981a1a">></span> <span style="color:#000000">queryWrapper</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">LambdaQueryWrapper</span><span style="color:#981a1a"><></span>(); <span style="color:#aa5500">//添加过滤条件</span> <span style="color:#000000">queryWrapper</span>.<span style="color:#000000">like</span>(<span style="color:#000000">name</span> <span style="color:#981a1a">!=</span> <span style="color:#221199">null</span>,<span style="color:#000000">Dish</span>::<span style="color:#000000">getName</span>,<span style="color:#000000">name</span>); <span style="color:#aa5500">//添加排序条件</span> <span style="color:#000000">queryWrapper</span>.<span style="color:#000000">orderByDesc</span>(<span style="color:#000000">Dish</span>::<span style="color:#000000">getUpdateTime</span>); <span style="color:#aa5500">//执行分页查询</span> <span style="color:#000000">dishService</span>.<span style="color:#000000">page</span>(<span style="color:#000000">pageInfo</span>,<span style="color:#000000">queryWrapper</span>); <span style="color:#aa5500">//对象拷贝</span> <span style="color:#000000">BeanUtils</span>.<span style="color:#000000">copyProperties</span>(<span style="color:#000000">pageInfo</span>,<span style="color:#000000">dishDtoPage</span>,<span style="color:#aa1111">"records"</span>); <span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#000000">Dish</span><span style="color:#981a1a">></span> <span style="color:#000000">records</span> <span style="color:#981a1a">=</span> <span style="color:#000000">pageInfo</span>.<span style="color:#000000">getRecords</span>(); <span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#000000">DishDto</span><span style="color:#981a1a">></span> <span style="color:#000000">list</span> <span style="color:#981a1a">=</span> <span style="color:#000000">records</span>.<span style="color:#000000">stream</span>().<span style="color:#000000">map</span>((<span style="color:#000000">item</span>) <span style="color:#981a1a">-></span> { <span style="color:#000000">DishDto</span> <span style="color:#000000">dishDto</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">DishDto</span>(); <span style="color:#000000">BeanUtils</span>.<span style="color:#000000">copyProperties</span>(<span style="color:#000000">item</span>,<span style="color:#000000">dishDto</span>); <span style="color:#008855">Long</span> <span style="color:#000000">categoryId</span> <span style="color:#981a1a">=</span> <span style="color:#000000">item</span>.<span style="color:#000000">getCategoryId</span>();<span style="color:#aa5500">//分类id</span> <span style="color:#aa5500">//根据id查询分类对象</span> <span style="color:#000000">Category</span> <span style="color:#000000">category</span> <span style="color:#981a1a">=</span> <span style="color:#000000">categoryService</span>.<span style="color:#000000">getById</span>(<span style="color:#000000">categoryId</span>); <span style="color:#770088">if</span>(<span style="color:#000000">category</span> <span style="color:#981a1a">!=</span> <span style="color:#221199">null</span>){ <span style="color:#008855">String</span> <span style="color:#000000">categoryName</span> <span style="color:#981a1a">=</span> <span style="color:#000000">category</span>.<span style="color:#000000">getName</span>(); <span style="color:#000000">dishDto</span>.<span style="color:#000000">setCategoryName</span>(<span style="color:#000000">categoryName</span>); } <span style="color:#770088">return</span> <span style="color:#000000">dishDto</span>; }).<span style="color:#000000">collect</span>(<span style="color:#000000">Collectors</span>.<span style="color:#000000">toList</span>()); <span style="color:#000000">dishDtoPage</span>.<span style="color:#000000">setRecords</span>(<span style="color:#000000">list</span>); <span style="color:#770088">return</span> <span style="color:#000000">R</span>.<span style="color:#000000">success</span>(<span style="color:#000000">dishDtoPage</span>); }</span></span>
数据库查询菜品信息时,获取到的分页查询结果 Page 的泛型为 Dish,而我们最终需要给前端页面返回的类型为 DishDto,所以这个时候就要进行转换,基本属性我们可以直接通过属性拷贝的形式对Page中的属性进行复制,而对于结果列表 records属性,我们是需要进行特殊处理的(需要封装菜品分类名称);
3.4 功能测试
代码编写完毕之后,我们重新启动服务,访问项目,可以通过debug端点的形式跟踪,整个分页查询过程中,数据的转换和封装。
在测试的过程中,我们发现有一些菜品图片并没有展示出来,如下:
这是因为我们在导入sql脚本时,导入了一部分菜品的数据,而菜品的图片在我们的磁盘目录中并不存在,所以才会出现图片展示不出来的情况,而我们后续自己添加的菜品信息是没有问题的。
4. 菜品修改
4.1 需求分析
在菜品管理列表页面点击修改按钮,跳转到修改菜品页面,在修改页面回显菜品相关信息并进行修改,最后点击确定按钮完成修改操
4.2 前端页面分析
在开发代码之前,需要梳理一下修改菜品时前端页面(add.html)和服务端的交互过程:
1). 点击菜品列表的中的修改按钮,携带菜品id跳转至add.html
2). 进入add.html,页面发送ajax请求,请求服务端获取分类数据,用于菜品分类下拉框中数据展示(已实现)
3). add.html获取id, 发送ajax请求,请求服务端,根据id查询当前菜品信息,用于菜品信息回显
4). 页面发送请求,请求服务端进行图片下载,用于页图片回显(已实现)
5). 点击保存按钮,页面发送ajax请求,将修改后的菜品相关数据以json形式提交到服务端
经过上述的分析,我们发现,菜品分类下拉框的展示、图片的下载回显功能我们都已经实现了。我们只需要在这里实现两个功能即可,分别是:
1). 根据ID查询菜品及菜品口味信息
具体请求信息如下:
请求 | 说明 |
请求方式 | GET |
请求路径 | /dish/{id} |
2). 修改菜品及菜品口味信息
具体请求信息如下:
请求 | 说明 |
请求方式 | PUT |
请求路径 | /dish |
请求参数 | json格式数据 |
具体的json格式数据,我们可以通过浏览器抓取:
<span style="background-color:#f8f8f8"><span style="color:#333333">{ <span style="color:#000000">"id"</span>:<span style="color:#aa1111">"1422783914845487106"</span>, <span style="color:#000000">"name"</span>:<span style="color:#aa1111">"佛跳墙"</span>, <span style="color:#000000">"categoryId"</span>:<span style="color:#aa1111">"1397844357980663809"</span>, <span style="color:#000000">"price"</span>:<span style="color:#116644">88800</span>, <span style="color:#000000">"code"</span>:<span style="color:#aa1111">""</span>, <span style="color:#000000">"image"</span>:<span style="color:#aa1111">"da9e1c70-fc32-4781-9510-a1c4ccd2ff59.jpg"</span>, <span style="color:#000000">"description"</span>:<span style="color:#aa1111">"佛跳墙"</span>, <span style="color:#000000">"status"</span>:<span style="color:#116644">1</span>, <span style="color:#000000">"sort"</span>:<span style="color:#116644">0</span>, <span style="color:#000000">"createTime"</span>:<span style="color:#aa1111">"2021-08-04 12:58:14"</span>, <span style="color:#000000">"createUser"</span>:<span style="color:#aa1111">"1412578435737350122"</span>, <span style="color:#000000">"updateUser"</span>:<span style="color:#aa1111">"1412578435737350122"</span>, <span style="color:#000000">"flavors"</span>:[ { <span style="color:#000000">"id"</span>:<span style="color:#aa1111">"1422783914883235842"</span>, <span style="color:#000000">"dishId"</span>:<span style="color:#aa1111">"1422783914845487106"</span>, <span style="color:#000000">"name"</span>:<span style="color:#aa1111">"辣度"</span>, <span style="color:#000000">"value"</span>:<span style="color:#aa1111">"[\"不辣\",\"微辣\",\"中辣\",\"重辣\"]"</span>, <span style="color:#000000">"createTime"</span>:<span style="color:#aa1111">"2021-08-04 12:58:14"</span>, <span style="color:#000000">"updateTime"</span>:<span style="color:#aa1111">"2021-08-04 12:58:14"</span>, <span style="color:#000000">"createUser"</span>:<span style="color:#aa1111">"1412578435737350122"</span>, <span style="color:#000000">"updateUser"</span>:<span style="color:#aa1111">"1412578435737350122"</span>, <span style="color:#000000">"isDeleted"</span>:<span style="color:#116644">0</span>, <span style="color:#000000">"showOption"</span>:<span style="color:#221199">false</span> }, { <span style="color:#000000">"id"</span>:<span style="color:#aa1111">"1422783914895818754"</span>, <span style="color:#000000">"dishId"</span>:<span style="color:#aa1111">"1422783914845487106"</span>, <span style="color:#000000">"name"</span>:<span style="color:#aa1111">"忌口"</span>, <span style="color:#000000">"value"</span>:<span style="color:#aa1111">"[\"不要葱\",\"不要蒜\",\"不要香菜\",\"不要辣\"]"</span>, <span style="color:#000000">"createTime"</span>:<span style="color:#aa1111">"2021-08-04 12:58:14"</span>, <span style="color:#000000">"updateTime"</span>:<span style="color:#aa1111">"2021-08-04 12:58:14"</span>, <span style="color:#000000">"createUser"</span>:<span style="color:#aa1111">"1412578435737350122"</span>, <span style="color:#000000">"updateUser"</span>:<span style="color:#aa1111">"1412578435737350122"</span>, <span style="color:#000000">"isDeleted"</span>:<span style="color:#116644">0</span>, <span style="color:#000000">"showOption"</span>:<span style="color:#221199">false</span> } ] }</span></span>
4.3 功能实现
4.3.1 根据ID查询菜品信息
4.3.1.1 代码实现
页面发送ajax请求,请求服务端,根据id查询当前菜品信息和对应的口味信息,用于修改页面中菜品信息回显。
1). 在DishService接口中扩展getByIdWithFlavor方法
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">//根据id查询菜品信息和对应的口味信息</span> <span style="color:#770088">public</span> <span style="color:#000000">DishDto</span> <span style="color:#0000ff">getByIdWithFlavor</span>(<span style="color:#008855">Long</span> <span style="color:#000000">id</span>);</span></span>
2). 在DishService实现类中实现此方法
具体逻辑为:
A. 根据ID查询菜品的基本信息
B. 根据菜品的ID查询菜品口味列表数据
C. 组装数据并返回
代码实现:
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/**</span> <span style="color:#aa5500">* 根据id查询菜品信息和对应的口味信息</span> <span style="color:#aa5500">* @param id</span> <span style="color:#aa5500">* @return</span> <span style="color:#aa5500">*/</span> <span style="color:#770088">public</span> <span style="color:#000000">DishDto</span> <span style="color:#0000ff">getByIdWithFlavor</span>(<span style="color:#008855">Long</span> <span style="color:#000000">id</span>) { <span style="color:#aa5500">//查询菜品基本信息,从dish表查询</span> <span style="color:#000000">Dish</span> <span style="color:#000000">dish</span> <span style="color:#981a1a">=</span> <span style="color:#770088">this</span>.<span style="color:#000000">getById</span>(<span style="color:#000000">id</span>); <span style="color:#000000">DishDto</span> <span style="color:#000000">dishDto</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">DishDto</span>(); <span style="color:#000000">BeanUtils</span>.<span style="color:#000000">copyProperties</span>(<span style="color:#000000">dish</span>,<span style="color:#000000">dishDto</span>); <span style="color:#aa5500">//查询当前菜品对应的口味信息,从dish_flavor表查询</span> <span style="color:#000000">LambdaQueryWrapper</span><span style="color:#981a1a"><</span><span style="color:#000000">DishFlavor</span><span style="color:#981a1a">></span> <span style="color:#000000">queryWrapper</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">LambdaQueryWrapper</span><span style="color:#981a1a"><></span>(); <span style="color:#000000">queryWrapper</span>.<span style="color:#000000">eq</span>(<span style="color:#000000">DishFlavor</span>::<span style="color:#000000">getDishId</span>,<span style="color:#000000">dish</span>.<span style="color:#000000">getId</span>()); <span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#000000">DishFlavor</span><span style="color:#981a1a">></span> <span style="color:#000000">flavors</span> <span style="color:#981a1a">=</span> <span style="color:#000000">dishFlavorService</span>.<span style="color:#000000">list</span>(<span style="color:#000000">queryWrapper</span>); <span style="color:#000000">dishDto</span>.<span style="color:#000000">setFlavors</span>(<span style="color:#000000">flavors</span>); <span style="color:#770088">return</span> <span style="color:#000000">dishDto</span>; }</span></span>
3). 在DishController中创建get方法
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/**</span> <span style="color:#aa5500">* 根据id查询菜品信息和对应的口味信息</span> <span style="color:#aa5500">* @param id</span> <span style="color:#aa5500">* @return</span> <span style="color:#aa5500">*/</span> <span style="color:#555555">@GetMapping</span>(<span style="color:#aa1111">"/{id}"</span>) <span style="color:#770088">public</span> <span style="color:#000000">R</span><span style="color:#981a1a"><</span><span style="color:#000000">DishDto</span><span style="color:#981a1a">></span> <span style="color:#0000ff">get</span>(<span style="color:#555555">@PathVariable</span> <span style="color:#008855">Long</span> <span style="color:#000000">id</span>){ <span style="color:#000000">DishDto</span> <span style="color:#000000">dishDto</span> <span style="color:#981a1a">=</span> <span style="color:#000000">dishService</span>.<span style="color:#000000">getByIdWithFlavor</span>(<span style="color:#000000">id</span>); <span style="color:#770088">return</span> <span style="color:#000000">R</span>.<span style="color:#000000">success</span>(<span style="color:#000000">dishDto</span>); } </span></span>
@PathVariable : 该注解可以用来提取url路径中传递的请求参数。
4.3.1.2 功能测试
编写完代码后,重启服务进行测试,点击列表的修改按钮,查询数据回显情况。
4.3.2 修改菜品信息
4.3.2.1 代码实现
点击保存按钮,页面发送ajax请求,将修改后的菜品相关数据以json形式提交到服务端。在修改菜品信息时需要注意,除了要更新dish菜品表,还需要更新dish_flavor菜品口味表。
1). 在DishService接口中扩展方法updateWithFlavor
1.<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">//更新菜品信息,同时更新对应的口味信息</span> <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">updateWithFlavor</span>(<span style="color:#000000">DishDto</span> <span style="color:#000000">dishDto</span>);</span></span>
2). 在DishServiceImpl中实现方法updateWithFlavor
在该方法中,我们既需要更新dish菜品基本信息表,还需要更新dish_flavor菜品口味表。而页面再操作时,关于菜品的口味,有修改,有新增,也有可能删除,我们应该如何更新菜品口味信息呢,其实,无论菜品口味信息如何变化,我们只需要保持一个原则: 先删除,后添加。
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#555555">@Override</span> <span style="color:#555555">@Transactional</span> <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">updateWithFlavor</span>(<span style="color:#000000">DishDto</span> <span style="color:#000000">dishDto</span>) { <span style="color:#aa5500">//更新dish表基本信息</span> <span style="color:#770088">this</span>.<span style="color:#000000">updateById</span>(<span style="color:#000000">dishDto</span>); <span style="color:#aa5500">//清理当前菜品对应口味数据---dish_flavor表的delete操作</span> <span style="color:#000000">LambdaQueryWrapper</span><span style="color:#981a1a"><</span><span style="color:#000000">DishFlavor</span><span style="color:#981a1a">></span> <span style="color:#000000">queryWrapper</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">LambdaQueryWrapper</span>(); <span style="color:#000000">queryWrapper</span>.<span style="color:#000000">eq</span>(<span style="color:#000000">DishFlavor</span>::<span style="color:#000000">getDishId</span>,<span style="color:#000000">dishDto</span>.<span style="color:#000000">getId</span>()); <span style="color:#000000">dishFlavorService</span>.<span style="color:#000000">remove</span>(<span style="color:#000000">queryWrapper</span>); <span style="color:#aa5500">//添加当前提交过来的口味数据---dish_flavor表的insert操作</span> <span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#000000">DishFlavor</span><span style="color:#981a1a">></span> <span style="color:#000000">flavors</span> <span style="color:#981a1a">=</span> <span style="color:#000000">dishDto</span>.<span style="color:#000000">getFlavors</span>(); <span style="color:#000000">flavors</span> <span style="color:#981a1a">=</span> <span style="color:#000000">flavors</span>.<span style="color:#000000">stream</span>().<span style="color:#000000">map</span>((<span style="color:#000000">item</span>) <span style="color:#981a1a">-></span> { <span style="color:#000000">item</span>.<span style="color:#000000">setDishId</span>(<span style="color:#000000">dishDto</span>.<span style="color:#000000">getId</span>()); <span style="color:#770088">return</span> <span style="color:#000000">item</span>; }).<span style="color:#000000">collect</span>(<span style="color:#000000">Collectors</span>.<span style="color:#000000">toList</span>()); <span style="color:#000000">dishFlavorService</span>.<span style="color:#000000">saveBatch</span>(<span style="color:#000000">flavors</span>); }</span></span>
3). 在DishController中创建update方法
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/**</span> <span style="color:#aa5500">* 修改菜品</span> <span style="color:#aa5500">* @param dishDto</span> <span style="color:#aa5500">* @return</span> <span style="color:#aa5500">*/</span> <span style="color:#555555">@PutMapping</span> <span style="color:#770088">public</span> <span style="color:#000000">R</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#0000ff">update</span>(<span style="color:#555555">@RequestBody</span> <span style="color:#000000">DishDto</span> <span style="color:#000000">dishDto</span>){ <span style="color:#000000">log</span>.<span style="color:#000000">info</span>(<span style="color:#000000">dishDto</span>.<span style="color:#000000">toString</span>()); <span style="color:#000000">dishService</span>.<span style="color:#000000">updateWithFlavor</span>(<span style="color:#000000">dishDto</span>); <span style="color:#770088">return</span> <span style="color:#000000">R</span>.<span style="color:#000000">success</span>(<span style="color:#aa1111">"修改菜品成功"</span>); }</span></span>
4.3.2.2 功能测试
代码编写完成之后,重启服务,然后按照前面分析的操作流程进行测试,查看数据是否正常修改即可。