SSM整合(二)---功能模块开发

1.创建包dao、service(包含Impl包)、controller
2.dao层
BookDao接口:

package com.itheima.dao;

import com.itheima.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 org.springframework.stereotype.Component;

import java.util.List;
public interface BookDao {
    @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
    //添加书籍
    public void save(Book book);
    //更新
    @Update("update tbl_book set type = #{type},name = #{name},description = #{description} where id = #{id}")
    public void update(Book book);
    //删除
    @Delete("delete from tbl_book where id = #{id}")
    public void deleteById(Integer id);
    //查询书籍通过id
    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);
    //查询全部
    @Select("select * from tbl_book")
    public List<Book> getAll();
}

3.service层
BookService

package com.itheima.service;

import com.itheima.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 org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
public interface BookService {

    //添加书籍
    public boolean save(Book book);
    //更新书籍
    public boolean update(Book book);
    //按id删除
    public boolean deleteById(Integer id);
    //按id查询
    public Book getById(Integer id);
    //查询全部
    public List<Book> getAll();
}

BookServiceImpl类:

package com.itheima.service.impl;

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

import javax.annotation.Resource;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Resource
    private BookDao bookDao;

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

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

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

    public Book getById(Integer id) {
        return bookDao.getById(id);
    }

    public List<Book> getAll() {
        return bookDao.getAll();
    }
}

4.controller层
BookController:

package com.itheima.controller;

import com.itheima.domain.Book;
import com.itheima.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("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping
    public Result save(@RequestBody Book book) {
        boolean flag=bookService.save(book);
        return new Result(flag,flag ? Code.SAVE_OK:Code.SAVE_ERR);
    }

    @PutMapping
    public Result update(@RequestBody Book book) {
        boolean flag=bookService.update(book);
        return new Result(flag,flag ? Code.UPDATE_OK:Code.UPDATE_ERR);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag=bookService.deleteById(id);
        return new Result(flag,flag ? Code.DELETE_OK:Code.DELETE_ERR);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book book=bookService.getById(id);
        Integer code=book !=null ? Code.GET_OK : Code.GET_ERR;
        String msg=book !=null ? " " : "数据查询失败,请重试!";
        return new Result(book,code,msg);
    }

    @GetMapping
    public Result getAll() {
        List<Book> books=bookService.getAll();
        Integer code=books !=null ? Code.GET_OK : Code.GET_ERR;
        String msg=books !=null ? " " : "数据查询失败,请重试!";
        return new Result(books,code,msg);
    }
}

5.domain层

package com.itheima.domain;

public class Book {
    private Integer id;
    private String type;
    private String name;
    private String description;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

测试:
对service层进行测试
BookServiceTest类

package com.itheima.service;

import com.itheima.config.SpringConfig;
import com.itheima.domain.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

//Spring整合Junit的测试
//DAO层写完就可以做测试了,但是一般都对业务层做测试
@RunWith(SpringJUnit4ClassRunner.class)
//指定配置类
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {
    @Autowired
    private BookService bookService;
    @Test
    public void testGetById()
    {
        Book book=bookService.getById(1);
        System.out.println(book);
    }

    @Test
    public void testGetAll()
    {
        List<Book> books=bookService.getAll();
        for(Book book:books)
        {
            System.out.println(book);
        }
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值