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;
    }

}

 

 

后端完成!

 

posted @ 2021-06-15 10:04  Gazikel  阅读(97)  评论(0编辑  收藏  举报