图书管理(spring+springMVC+mybatis整合)

对于三个框架学习以后,发现配置是一个很繁琐的过程,写一个整合记录,好让自己和需要的人学习学习.
首先用的是Oracle的数据库.
#准备工作
这是BookInfo表
图书管理(spring+springMVC+mybatis整合)_第1张图片
这是BookTypeInfo表
这是BookTypeInfo表
两个表的主键都是ID,有自增的序列.没有增加触发器.
用最简单的表来做框架的整合.
导入相关jar包
图书管理(spring+springMVC+mybatis整合)_第2张图片
#开始编程

首先创建文件夹还有基本的实体类

把接口和实现分成两个包,便于以后类多的时候查找,model里面就是放Oracle对应的实体类(对应的列名还有get、set方法)
图书管理(spring+springMVC+mybatis整合)_第3张图片
图书管理(spring+springMVC+mybatis整合)_第4张图片

编写mybatis里面对应的mapper

先写mapper文件,待会dao的实现就是使用mapper
IBookInfoMapper文件

public interface IBookInfoMapper {
	
		//增加图书信息
		public boolean addBook(BookInfo bookInfo);
		//删除图书信息
		public boolean delBook(int id);
		//修改图书信息
		public boolean editBook(BookInfo bookInfo);
		//获取图书列表
		public List getAllBook();
		//获取某本图书
		public BookInfo getBook();
}

这个是mybatis里面mapper都需要的


编写的BookInfoMapper.xml





	
	
		
		
		
		
		
		
			
			
		
	
	
	
	
	
		
		
		
		
		
		
			
		
	
	
	
	
	
		
		select seq_bookId.nextval from dual
		
		insert into bookInfo  values (#{id},#{bookTypeInfo.id},#{bookName},#{author},#{price})
	
	
	
	
		delete from BookInfo where id = #{id}
	
	
	
	
		update BookInfo set bookTypeid =#{bookTypeInfo.id},bookName=#{bookName},author=#{author},price =#{price} where id = #{id}
	

因为后面要用到图书类型的下拉框,所以就顺便吧IBookTypeInfoMapper和BookTypeInfoMapper.xml写了
IBookTypeInfoMapper文件:

public interface IBookTypeInfoMapper {
	//得到所有图书类型
	public List getBookTypeList();
}

BookTypeInfoMapper.xml文件:





 

编写dao和service(实现类加上spring注解)

IBookInfoDao和IBookInfoMapper除了类名其他都一样,这里就不写了.
BookInfoDaoImpl文件:

package dao.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import mapper.IBookInfoMapper;
import model.BookInfo;
import dao.inter.IBookInfoDao;

@Repository
public class BookInfoDaoImpl implements IBookInfoDao{

	@Autowired
	//具体由我们配置好的mybatis的mapper来实现
	private IBookInfoMapper iBookInfoMapper;
	@Override
	public boolean addBook(BookInfo bookInfo) {
		// TODO Auto-generated method stub
		return iBookInfoMapper.addBook(bookInfo);
	}

	@Override
	public boolean delBook(int id) {
		// TODO Auto-generated method stub
		return iBookInfoMapper.delBook(id);
	}

	@Override
	public boolean editBook(BookInfo bookInfo) {
		// TODO Auto-generated method stub
		return iBookInfoMapper.editBook(bookInfo);
	}

	@Override
	public List getAllBook() {
		// TODO Auto-generated method stub
		return iBookInfoMapper.getAllBook();
	}

	@Override
	public BookInfo getBook(int id) {
		// TODO Auto-generated method stub
		return iBookInfoMapper.getBook(id);
	}

	
}

BookTypeInfoDaoImpl文件:

package dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import mapper.IBookTypeInfoMapper;
import model.BookTypeInfo;
import dao.inter.IBookTypeInfoDao;

@Repository
public class BookTypeInfoDaoImpl implements IBookTypeInfoDao{

	@Autowired
	private IBookTypeInfoMapper iBookTypeInfoMapper;
	@Override
	public List getBookTypeList() {
		// TODO Auto-generated method stub
		return iBookTypeInfoMapper.getBookTypeList();
	}

}

service里面的接口也是和dao接口一样,这里就把service的接口写到一个java类里面了
IBookService文件:

public interface IBookService{
	
	//增加图书信息
	public boolean addBook(BookInfo bookInfo);
	//删除图书信息
	public boolean delBook(int id);
	//修改图书信息
	public boolean editBook(BookInfo bookInfo);
	//获取图书列表
	public List getAllBook();
	//获取某本图书
	public BookInfo getBook(int id);
	//得到所有图书类型
	public List getBookTypeList();
	
}

BookServiceImpl文件:
千万记得service注解要导入正确包。

import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements IBookService {
	@Autowired
	private IBookInfoDao iBookInfoDao;
	@Autowired
	private IBookTypeInfoDao iBookTypeInfoDao;
	@Override
	public boolean addBook(BookInfo bookInfo) {
		// TODO Auto-generated method stub
		return iBookInfoDao.addBook(bookInfo);
	}

	@Override
	public boolean delBook(int id) {
		// TODO Auto-generated method stub
		return iBookInfoDao.delBook(id);
	}

	@Override
	public boolean editBook(BookInfo bookInfo) {
		// TODO Auto-generated method stub
		return iBookInfoDao.editBook(bookInfo);
	}

	@Override
	public List getAllBook() {
		// TODO Auto-generated method stub
		return iBookInfoDao.getAllBook();
	}

	@Override
	public BookInfo getBook(int id) {
		// TODO Auto-generated method stub
		return iBookInfoDao.getBook(id);
	}

	@Override
	public List getBookTypeList() {
		// TODO Auto-generated method stub
		return iBookTypeInfoDao.getBookTypeList();
	}

}

重要的一步mybatis、spring的配置文件

src下面创建一个config存放配置文件
mybatis.xml文件:




	
		
		
	
	
		
		
	

src根目录直接放spring配置文件beans.xml省的配置
beans.xml文件(一般要改的地方就数据库的datasource还有spring注解扫描的包):



	
	
		
			oracle.jdbc.driver.OracleDriver
		
		
			jdbc:oracle:thin:@localhost:1521:ORCLDB
		
		
			scott
		
		
			root
		
	
	
	
		
		
		
	
	
	
		
		
	
	
	
	

接下来就是测试了

写了那么久终于能测试下了555.
吐血,之前配置的BookMapper.xml里面的查找某本图书少了resultMap的声明,因为一次性写的所以找了挺久的.希望读到这篇文章的童鞋一定注意不要这样一口气在mapper里面写那么多方法,可以一个一个来,不然其中一个有问题所有的都不能运行.
放上我的测试类:

public class BookTest {

	//测试新增图书信息
	@Test
	public void addBook(){
		ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
		IBookService bs = ac.getBean("bookService",IBookService.class);
		BookInfo bi  = new BookInfo();
		bi.setAuthor("藤子不二雄3");
		bi.setBookName("机器猫");
		BookTypeInfo bti = new BookTypeInfo();
		bti.setId(5);
		bi.setBookTypeInfo(bti);
		bi.setPrice(35.5);
		try{
			bs.addBook(bi);
			System.out.println("ok");
		}catch(Exception e ){
			e.printStackTrace();
		}
	}
	//测试更改图书信息
	@Test
	public void updateBook(){
		ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
		IBookService bs = ac.getBean("bookService",IBookService.class);
		BookInfo bi  = new BookInfo();
		bi.setId(5);
		bi.setAuthor("测试3");
		bi.setBookName("机器猫");
		BookTypeInfo bti = new BookTypeInfo();
		bti.setId(5);
		bi.setBookTypeInfo(bti);
		bi.setPrice(35.5);
		try{
			bs.editBook(bi);
			System.out.println("ok");
		}catch(Exception e ){
			e.printStackTrace();
		}
	}
	//测试查询所有图书
	@Test
	public void selAllBook(){
		ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
		IBookService bs = ac.getBean("bookService",IBookService.class);
		try{
			List li =bs.getAllBook();
			for(BookInfo bi:li){
				System.out.println(bi.getAuthor()+"   :"+bi.getBookName()+"  "+bi.getBookTypeInfo().getBookTypeName());
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	//测试查找指定图书
	@Test
	public void getOneBook(){
		ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
		IBookService bs = ac.getBean("bookService",IBookService.class);
		try{
			BookInfo bi =null;
			bi =bs.getBook(2);
			if(bi!=null)
			System.out.println(bi.getAuthor()+"   :"+bi.getBookName());
			else 
				System.out.println("kong ");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	//测试删除特定图书
	@Test
	public void delBook(){
		ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
		IBookService bs = ac.getBean("bookService",IBookService.class);
		try{
			boolean flag =false;
			flag =bs.deleteBook(5);
			if(flag)
				System.out.println("删除成功");
			else 
				System.out.println("删除失败");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	@Test
	public void  getAllType(){
		ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
		IBookService bs = ac.getBean("bookService",IBookService.class);
		try{
			List list = bs.getAllType();
			for(BookTypeInfo bti :list){
				System.out.println(bti.getId()+"  :" +bti.getBookTypeName());
			}
		}catch(Exception e ){
			e.printStackTrace();
		}
	}
}

接下来就准备到视图

配置spring、springMVC有关的web.xml##

 
  

	org.springframework.web.context.ContextLoaderListener

  
  
  
  	contextConfigLocation
  	classpath*:beans.xml
  
  
  
  	
  		org.springframework.web.util.IntrospectorCleanupListener
  	
  
  
  
	  springMVC
	  
		org.springframework.web.servlet.DispatcherServlet
	  
	  
	  
		  contextConfigLocation
		  classpath*:config/spring-mvc.xml
	  
	  
	  1
  
  
  
  
  springMVC
  /
  

  
  
  encodingFilter
  org.springframework.web.filter.CharacterEncodingFilter
  
  encoding
  utf-8
  
  
  
  encodingFilter
  /*
  

接下来写springMVC的配置文件
因为我在web.xml配置了文件名,文件路径,所以就不需要有配置文件的默认限制了.
在配置好的config里面配置spring-mvc.xml:



	
	
	 
	 
	 
	 
	 
     
     
	 
	 
	 
	 
	 
	 
	 

写对应的jsp页面

book_add.jsp:


新增图书

书名作者价格图书类型

book_edit.jsp:




Insert title here




修改图书${book.bookTypeInfo.id }

书名作者价格图书类型

book_list.jsp:



		
书名作者价格类型操作
${book.bookName} ${book.author} ${book.price} ${book.bookTypeInfo.bookTypeName} 编辑 删除
新增

failed_delete.jsp:




error


	

Delete Book Failed

springMVC的controller

src创建controller包,创建BookController

package controller;

import java.util.List;
import model.BookInfo;
import model.BookTypeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import service.inter.IBookService;


@RequestMapping("book")
@Controller
public class BookController {
	
	@Autowired
	private IBookService bookService;
	@RequestMapping("bookList")
	/**
	 * 获取图书列表并转到页面
	 * @return
	 */
	public ModelAndView showList(){
		List li = null;
		try{
			li =bookService.getAllBook();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return new ModelAndView("book_list","list",li);
	}
	
	@RequestMapping("toaddBook")
	/**
	 * 获取所有图书类型并转到页面
	 * @return
	 */
	public ModelAndView toAddBookView(){
		List li=null;
		try{
			li =bookService.getBookTypeList();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return new ModelAndView("book_add","list" ,li);
	}
	
	@RequestMapping("addBook")
	/**
	 * 图书新增
	 * @param bi
	 * @return
	 */
	public String  toAddBookView(BookInfo bi){
		try{
			System.out.println(bi.getBookName());
			bookService.addBook(bi);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "redirect:bookList";
	}
	@RequestMapping("toEditBook")
	/**
	 * 这个方法用来把图书对象加入到request然后转到toEdit
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	public String toEditBook(int id ,RedirectAttributes redirectAttributes){
		
		BookInfo bi =bookService.getBook(id);
		//相当于在这次的request里面加一个属性
		redirectAttributes.addFlashAttribute("book",bi);
		
		return "redirect:toEdit";
	}
	@RequestMapping("toEdit")
	/**
	 * 获取图书类型然后转到book_edit页面
	 * @return
	 */
	public ModelAndView toEditBook(){
		List li=null;
		try{
			li =bookService.getBookTypeList();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return new ModelAndView("book_edit","list" ,li);
	}
	@RequestMapping("editBook")
	@ResponseBody
	/**
	 * 修改方法
	 * @param book
	 * @return
	 */
	public String editBook(BookInfo book){
		try{
			bookService.editBook(book);
			return "succeed";
		}catch(Exception e){
			e.printStackTrace();
		}
		return "falied";
	}
	@RequestMapping("delBook")
	/**
	 * 删除方法
	 * @param id
	 * @return
	 */
	public String delBook(int id){
		boolean flag=false;
		try{
			bookService.delBook(id);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		if(flag){
			return "redirect:bookList";
		}else{
			return "failed_delete";
		}
	}
}

最后把我的包和类都截图下
图书管理(spring+springMVC+mybatis整合)_第5张图片
整合到这完毕.功能测试成功!哈哈.不过码代码累啊,第一次写整合帖,还出错了.不过经验也学到了,希望能给需要的童鞋一点启发.整个项目我会发上来.给需要的人.
不知道怎么取消下载的积分,=-= 太久没来博客。sorry
源码地址: https://download.csdn.net/download/a526059967/9704924#comment

或者百度云链接:https://pan.baidu.com/s/15xLbN_x5mr68A-jxpUQs2A 提取码:BYcG

你可能感兴趣的:(java,ssm框架整合,spring,springMVC,mybatis)