Spring MVC、Spring和Mybatis整合环境搭建

阅读更多

一、基础环境准备

 

1. 创建Java Web工程,引入依赖的相关jar

    commons-logging-1.1.jar

    mybatis-3.3.0.jar

    mybatis-spring-1.2.4.jar

    mysql-connector-java-5.0.2.jar

    spring-aop-4.3.2.RELEASE.jar

    spring-beans-4.3.2.RELEASE.jar

    spring-context-4.3.2.RELEASE.jar

    spring-context-support-4.3.2.RELEASE.jar

    spring-core-4.3.2.RELEASE.jar

    spring-expression-4.3.2.RELEASE.jar

    spring-jdbc-4.3.2.RELEASE.jar

    spring-tx-4.3.2.RELEASE.jar

    spring-web-3.2.4.RELEASE.jar

    spring-webmvc-3.2.4.RELEASE.jar

 

2. 在mysql中创建数据库coredb,并创建测试表user

 

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userName` varchar(50) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', 'tuozixuan', '111111');

 

 

 

二、编写示例代码

1. 创建若干包

    com.tuozixuan.one.controller

    com.tuozixuan.one.service

    com.tuozixuan.one.service.impl

    com.tuozixuan.one.mapper

    com.tuozixuan.one.model

 

2. 编写示例代码

 

#############UserController#############

package com.tuozixuan.one.controller;

 

import javax.annotation.Resource;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

 

import com.tuozixuan.one.service.UserService;

 

@Controller

public class UserController

{

    @Resource

    private UserService userService;

    

    @RequestMapping("/getUser")

    public ModelAndView hello()

    {

        System.out.println("getUser start");

        userService.getUser();

        System.out.println("getUser end");

        ModelAndView mv = new ModelAndView("user");

        return mv;

    }

}

 

#############UserService#############

package com.tuozixuan.one.service;

 

import com.tuozixuan.one.model.UserModel;

 

public interface UserService

{

    UserModel getUser();

}

 

#############UserServiceImpl#############

package com.tuozixuan.one.service.impl;

 

import java.util.List;

 

import javax.annotation.Resource;

 

import org.springframework.stereotype.Service;

 

import com.tuozixuan.one.mapper.UserMapper;

import com.tuozixuan.one.model.UserModel;

import com.tuozixuan.one.service.UserService;

 

@Service

public class UserServiceImpl implements UserService

{

    @Resource

    private UserMapper userMapper;

    

    public UserModel getUser()

    {

        

        List userList = userMapper.getUser();

        if (userList.size() > 0)

        {

            System.out.println("user:" + userList.get(0).getUserName());

        }

        

        return null;

    }

    

}

 

 

#############UserMapper#############

package com.tuozixuan.one.mapper;

 

import java.util.List;

 

import com.tuozixuan.one.model.UserModel;

 

public interface UserMapper

{

    List getUser();

}

 

 

#############UserModel#############

package com.tuozixuan.one.model;

 

public class UserModel

{

    private String userName;

 

    public String getUserName()

    {

        return userName;

    }

 

    public void setUserName(String userName)

    {

        this.userName = userName;

    }

}

 

#############UserMapper.xml#############

"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 

 

 

 

 

 

#############user.jsp#############

在WebRoot下创建user.jsp,内容任意

 

 

 

三、SSM整合配置文件

1. web.xml配置如下:

 



  	
  
    index.jsp
  
  
  	
	  
        contextConfigLocation  
        classpath:spring.xml  
  	
  	
  	
  	
	 	org.springframework.web.context.ContextLoaderListener
	
  
  	
	
		springMvc
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
		1
	
	
		springMvc
		/
	

 

 

2. 在src目录下新建spring-mvc.xml配置文件

 



     
    
	
	
    
    
    
    
    
    
    
      
    	  
    	  
	  

  

 

3. 在src目录下新建spring.xml配置文件

 


	



	
	
		
		
		
		
	
	
	
	
		
		
		
	
	
	
	
    	
    	
  	
	

 

 

 

4. 在src目录下新建mybatis-config.xml配置文件

 





 

 

四、运行与结果

1. 把web工程部署到tomcat里,在浏览器中访问http://localhost:8080/one/getUser,发现可以访问到user.jsp, 并在控制台打印出了日志内容:

getUser start

user:tuozixuan

getUser end

 

你可能感兴趣的:(Spring,MVC,Mybatis,ssm,整合,搭建)