SpringMvc学习笔记——SpringMvc整合Mybatis(ssm整合)

应为Spring与SpringMVC浑然一体,所以SpringMvc整合Mybatis,就是Spring与Mybatis整合,再将其搭建成WEB环境即可。

 

将Spring与Mybatis整合:

https://blog.csdn.net/hu18315778112/article/details/88091145

整合完后,在项目的web.xml中配置Spring容器和SpringMVC控制器:

  
  
  	org.springframework.web.context.ContextLoaderListener
  
  
  
  	contextConfigLocation
  	classpath:applicationContext.xml
  
  
  
  
  	encoding
  	org.springframework.web.filter.CharacterEncodingFilter
  	
  		encoding
  		UTF-8
  	
  
  
  
  	encoding
  	*.action
  
  
  
  
  	springmvc
  	org.springframework.web.servlet.DispatcherServlet
  	
  	
  		contextConfigLocation
  		classpath:springmvc.xml
  	
  
  
  
  	springmvc
  	
	*.action  
  

这样基本完成了,下面直接书写代码进行测试。

测试步骤:

  1. 创建数据库。
  2. 创建实体类(Item.java)
  3. 创建Mapper接口(ItemMapper.java)
  4. 创建mapper映射文件(ItemMapper.xml)
  5. 将映射文件添加到Mybatis容器
  6. 书写测试类——将采用注解配置,注意将测试类注册到SpringMvc容器等细节
  • 数据库:

SpringMvc学习笔记——SpringMvc整合Mybatis(ssm整合)_第1张图片

  • 实体类——Item.java:

package hh.pojo;

import java.util.Date;

public class Item {

	private Integer id;
	private String name;
	private Float price;
	private String detail;
	private Date createtime;
	
	public Item() {}
	
	public Item(Integer id, String name, Float price, String detail, Date createtime) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.detail = detail;
		this.createtime = createtime;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Float getPrice() {
		return price;
	}
	public void setPrice(Float price) {
		this.price = price;
	}
	public String getDetail() {
		return detail;
	}
	public void setDetail(String detail) {
		this.detail = detail;
	}
	public Date getCreatetime() {
		return createtime;
	}
	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}
	
}
  • Mapper接口——ItemMapper.java:

package hh.mapper;

import java.util.List;

import hh.pojo.Item;

public interface ItemMapper {

	public List selectAllItem();
}
  • mapper映射文件——ItemMapper.xml:







	
	

  • 将映射文件添加到Mybatis容器:

	
		
		
		
	
  • 测试类:

package hh.controller;

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 hh.mapper.ItemMapper;
import hh.pojo.Item;

// 注册到SpringMVC容器
@Controller
public class TestSsm {
	
//	注入Mapper对象
	@Autowired
	private ItemMapper itemMapper;
	
	@RequestMapping("/testSsm.action")
	public ModelAndView testSsm() {
		
		List itemlist = itemMapper.selectAllItem();
		
		ModelAndView mav=new ModelAndView();
		mav.addObject("itemList",itemlist);
		mav.setViewName("/WEB-INF/jsp/itemList.jsp");
		return mav;
	}

}
  • 执行结果:

SpringMvc学习笔记——SpringMvc整合Mybatis(ssm整合)_第2张图片

成功从数据库读取数据并显示,SSM框架搭建完毕!

你可能感兴趣的:(框架学习)