Mybatis

NewsController.java

package com.teach.ssm.mvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.ssm.mvc.domain.News;
import com.teach.ssm.mvc.dto.NewsDto;
import com.teach.ssm.mvc.service.NewsService;

@Controller
public class NewsController {
      @Autowired
      private NewsService newsService;
      
      @RequestMapping("/pubNews")
      @ResponseBody
      public Map<String, Object> pubNews(@RequestBody NewsDto newsDto){
          //1.获取请求
     News news=newsDto.getNews();
        //2.  
      int pubCount=newsService.pubNews(news);
        //3.  
      Map<String, Object> ret=new HashMap(){{
          put("code", 200);
          put("message", "成功发布新闻"+pubCount+"条,id"+news.getId());
          
      }};
      return ret;
      }
      //模糊查询
      @RequestMapping("/queryNewsByTitle")
      @ResponseBody
      public Map<String, Object> queryNewsByTitle(@RequestBody Map<String, Object> params){
          //1.获取请求
          String title=(String)params.get("title");
        //2.  
          List<News> newsList=(List<News>) newsService.queryNewsByTitle(title);
        //3.  
          Map<String, Object> redata=new HashMap(){{
              put("newsList", newsList);
              
          }};
      Map<String, Object> ret=new HashMap(){{
          put("code", 200);
          put("message", "根据title模糊查询");
          put("redata", redata);
      }};
      return ret;
      }
      
      @RequestMapping("/modifyNewsTitleById")
      @ResponseBody
      public Map<String, Object> pubNews(@RequestBody News news){
        //获取参数
//        Integer id=(Integer)params.get("id");
//        String title=(String)params.get("title");
//        News news=new News();
//        news.setId(id);
//        news.setTitle(title);
          //
        newsService.modifyNewsTitleById(news);
        //3.  
      Map<String, Object> ret=new HashMap(){{
          put("code", 200);
          put("message", "修改id"+news.getId()+"新闻成功");
          
      }};
      return ret;
      }
      
      //更新数据
      @RequestMapping("/updateAllNews")
      @ResponseBody
      public Map<String, Object> updateAllNews(@RequestBody Map<String, Object> upparam){
        //
          int id=(int)upparam.get("id");
          //
        List<News> newsList=(List<News>) newsService.readNewsByID(id);
        //3.  
        
          Map<String, Object> reData=new HashMap(){{
              put("newsList", newsList);
              
          }};
      Map<String, Object> ret=new HashMap(){{
          put("code", 200);
          put("message", "更新成功");
          put("message", reData);
          
      }};
      return ret;
      }
      
      
      
      //根据id删除数据
      @RequestMapping("/deleteNewsById")
      @ResponseBody
      public Map<String, Object> deleteNewsById(@RequestBody Map<String, Object> params){
          
          Integer id=(Integer)params.get("id");
          //
        newsService.deleteNewsById(id);
        //3.  
      Map<String, Object> ret=new HashMap(){{
          put("code", 200);
          put("message", "成功删除id"+params.get("id")+"的新闻");
      }};
      return ret;
      }
      
      @RequestMapping("/removeNewsByIds")
      @ResponseBody
      public Map<String, Object> removeNewsByIds(@RequestBody Map<String, Object> params){
          
          List<Integer> idList=(List<Integer>)params.get("ids");
          Integer[] ids=idList.toArray(new Integer[0]);
          //
        int deleteCount=newsService.removeNewsByIds(ids);
        //3.  
        Map<String, Object> data=new HashMap(){{
              put("deleteCount", deleteCount);
          }};
      Map<String, Object> ret=new HashMap(){{
          put("code", 200);
          put("message", "根据id数组删除新闻成功");
          put("data", data);
      }};
      return ret;
      }
      
    //根据id删除数据
      @RequestMapping("/testTransation")
      @ResponseBody
      public Map<String, Object> testTransation(){
          
        newsService.removeNewsById();
        //3.  
      Map<String, Object> ret=new HashMap(){{
          put("code", 200);
          put("message", "ok");
      }};
      return ret;
      }
}

NewsDao.java

package com.teach.ssm.mvc.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.teach.ssm.mvc.domain.News;

@Repository
public interface NewsDao {

@Insert("insert into news(title,author,content) values(#{title},#{author},#{content})")
@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
public int createNews(News news);

@Select("select id,title,author,content from news where id=#{id}")
public News readNewsById(int id);

@Select("select id,title,author,content from news where title like #{title}")
public List<News> readNewsByTitle(String title);

@Update("update news set title=#{title} where id =#{id}")
public int updateNewsTitleById(News news);

@Delete("delete from news where id in (${ids})")
public int deleteNewsByIds(@Param("ids") String ids);

public int deleteNewsById(int id);


}


NewsService.java

package com.teach.ssm.mvc.service;

import java.util.List;

import com.teach.ssm.mvc.domain.News;

public interface NewsService {
    public int pubNews(News news);

    public List<News> queryNewsByTitle(String title);

    public    int modifyNewsTitleById(News news);

    public void modiflyNewsById(News news);

    public int removeNewsByIds(Integer[] ids);

    public int removeNewsById();

    public List<News> readNewsByID(int id);

    public int deleteNewsById(int id);

}

NewsServiceImpl.java

package com.teach.ssm.mvc.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.teach.ssm.mvc.dao.NewsDao;
import com.teach.ssm.mvc.domain.News;
import com.teach.ssm.mvc.service.NewsService;

@Service
public class NewsServiceImpl implements NewsService{
    
@Autowired
private NewsDao newsDao;

@Override
public int pubNews(News news) {
    // TODO Auto-generated method stub
    int result= newsDao.createNews(news);
//    System.out.println(news.getId());
    return result;
}
@Override
public List<News> queryNewsByTitle(String title){
    return newsDao.readNewsByTitle("%"+title+"%");
}
@Override
public int modifyNewsTitleById(News news) {
    // TODO Auto-generated method stub
    return newsDao.updateNewsTitleById(news);
}

@Override
public int deleteNewsById(int id) {
    // TODO Auto-generated method stub
    return newsDao.deleteNewsById(id);
}
@Override
public void modiflyNewsById(News news) {
    // TODO Auto-generated method stub
    
}
@Override
public int removeNewsByIds(Integer[] ids) {
    // TODO Auto-generated method stub
    return newsDao.deleteNewsByIds(StringUtils.join(ids,","));
}

@Transactional
public int removeNewsById() {
    newsDao.deleteNewsById(14);
    newsDao.deleteNewsById(15);
    
    System.out.println(1/0);
    return 0;    
}
@Override
public List<News> readNewsByID(int id) {
    // TODO Auto-generated method stub
    return null;
}

}
News.java

package com.teach.ssm.mvc.domain;

public class News {
    
    private int id;
    private String title;
    private String author;
    private String content;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    @Override
    public String toString() {
        return "News [id=" + id + ", title=" + title + ", author=" + author + ", content=" + content + "]";
    }    

}
NewsDto.java

package com.teach.ssm.mvc.dto;

import com.teach.ssm.mvc.domain.News;

public class NewsDto {
private News news;

public News getNews() {
    return news;
}

public void setNews(News news) {
    this.news = news;
}

}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值