当前位置:Gxlcms > mysql > javaWeb项目SpringMVC3.2.1与Mybatis3.0.4整合实例(Mybaits

javaWeb项目SpringMVC3.2.1与Mybatis3.0.4整合实例(Mybaits

时间:2021-07-01 10:21:17 帮助过:35人阅读

网上有许多关于SpringMVC与Mybatis整合的实例,但是由于Mybaits-spring有好几种配置方式,这样看起来会觉得很乱,不知道哪个是对的,觉得很复杂(至少我的感觉是这样的)。把关于这块的学习过程记录下来,留作备注同时与大家分享交流。 工具 :eclipse-luna、m

网上有许多关于SpringMVC与Mybatis整合的实例,但是由于Mybaits-spring有好几种配置方式,这样看起来会觉得很乱,不知道哪个是对的,觉得很复杂(至少我的感觉是这样的)。把关于这块的学习过程记录下来,留作备注同时与大家分享交流。

工具:eclipse-luna、mysql数据库

SpringMVC版本:3.2.1

Mybatis版本:3.0.4

Mybaits-spring版本:

服务器:tomcat7.0.57

文件结构


项目所使用的lib包截图:


我们按照javaWeb项目在服务器中执行顺序(web项目首先加载web.xml文件)来说:

web.xml文件:在文件中,我们需要配置Spring的监听器ContextLoaderListener和SpringMVC的转发器DispatcherServlet,在web.xml中加入如下代码

  
  
  	
		org.springframework.web.context.ContextLoaderListener
  	
  
  
  
  	
		contextConfigLocation
		/WEB-INF/spring-applicationContext.xml
  	
  
  
  
  	encodingFilter
  	org.springframework.web.filter.CharacterEncodingFilter
  	
  		encoding
  		utf-8
  	
  
  
  
  	encodingFilter
  	*.yy
  
  
  
  	encodingFilter
  	*.jsp
  
  
  
  
        springmvc
        org.springframework.web.servlet.DispatcherServlet
	        
	        	 contextConfigLocation
	      		 /WEB-INF/spring-dispatcher.xml
	        
        1
  

  
        springmvc
        *.yy
  
  

在web.xml文件中,包含spring的配置文件:spring-applicationContext.xml ;SpringMVC的配置文件:spring-dispatcher.xml。

spring-dispatcher.xml文件:在文件中加入如下代码


      
    
    
        
        
            org.springframework.web.servlet.view.JstlView
        
        
        
    


spring-applicationContext.xml文件在文件中加入如下代码

 
             
      
    
    
    
	
		
			${driverClassName}
		
		
			${password}
		
		
			${username}
		
		
			${url}
		
	
	
	
文件中包含数据库连接池datasource的配置文件:jdbc.properties;spring与mybatis结合的配置文件:spring-mybatis.xml。spring-mybatis.xml文件的内容可以都放在spring.xml文件中,合成一个spring.xml文件和分成两个文件各有各的好处,看你的习惯

jdbc.properties文件在文件中加入如下代码

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/你的数据库名字
username=root
password=root

spring-mybatis.xml文件在文件中加入如下代码

  
	
	
	
	
		
			classpath:com/yy/mapper/**/*Mapper.xml
		
	

	

	
文件中包含mybatis的配置文件:mybatis-config.xml

mybatis-config.xml文件在文件中加入如下代码


配置文件到这里就配置完了,下面我们说一下controller、service、dao、model四层的代码

controller层:

package com.yy.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yy.model.Order;
import com.yy.service.BuyOrderService;

@Controller
@RequestMapping("buyOrder")
public class BuyOrderController {
	
	@Resource
	private BuyOrderService buyOrderService;
	
	@RequestMapping("/addBuyOrder")
	public ModelAndView addBuyOrder(HttpServletRequest resquest,HttpServletResponse response){
		return new ModelAndView("/test");
	}
	
	@RequestMapping("/deleteBuyOrder")
	public ModelAndView deleteBuyOrder(HttpServletRequest resquest,HttpServletResponse response){
		return new ModelAndView("/test");
	}
	
	@RequestMapping("/getBuyOrderById")
	public ModelAndView getBuyOrderById(int id,HttpServletRequest resquest,HttpServletResponse response){
		Order order = buyOrderService.getBuyOrderById(id);
		System.out.println(order.getId()+"+++"+order.getId());
		return new ModelAndView("/test");
	}
	
	@RequestMapping("/getBuyOrderList")
	public ModelAndView getBuyOrderList(HttpServletRequest resquest,HttpServletResponse response){
		return new ModelAndView("/test");
	}
}

service

package com.yy.service;

import com.yy.model.Order;

public interface BuyOrderService {

	public Order getBuyOrderById(int id);
	
	public void addBuyOrder(Order order);
}
serviceImpl
package com.yy.service.Impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yy.dao.BuyOrderDao;
import com.yy.model.Order;
import com.yy.service.BuyOrderService;

@Service
public class BuyOrderServiceImpl implements BuyOrderService{
	
	@Resource
	private BuyOrderDao buyOrderDao; 

	@Override
	public Order getBuyOrderById(int id) {
		return buyOrderDao.getBuyOrderById(id);
	}

	@Override
	public void addBuyOrder(Order order) {
		buyOrderDao.addBuyOrder(order);
	}

}

dao

package com.yy.dao;

import com.yy.model.Order;

public interface BuyOrderDao {

	Order getBuyOrderById(int id);

	void addBuyOrder(Order order);

}

mapper映射器类、映射器xml文件

映射器类文件

package com.yy.mapper;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.yy.dao.BuyOrderDao;
import com.yy.model.Order;

@Repository
public class BuyOrderMapper extends SqlSessionDaoSupport implements BuyOrderDao {

	@Override
	public Order getBuyOrderById(int id) {
		return (Order)this.getSqlSession().selectOne("com.yy.mapper.BuyOrderMapper.getBuyOrderById", id);
	}

	@Override
	public void addBuyOrder(Order order) {
		this.getSqlSession().insert("org.mybatis.spring.com.yy.mapper.addBuyOrder",order);
	}
}
映射器xml文件




	

model

package com.yy.model;

import java.sql.Date;

public class Order {
	private int id;
	private String code;
	private String username;
	private String tel;
	private String addresss;
	private Date create_time;
	
	public Order(){}
	
	public Order(int id, String code, String username, String tel,String addresss, Date create_time) {
		super();
		this.id = id;
		this.code = code;
		this.username = username;
		this.tel = tel;
		this.addresss = addresss;
		this.create_time = create_time;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getAddresss() {
		return addresss;
	}
	public void setAddresss(String addresss) {
		this.addresss = addresss;
	}
	public Date getCreate_time() {
		return create_time;
	}
	public void setCreate_time(Date create_time) {
		this.create_time = create_time;
	}
}

前台jsp文件:

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>






Insert title here


	欢迎
test.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>





Insert title here


	test

数据sql文件

DROP TABLE IF EXISTS `order1`;
CREATE TABLE `order1` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `code` varchar(20) DEFAULT NULL,
  `username` varchar(20) DEFAULT NULL,
  `tel` varchar(20) DEFAULT NULL,
  `address` varchar(20) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of order1
-- ----------------------------
INSERT INTO `order1` VALUES ('1', '001', 'yy', '123456', '亚洲', '2014-12-16 16:19:51');
INSERT INTO `order1` VALUES ('2', '002', 'yyy', '654321', '欧洲', '2014-12-24 16:21:04');

到这里,代码就全部呈现了。哪里的细节你觉得不明白,欢迎交流

人气教程排行