发表于: 2025-06-22 20:49:11

0 25


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)

学习多条件查询

多条件查询常用到Mybatis的if判断,这样只有条件满足时,才生成对应的SQL。  

<select id="selectUser" parameterType="map" resultMap="BaseResultMap">  

        select  

        <include refid="Base_Column_List" />  

        from user  

        <where>  

            <if test="name != null">  

                name = #{name,jdbcType=VARCHAR}  

            </if>  

            <if test="age != null">  

                and age = #{age,jdbcType=INTEGER}  

            </if>  

        </where>  

    </select>  

package org.example.controller;

import org.example.model.ApiResponse;
import org.example.model.Banner;
import org.example.model.Comment;
import org.example.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/banner")
public class BannerController {
@Autowired
private BannerService bannerService;
@GetMapping("/search")
public ApiResponse<List<Banner>> queryBanner(String author, String state) {
return ApiResponse.success(bannerService.getByCondition(author, state));
}
@GetMapping("/all")
public ApiResponse <List<Banner>> getAllBanners() {
List<Banner> banners = bannerService.getAllBanners();
return ApiResponse.success(banners);
}
@PutMapping("/{id}")
public ApiResponse<Map<String, Object>> updateBanner(@PathVariable Long id, @RequestBody Banner banner) {
try {
banner.setId(id);
boolean success = bannerService.updateBanner(banner);
if (success) {
Map<String, Object> response = new HashMap<>();
response.put("id", id);
return ApiResponse.success(response);
} else {
return ApiResponse.error(500, "更新失败,请检查ID是否存在或字段是否有改动");
}
} catch (Exception e) {
e.printStackTrace();
return ApiResponse.error(400, "更新失败: " + e.getMessage());
}
}
@PostMapping("/")
public ApiResponse <Long> createBanner(@RequestBody Banner banner) {
return ApiResponse.success(bannerService.insertBanner(banner));
}
@DeleteMapping("/{id}")
public ApiResponse <Boolean> deleteBanner(@PathVariable Long id) {
return ApiResponse.success(bannerService.deleteBanner(id));
}
}

明天计划的事情:(一定要写非常细致的内容)
遇到的问题:(遇到什么困难,怎么解决的)
收获:(通过今天的学习,学到了什么知识)


返回列表 返回列表
评论

    分享到