springmvc+spring+mybatis实现增删改查

本篇继上一篇springmvc+spring+mybatis框架的搭建

 首先编写SQL语句

BookMpper.xml





    
    
    
    
    
    
    
    
    	insert into tb_book 
    	(bookName)values(#{bookName})
    
    
    
    
    	delete from tb_book where id = #{id}
    
    
    
    
    	update tb_book set 
    	bookName=#{bookName} where id=#{id}
    
    

BookDao.java

package com.zjq.ssm.dao;

import java.util.List;
import com.zjq.ssm.entity.Book;

public interface BookDao {
	//查找数据
	public List getAllBook();
	//插入数据
	public void insertBook(Book book);
	//删除数据
    public void deleteBookById(int id);
    //根据ID查找
    public Book findById(int id);
    //更新数据
    public void updateBook(Book book);
}

 BookService.java

 

package com.zjq.ssm.service;

import java.util.List;

import com.zjq.ssm.entity.Book;

public interface BookService {

	// 查找数据
    public List getAllBook();
    //插入数据
    public void insertBook(Book book);
    //删除数据
    public void deleteBookById(int id);
    //根据ID查找
    public Book findById(int id);
    //更新数据
    public void updateBook(Book book);
}

BookServiceImpl.java

package com.zjq.ssm.impl;

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

import org.springframework.stereotype.Service;

import com.zjq.ssm.dao.BookDao;
import com.zjq.ssm.entity.Book;
import com.zjq.ssm.service.BookService;

@Service
public class BookServiceImpl implements BookService{
	@Resource
	BookDao bookDao;
	
	@Override
	public List getAllBook() {	
		return bookDao.getAllBook();
	}

	@Override
	public void insertBook(Book book) {
		// TODO Auto-generated method stub
		bookDao.insertBook(book);
	}

	@Override
	public void deleteBookById(int id) {
		// TODO Auto-generated method stub
		bookDao.deleteBookById(id);
	}

	@Override
	public Book findById(int id) {
		// TODO Auto-generated method stub
		return bookDao.findById(id);
	}

	@Override
	public void updateBook(Book book) {
		// TODO Auto-generated method stub
		bookDao.updateBook(book);
	}
}

查询操作

先写个跳转界面

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




Insert title here


	跳转

findAll.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>




图书管理




    

图书管理

编号 书名 修改 删除
${book.id} ${book.bookName} 修改 删除


添加

 

/**
	 * 查找所有书本
	 * @param model
	 * @return
	 */
	@RequestMapping("/getAllBook")
	public String getAllBook(Model model) {
		List books = bookService.getAllBook();
		for (Book book : books) {
			System.out.println(book.getBookName());
		}
		
		model.addAttribute("books", books);
		return "findAll";
	}

实现添加操作

insert.jsp

书名:
/**
	 * 跳转到添加界面
	 * @return
	 */
	@RequestMapping(value="insert")
	public String insert() {
		return "insert";
	}
	
	/**
	 * 添加书本
	 * @param book
	 * @return
	 */
	@RequestMapping(value="/insertBook", method=RequestMethod.POST)
	public String insertBook(@Validated Book book) {
		bookService.insertBook(book);
		return "redirect:/Book/getAllBook";
	}

在web.xml文件下设置字符过滤器,否则中文将无法存储


	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
		
			encoding
			UTF-8
		
		
		
			forceEncoding
			true
		
	
	
		encodingFilter
		/*
	

 删除操作

/**
	 * 根据ID删除书本
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/deleteBookById/{id}")
	public String deleteBookById(@PathVariable("id") int id) {
		bookService.deleteBookById(id);
		return "redirect:/Book/getAllBook";
	}

更新操作

insert.jsp(在原有基础上添加)


	

书名:
/**
	 * 跳转到更新界面
	 * 数据回传
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/toupdate/{id}")
	public String ToUpdate(@PathVariable("id") int id, Map map) {
		Book book = bookService.findById(id);
		map.put("book", book);
		return "insert";
	}
	
	/**
	 * 更新书本
	 * @param book
	 * @return
	 */
	@RequestMapping(value="/updateBook")
	public String updateBook(Book book) {
		bookService.updateBook(book);
		return "redirect:/Book/getAllBook";
	}

完整的HomeController.java

package com.zjq.ssm.controller;

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

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.zjq.ssm.entity.Book;
import com.zjq.ssm.service.BookService;


@Controller
@RequestMapping("/Book")
public class HomeController {
	
	@Resource
	@Autowired
	BookService bookService;

	/**
	 * 查找所有书本
	 * @param model
	 * @return
	 */
	@RequestMapping("/getAllBook")
	public String getAllBook(Model model) {
		List books = bookService.getAllBook();
		for (Book book : books) {
			System.out.println(book.getBookName());
		}
		
		model.addAttribute("books", books);
		return "findAll";
	}
	
	/**
	 * 跳转到添加界面
	 * @return
	 */
	@RequestMapping(value="insert")
	public String insert() {
		return "insert";
	}
	
	/**
	 * 添加书本
	 * @param book
	 * @return
	 */
	@RequestMapping(value="/insertBook", method=RequestMethod.POST)
	public String insertBook(@Validated Book book) {
		bookService.insertBook(book);
		return "redirect:/Book/getAllBook";
	}
	
	/**
	 * 根据ID删除书本
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/deleteBookById/{id}")
	public String deleteBookById(@PathVariable("id") int id) {
		bookService.deleteBookById(id);
		return "redirect:/Book/getAllBook";
	}
	
	/**
	 * 根据ID查找书本
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/findById")
	public String findById(Model model, int id) {
		Book book = bookService.findById(id);
		model.addAttribute("book", book);
		return "redirect:/Book/getAllBook";
	}
	
	/**
	 * 跳转到更新界面
	 * 数据回传
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/toupdate/{id}")
	public String ToUpdate(@PathVariable("id") int id, Map map) {
		Book book = bookService.findById(id);
		map.put("book", book);
		return "insert";
	}
	
	/**
	 * 更新书本
	 * @param book
	 * @return
	 */
	@RequestMapping(value="/updateBook")
	public String updateBook(Book book) {
		bookService.updateBook(book);
		return "redirect:/Book/getAllBook";
	}
	
}

完整的insert.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




Insert title here


	
书名:


书名:

 

你可能感兴趣的:(springmvc学习,mybatis学习)