SpringBoot和mybatis的高级应用

╰半橙微兮° 2022-06-02 11:37 227阅读 0赞
  1. package com.moxi.service;
  2. import java.util.List;
  3. import org.apache.ibatis.annotations.Insert;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import org.apache.ibatis.annotations.Select;
  6. import org.apache.ibatis.annotations.Update;
  7. import com.moxi.model.News;
  8. import com.moxi.util.Constant;
  9. @Mapper
  10. public interface NewsService {
  11. @Select("SELECT * FROM MOXI.NEWS WHERE ID = #{id};")
  12. News findById(News news);
  13. @Select({
  14. "<script>",
  15. "SELECT N.*,C.NAME AS CATEGORYNAME,C.IMAGE AS CATEGORYIMAGE FROM MOXI.NEWS N ",
  16. "LEFT JOIN MOXI.NEWS_CATEGORY C ON N.CATEGORY = C.ID ",
  17. "WHERE N.STATE = 1 ",
  18. "<when test='title!=null'>",
  19. "AND N.TITLE LIKE CONCAT('%',#{title},'%')",
  20. "</when>",
  21. "<when test='category!=0'>",
  22. "AND category = #{category}",
  23. "</when>",
  24. "<when test='commendState!=0'>",
  25. "AND commendState = #{commendState}",
  26. "</when>",
  27. "<when test='orderBy==\""+Constant.OrderByAddDateAsc+"\"'>",
  28. "order by "+Constant.OrderByAddDateAsc+",addDate desc",
  29. "</when>",
  30. "<when test='orderBy==\""+Constant.OrderByAddDateDesc+"\"'>",
  31. "order by "+Constant.OrderByAddDateDesc,
  32. "</when>",
  33. "<when test='orderBy==\""+Constant.OrderByBrowsesDesc+"\"'>",
  34. "order by "+Constant.OrderByBrowsesDesc+",addDate desc",
  35. "</when>",
  36. "<when test='orderBy==\""+Constant.OrderByCommentsDesc+"\"'>",
  37. "order by "+Constant.OrderByCommentsDesc+",addDate desc",
  38. "</when>",
  39. "<when test='orderBy==\""+Constant.OrderByLikesDesc+"\"'>",
  40. "order by "+Constant.OrderByLikesDesc+",addDate desc",
  41. "</when>",
  42. "<when test='orderBy==\""+Constant.OrderByScoreDesc+"\"'>",
  43. "order by "+Constant.OrderByScoreDesc+",addDate desc",
  44. "</when>",
  45. "limit #{start},#{end}",
  46. "</script>" })
  47. List<News> list(News news);
  48. @Select({
  49. "<script>",
  50. "SELECT COUNT(*) FROM MOXI.NEWS N ",
  51. "LEFT JOIN MOXI.NEWS_CATEGORY C ON N.CATEGORY = C.ID ",
  52. "WHERE N.STATE = 1 ",
  53. "<when test='title!=null'>",
  54. "AND N.TITLE LIKE CONCAT('%',#{title},'%')",
  55. "</when>",
  56. "<when test='category!=0'>",
  57. "AND category = #{category}",
  58. "</when>",
  59. "<when test='commendState!=0'>",
  60. "AND commendState = #{commendState}",
  61. "</when>",
  62. "</script>" })
  63. int count(News news);
  64. @Insert("INSERT INTO `moxi`.`news` (`id`,`title`,`description`,`category`,`image`,`content`,`addDate`,`updateDate`,`commendState`,`state`,`browses`,`likes`,`comments`,`score`) VALUES (null,#{title},#{description},#{category},#{image},#{content},now(),now(),1,1,0,0,0,0);")
  65. int insert(News news);
  66. @Update("UPDATE `moxi`.`news` SET `title` = #{title}, `description` = #{description}, `category` = #{category}, `image` = #{image}, `content` = #{content}, `updateDate` = now() WHERE `id` = #{id};")
  67. int update(News news);
  68. @Update("UPDATE `moxi`.`news` SET `state` = #{state}, `commendState` = #{commendState}, `browses` = #{browses}, `likes` = #{likes}, `comments` = #{comments}, `score` = #{score} WHERE `id` = #{id};")
  69. int updateState(News news);
  70. }
  71. -------------
  72. package com.moxi.service;
  73. import java.util.List;
  74. import org.apache.ibatis.annotations.Insert;
  75. import org.apache.ibatis.annotations.Mapper;
  76. import org.apache.ibatis.annotations.Select;
  77. import org.apache.ibatis.annotations.Update;
  78. import com.moxi.model.NewsCategory;
  79. @Mapper
  80. public interface NewsCategoryService {
  81. @Select("SELECT * FROM `moxi`.`news_category` where id = #{id};")
  82. NewsCategory findById(NewsCategory newsCategory);
  83. @Select({
  84. "<script>",
  85. "SELECT * FROM `moxi`.`news_category`",
  86. "WHERE state = 1",
  87. "<when test='name!=null'>",
  88. "AND name LIKE CONCAT('%',#{name},'%')",
  89. "</when>",
  90. "order by addDate desc limit #{start},#{end}",
  91. "</script>" })
  92. List<NewsCategory> list(NewsCategory newsCategory);
  93. @Select({
  94. "<script>",
  95. "SELECT count(*) FROM `moxi`.`news_category`",
  96. "WHERE state = 1",
  97. "<when test='name!=null'>",
  98. "AND name LIKE CONCAT('%',#{name},'%')",
  99. "</when>",
  100. "</script>" })
  101. int count(NewsCategory newsCategory);
  102. @Insert("INSERT INTO `moxi`.`news_category` (`id`, `name`, `description`, `image`, `addDate`, `state`) VALUES (null, #{name}, #{description}, #{image}, now(), 1);")
  103. int insert(NewsCategory newsCategory);
  104. @Update("UPDATE `moxi`.`news_category`SET `name` = #{name}, `description` = #{description}, `image` = #{image} WHERE `id` = #{id};")
  105. int update(NewsCategory newsCategory);
  106. @Update("UPDATE `moxi`.`news_category`SET `state` = #{state} WHERE `id` = #{id};")
  107. int updateState(NewsCategory newsCategory);
  108. }
  109. --------

发表评论

表情:
评论列表 (有 0 条评论,227人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mybatis高级

    一对一查询 第一种,用拓展类的方式 需要一对一查询Course和Teacher两个表 新建一个类,继承Course类,然后将Teacher类中的属性加到这个新类中

    相关 MyBatis高级

    一对一查询: 1.resultType进行实现: 执行的sql语句: 查询的主表:订单表 查询的关联表:用户表 orders表有一个外键 select