码迷,mamicode.com
首页 > 其他好文 > 详细

SSM整合(5) 添加一个查询功能

时间:2021-05-23 23:05:04      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:实现类   mit   inpu   turn   select   最好   oid   java   books   

新加一个功能,写法上最好是从下往上写
1.dao层添加一个接口

package dao;

import org.apache.ibatis.annotations.Param;
import pojo.Books;

import java.util.List;

public interface BookMapper {
    //增加一本书
    int addBook(Books book);
    //删除一本书
    int deleteBookById(@Param("bookID") int id);
    //更新一本书
    int updateBook(Books book);
    //查询一本书
    Books queryBookById(@Param("bookID") int id);
    //查询全部书
    List<Books> queryAllBook();
    //根据名字查询一本书
    List<Books> queryBookByName(@Param("bookName") String bookName);
}

2.配置mapper

<select id="queryBookByName" resultType="Books">
        select *
        from ssmbuild.books
        <where>
        <if test="bookName!=null || bookName!=‘‘">
            <bind name="BindbookName" value="‘%‘+bookName+‘%‘"/>
            bookName like #{BindbookName}
        </if>
        </where>
    </select>

定义Service接口和实现类

package service;
import pojo.Books;


import java.util.List;

public interface BookService {
    //增加一本书
    int addBook(Books book);
    //删除一本书
    int deleteBookById(int id);
    //更新一本书
    int updateBook(Books book);
    //查询一本书
    Books queryBookById(int id);
    //查询全部书
    List<Books> queryAllBook();
    List<Books> queryBookByName(String bookName);
}

package service;

import dao.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import pojo.Books;


import java.util.List;

public class BookServiceImpl implements BookService {
    //service调用dao
    private BookMapper bookMapper;

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }
    public int addBook(Books book) {
        return bookMapper.addBook(book);
    }

    public int deleteBookById(int id) {
        return bookMapper.deleteBookById(id);
    }

    public int updateBook(Books book) {
        return bookMapper.updateBook(book);
    }

    public Books queryBookById(int id) {
        return bookMapper.queryBookById(id);
    }

    public List<Books> queryAllBook() {
        return bookMapper.queryAllBook();
    }

    public List<Books> queryBookByName(String bookName) {
        return bookMapper.queryBookByName(bookName);
    }


}

控制类多添加一个方法

//查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String BookName,Model model){
        List<Books> list = bookService.queryBookByName(BookName);
        model.addAttribute("list",list);
        return "allBook";
    }

jsp插入代码

<div class="col-md-4 column">
            <!--查询书籍-->
            <form action="${pageContext.request.contextPath}/book/queryBook" method="post" style="float:right" class="form-inline">
                <input type="text" name="BookName" class="form-control" placeholder="请输入要查询的书籍名称">
                <input type="submit" value="查询" class="btn btn-primary">
            </form>
        </div>

SSM整合(5) 添加一个查询功能

标签:实现类   mit   inpu   turn   select   最好   oid   java   books   

原文地址:https://www.cnblogs.com/OfflineBoy/p/14727712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!