SSM图书管理(一)

(1)bean(放入pojo)该bean和数据库中的表产生映射:首先创建Book实体类创建

package pojo;
public class Book {
    private int bookId;//属性要与表中的一模一样
    private String Isbn;
    private String Title;
    private String Price;
    public int getBookId() {
        return bookId;
    }
    public void setBookId(int bookId) {
        this.bookId = bookId;
    }
    public String getIsbn() {
        return Isbn;
    }
    public void setIsbn(String isbn) {
        Isbn = isbn;
    }
    public String getTitle() {
        return Title;
    }
    public void setTitle(String title) {
        Title = title;
    }
    public String getPrice() {
        return Price;
    }
    public void setPrice(String price) {
        Price = price;
    }
}

(2)创建mapper

package mapper;
import java.util.*;
import pojo.Book;
public interface BookMapper {
    //定义Book可以提供给用户进行哪些操作
    public List<Book> list();
    public void add(Book book);
    public void delete(int bookId);
    public Book get(int booId);
    public void update(Book book);
}

创建Book.xml:Book.xml建立关联并且执行相关操作

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.BookMapper">
    <!-- id和BookMapper里的函数名一样! -->
    <!-- resulttype 数据的返回类型 指定为Book即可 -->
    <select id="list" resultType="Book">
        select * from book_table
    </select>
    <insert id="add" parameterType="Book">
        insert into book_table (Isbn,Title,Price) values(#{Isbn},#{Title},#{Price})
    </insert>
    <delete id="delete" parameterType="Book">
        delete from book_table where bookId=#{bookId}
    </delete>
    <select id="get" parameterType="int" resultType="Book">
        select * from book_table where bookId=#{bookId}
    </select>
    <update id="update" parameterType="Book">
        update book_table set Isbn=#{Isbn},Title=#{Title},Price=#{Price} where bookId=#{bookId}
    </update>
</mapper>

(3)创建BookService接口:业务逻辑层,对外提供的功能

package service;
import java.util.List;
import pojo.Book;
public interface BookService {
    //调用list()实际是调用数据持久化层BookMapper中的list()
    public List<Book> list();//这里函数名与BookMapper中的名称无关
    public void add(Book book);
    public void delete(int bookId);
    public Book get(int booId);
    public void update(Book book);
}

接口实现类:

package service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import mapper.BookMapper;
import pojo.Book;
import service.BookService;
//告诉spring这是一个实现类
@ Service
public class BookServiceImpl implements BookService {
    @Autowired//自动帮我们new BookMapper的实例化对象 
    BookMapper bookMapper;
    //spring注解将属性实例化

    @Override
    public List<Book> list() {
        return bookMapper.list();
    }
    @Override
    public void add(Book book) {
        bookMapper.add(book);
    }
    @Override
    public void delete(int bookId) {
        bookMapper.delete(bookId);
    }
    @Override
    public Book get(int booId) {
        return bookMapper.get(booId);
    }
    @Override
    public void update(Book book) {
        bookMapper.update(book);
    }
}

(4)创建控制器类(控制层)

package control;//这个包在springMVC.xml进行了注册
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import pojo.Book;
import service.BookService;

@Controller//告诉spring mvc这是一个控制器类
@RequestMapping("")//接收用户请求
public class BookController {
    //所有的调用都是通过这个类
    @Autowired
    BookService bookService;//不需要new

    @RequestMapping("listBook")//相当于struts.xml中的action的名字name
    public ModelAndView ListBook(){
        ModelAndView mav=new ModelAndView();//充当原来set/get方法
        List<Book> cs=bookService.list();
        //mav存放转发到jsp页面的参数
        mav.addObject("cs",cs);//mav相当于一个hashmap
        //放入将要跳转的jsp文件名
        mav.setViewName("BookList");
        return mav;//return给框架
    }

    @RequestMapping("addBook")
    public ModelAndView addBook(Book book) {
        ModelAndView mav=new ModelAndView();
        bookService.add(book);
        //插入完成后马上回到list列表
        List<Book> cs=bookService.list();
        //mav存放转发到jsp页面的参数
        mav.addObject("cs",cs);//mav相当于一个hashmap
        //放入将要跳转的jsp文件名
        mav.setViewName("BookList");
        return mav;
    }

    @RequestMapping("deleBook")
    public ModelAndView deleBook(int bookId) {
        ModelAndView mav=new ModelAndView();
        bookService.delete(bookId);
        //插入完成后马上回到list列表
        List<Book> cs=bookService.list();
        //mav存放转发到jsp页面的参数
        mav.addObject("cs",cs);//mav相当于一个hashmap
        //放入将要跳转的jsp文件名
        mav.setViewName("BookList");
        return mav;
    }

    @RequestMapping("loadBook")
    public ModelAndView loadBook(int bookId) {
        ModelAndView mav=new ModelAndView();
        Book book=bookService.get(bookId);

        //mav存放转发到jsp页面的参数
        mav.addObject("book",book);//mav相当于一个hashmap
        //放入将要跳转的jsp文件名
        mav.setViewName("BookEdit");
        return mav;
    }

    @RequestMapping("editBook")
    public ModelAndView editBook(Book book) {
        ModelAndView mav=new ModelAndView();
        bookService.update(book);
        List<Book> cs=bookService.list();
        //mav存放转发到jsp页面的参数
        mav.addObject("cs",cs);//mav相当于一个hashmap
        //放入将要跳转的jsp文件名
        mav.setViewName("BookList");
        return mav;
    }
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页