栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMvc---ssm整合--功能模块开发(2)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMvc---ssm整合--功能模块开发(2)

第一步 dao包下新建一个BookDao接口
package com.dao;

import com.domain.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


public interface BookDao {
    @Insert("inser into tb_book values (null,#{type},#{name} ,#{description})")//book中的属性
    public void save(Book book);//存一个
    @Update("update tb_book set type =#{type},name =#{name},description=#{description} where id=#{id}")
    public void update(Book book);//更新
    @Delete("delete from tb_book where id=#{id}")
    public void delete(Integer id);//根据id删除
    @Select("select *from tb_book where id=#{id}")
    public Book getById(Integer id);//根据id查找单个
    @Select("select *from tb_book")
    public List getAll();//获取全部

}
第二步service包下建立一个BookService接口和一个impl包 BookService
package com.service;

import com.domain.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


public interface BookService {

    public boolean save(Book book);//存一个

    public boolean update(Book book);//更新

    public boolean delete(Integer id);//根据id删除

    public Book getById(Integer id);//根据id查找单个

    public List getAll();//获取全部
}
impl包建立BookServiceImpl
package com.service.impl;

import com.dao.BookDao;
import com.domain.Book;
import com.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookServiceImpl implements BookService {

    //需要用到BookDao的接口
    @Autowired
    private BookDao bookDao;//这里是没错的,只是idea为了给我提供帮助,但是我们不需要,只是看着别扭
    public boolean save(Book book) {
        bookDao.save(book);
        return true;
    }

    public boolean update(Book book) {
        bookDao.update(book);
        return true;
    }

    public boolean delete(Integer id) {
        bookDao.delete(id);
        return true;
    }

    public Book getById(Integer id) {

        return bookDao.getById(id);
    }

    public List getAll() {
        return bookDao.getAll();
    }
}

 

第三步controller包下新建一个BookController
package com.controller;

import com.dao.BookDao;
import com.domain.Book;
import com.service.BookService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;//这里是没错的,只是idea为了给我提供帮助,但是我们不需要,只是看着别扭
    @PostMapping
    public boolean save(@RequestBody Book book) {

        return bookService.save(book);
    }
    @PutMapping
    public boolean update(@RequestBody Book book) {
        return bookService.update(book);
    }
   @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
       return bookService.delete(id);

    }
     @GetMapping("/{id}")
    public Book getById(@PathVariable Integer id) {

        return bookService.getById(id);
    }
     @GetMapping
    public List getAll() {
        return bookService.getAll();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877309.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号