1. 程式人生 > 其它 >cvpr頂會熱詞的增刪改查

cvpr頂會熱詞的增刪改查

  對於頂會熱詞的一系列操作,我們選擇使用了SpringBoot+Mybatis+Thymeleaf+Layui的組合。具體實現如下。

實體類

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Info {

    private Integer id;
    private String name;
    private String pdf;
    private String author;
    private String title;
    private String booktitle;
    
private String date; }

使用Lombok, 需要的依賴如下

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

我們在這裡定義一個Vo類。因為後面使用Layui進行分頁的時候,需要傳回一些特定的引數,我們定義vo類方便管理。這裡不需要看懂,在寫前端頁面的時候可以返回這裡檢視。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class InfoVo { private int code; private String msg; private int count; private List<Info> data; }

mapper層

CvprMapper是一個介面

@Repository
@Mapper
public interface CvprMapper {

    // 根據 id 查詢 name
    public String getNameById(@Param("id") Integer id);

    // 根據 論文編號 論文題目 論文作者 發行日期 進行查詢
public List<Info> getInfo(@Param("id") Integer id, @Param("title") String title, @Param("author") String author, @Param("date") String date, @Param("page") int page, @Param("limit")int limit); // 根據 論文編號 論文題目 論文作者 發行日期 進行查詢數量 public int getInfoCount(@Param("id") Integer id, @Param("title") String title, @Param("author") String author, @Param("date") String date, @Param("page") int page, @Param("limit")int limit); // 根據 id 刪除 資訊 public int deleteInfoById(@Param("id") Integer id); // 新增一個資訊 public int addInfo(@Param("name") String name, @Param("pdf") String pdf, @Param("author") String author, @Param("title") String title, @Param("booktitle") String booktitle, @Param("date") String date); // 根據id查詢資訊 public int updateInfo(@Param("id") Integer id, @Param("title") String title, @Param("author") String author, @Param("booktitle") String booktitle, @Param("pdf") String pdf, @Param("date") String date); }

CvprMapper.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.gazikel.cvpr.mapper.CvprMapper">
    <select id="getNameById" parameterType="integer" resultType="string">
        select `name` from cvpr where id = #{id}
    </select>

    <select id="getInfo" resultType="info">
        <bind name="key_offset" value="(page-1)*limit"></bind>
        SELECT id, `name`, author, booktitle, pdf, `date` FROM cvpr
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="title != null">
                and `name` like #{title}
            </if>
            <if test="author != null">
                and author like #{author}
            </if>
            <if test="date != null">
                and `date` = #{date}
            </if>
        </where> limit #{key_offset}, #{limit}
    </select>

    <select id="getInfoCount" resultType="integer">
        select count(*) from cvpr
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="title != null">
                and `name` like #{title}
            </if>
            <if test="author != null">
                and author like #{author}
            </if>
            <if test="date != null">
                and `date` = #{date}
            </if>
        </where>
    </select>

    <delete id="deleteInfoById" parameterType="integer">
        delete from cvpr where id = #{id}
    </delete>

    <insert id="addInfo">
        insert into cvpr(`name`, pdf, author, title, booktitle, `date`) values (#{name}, #{pdf}, #{author}, #{title}, #{booktitle}, #{date})
    </insert>

    <update id="updateInfo">
        update cvpr set `name` = #{title}, pdf = #{pdf}, author = #{author}, title = #{title}, booktitle = #{booktitle}, `date` = #{date} where id = #{id}
    </update>
</mapper>

service層

CvprService

public interface CvprService {

    // 根據 id 查詢 name
    public String getNameById(Integer id);

    // 根據 論文編號 論文題目 論文作者 發行日期 進行查詢
    public InfoVo getInfo(Integer id, String title, String author, String date, int page, int limit);

    // 根據 id 刪除 資訊
    public int deleteInfoById(Integer id);

    // 新增一個資訊
    public int addInfo(String name, String pdf, String author, String title, String booktitle, String date);

    // 根據id查詢資訊
    public int updateInfo(Integer id, String title, String author, String booktitle, String pdf, String date);
}

CvprServiceImpl

@Service
public class CvprServiceImpl implements CvprService {

    @Autowired
    private CvprMapper cvprMapper;

    @Override
    public String getNameById(Integer id) {
        return cvprMapper.getNameById(id);
    }

    @Override
    public InfoVo getInfo(Integer id, String title, String author, String date, int page, int limit) {
        InfoVo infoVo = new InfoVo();
        infoVo.setCode(0);
        infoVo.setMsg("");
        // System.out.println(cvprMapper.getInfoCount(id, title, author, date, page, limit));
        // System.out.println(date);
        infoVo.setCount(cvprMapper.getInfoCount(id, title, author, date, page, limit));

        infoVo.setData(cvprMapper.getInfo(id, title, author, date, page, limit));
        return infoVo;
    }

    @Override
    public int deleteInfoById(Integer id) {
        return cvprMapper.deleteInfoById(id);
    }

    @Override
    public int addInfo(String name, String pdf, String author, String title, String booktitle, String date) {
        return cvprMapper.addInfo(name, pdf, author, title, booktitle, date);
    }

    @Override
    public int updateInfo(Integer id, String title, String author, String booktitle, String pdf, String date) {
        return cvprMapper.updateInfo(id, title, author, booktitle, pdf, date);
    }


}

controller層

@Controller
public class CvprController {

    @Autowired
    private CvprService cvprService;

    @GetMapping("/getList")
    @ResponseBody
    public String getInfo(@RequestParam("id")@Nullable Integer id, @RequestParam("title")@Nullable String title, @RequestParam("author")@Nullable String author, @RequestParam("date")@Nullable String date, @RequestParam("page") int page, @RequestParam("limit")int limit) {


        if ("".equals(id))
            id = null;
        if ("".equals(title) || title == null)
            title = null;
        else
            title = "%"+title+"%";

        if ("".equals(author) || title == null)
            author = null;
        else
            author = "%"+author+"%";
        if ("".equals(date) || date == null)
            date = null;
        else
            date = date.replaceAll("-", "");

        InfoVo info = cvprService.getInfo(id, title, author, date, page, limit);
        return JSON.toJSONString(info);
    }

    // 根據 id 刪除
    @PostMapping("/deleteInfo")
    @ResponseBody
    public Map<String, String> deleteInfo(@RequestParam("id") Integer id) {

        Map<String, String> map = new HashMap<>();
        int i = cvprService.deleteInfoById(id);

        if (i == 1) {
            map.put("status", "success");
        } else {
            map.put("status", "error");
        }

        return map;

    }

    @PostMapping("/addInfo")
    @ResponseBody
    public String addInfo(@RequestParam("title") String title, @RequestParam("author") String author, @RequestParam("booktitle") String booktitle, @RequestParam("pdf")String pdf, @RequestParam("date")String date) {

        if (date != null) {
            date = date.replaceAll("-", "");
        }

        cvprService.addInfo(title, pdf, author, title, booktitle, date);

        return null;
    }

    // 根據id修改論文資訊
    @PostMapping("/updateInfo")
    @ResponseBody
    public Map<String, String> updateInfo(Integer id, String title, String author, String booktitle, String pdf, String date) {

        if (date != null) {
            date = date.replaceAll("-", "");
        }
        Map<String,String> map = new HashMap<>();

        int i = cvprService.updateInfo(id, title, author, booktitle, pdf, date);

        if (i==1) {
            map.put("status", "success");
        } else {
            map.put("status", "error");
        }
        return map;
    }

}

後端完成!