SpringBoot和mybatis的高级应用

10 篇文章 0 订阅
8 篇文章 0 订阅
package com.moxi.service;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.moxi.model.News;
import com.moxi.util.Constant;
   
@Mapper
public interface NewsService {
   
   @Select("SELECT * FROM MOXI.NEWS WHERE ID = #{id};")
   News findById(News news);
   
   @Select({
      "<script>",
      "SELECT N.*,C.NAME AS CATEGORYNAME,C.IMAGE AS CATEGORYIMAGE FROM MOXI.NEWS N ",
      "LEFT JOIN MOXI.NEWS_CATEGORY C ON N.CATEGORY = C.ID ",
      "WHERE N.STATE = 1 ",
         "<when test='title!=null'>",
            "AND N.TITLE LIKE CONCAT('%',#{title},'%')",
         "</when>",
         "<when test='category!=0'>",
            "AND category = #{category}",
         "</when>",
         "<when test='commendState!=0'>",
            "AND commendState = #{commendState}",
         "</when>",
         "<when test='orderBy==\""+Constant.OrderByAddDateAsc+"\"'>",
            "order by "+Constant.OrderByAddDateAsc+",addDate desc",
         "</when>",
         "<when test='orderBy==\""+Constant.OrderByAddDateDesc+"\"'>",
            "order by "+Constant.OrderByAddDateDesc,
         "</when>",
         "<when test='orderBy==\""+Constant.OrderByBrowsesDesc+"\"'>",
            "order by "+Constant.OrderByBrowsesDesc+",addDate desc",
         "</when>",
         "<when test='orderBy==\""+Constant.OrderByCommentsDesc+"\"'>",
            "order by "+Constant.OrderByCommentsDesc+",addDate desc",
         "</when>",
         "<when test='orderBy==\""+Constant.OrderByLikesDesc+"\"'>",
            "order by "+Constant.OrderByLikesDesc+",addDate desc",
         "</when>",
         "<when test='orderBy==\""+Constant.OrderByScoreDesc+"\"'>",
            "order by "+Constant.OrderByScoreDesc+",addDate desc",
         "</when>",
         "limit #{start},#{end}",
      "</script>"
   })
   List<News> list(News news);
   
   @Select({
      "<script>",
      "SELECT COUNT(*) FROM MOXI.NEWS N ",
      "LEFT JOIN MOXI.NEWS_CATEGORY C ON N.CATEGORY = C.ID ",
      "WHERE N.STATE = 1 ",
         "<when test='title!=null'>",
            "AND N.TITLE LIKE CONCAT('%',#{title},'%')",
         "</when>",
         "<when test='category!=0'>",
            "AND category = #{category}",
         "</when>",
         "<when test='commendState!=0'>",
            "AND commendState = #{commendState}",
         "</when>",
      "</script>"
   })
   int count(News news);
   
   @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);")
   int insert(News news);

   @Update("UPDATE `moxi`.`news` SET `title` = #{title}, `description` = #{description}, `category` = #{category}, `image` = #{image}, `content` = #{content}, `updateDate` = now()  WHERE `id` = #{id};")
   int update(News news);
   
   @Update("UPDATE `moxi`.`news` SET `state` = #{state}, `commendState` = #{commendState}, `browses` = #{browses}, `likes` = #{likes}, `comments` = #{comments}, `score` = #{score} WHERE `id` = #{id};")
   int updateState(News news);
   
}
-------------
package com.moxi.service;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.moxi.model.NewsCategory;

@Mapper
public interface NewsCategoryService {
   
   @Select("SELECT * FROM `moxi`.`news_category` where id = #{id};")
   NewsCategory findById(NewsCategory newsCategory);
   
   @Select({
      "<script>",
      "SELECT * FROM `moxi`.`news_category`",
      "WHERE state = 1",
         "<when test='name!=null'>",
            "AND name LIKE CONCAT('%',#{name},'%')",
         "</when>",
         "order by addDate desc limit #{start},#{end}",
      "</script>"
   })
   List<NewsCategory> list(NewsCategory newsCategory);
   
   @Select({
      "<script>",
      "SELECT count(*) FROM `moxi`.`news_category`",
      "WHERE state = 1",
         "<when test='name!=null'>",
            "AND name LIKE CONCAT('%',#{name},'%')",
         "</when>",
      "</script>"
   })
   int count(NewsCategory newsCategory);
   
   @Insert("INSERT INTO `moxi`.`news_category` (`id`, `name`, `description`, `image`, `addDate`, `state`) VALUES (null, #{name}, #{description}, #{image}, now(), 1);")
   int insert(NewsCategory newsCategory);
   
   @Update("UPDATE `moxi`.`news_category`SET `name` = #{name}, `description` = #{description}, `image` = #{image} WHERE `id` = #{id};")
   int update(NewsCategory newsCategory);
   
   @Update("UPDATE `moxi`.`news_category`SET `state` = #{state} WHERE `id` = #{id};")
   int updateState(NewsCategory newsCategory);
}
--------
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值