Spring MVC + Mybaties 集成

环境和工具

  1. Java版本:
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
  1. Maven版本:
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-12+0800)
Maven home: D:\maven3\bin\..
Java version: 1.7.0_80, vendor: Oracle Corporation
Java home: D:\Java\jdk1.7.0_80\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
  1. Eclipse版本:
Version: Mars.2 Release (4.5.2)
Build id: 20160218-0600

创建maven web项目

项目创建和目录

利用Eclipse创建一个Maven project,在Select an Archetype时,选择maven-archetype-webapp,填好groupId和artifactId,完成创建后查看项目。如果项目报错,根据提示修改jre并添加tomcat的library。

Maven项目的目录说明:

目录 说明
src/main/java Application/Library sources(源代码)
src/main/resources Application/Library resources(资源文件)
src/main/filters Resource filter files
src/main/webapp Web application sources(html、js等文件)
src/test/java Test sources
src/test/resources Test resources
src/test/filters Test resource filter files
src/it Integration Tests (primarily for plugins)
src/assembly Assembly descriptors
src/site Site
LICENSE.txt Project's license
NOTICE.txt Notices and attributions required by libraries that the project depends on
README.txt Project's readme

项目结构和代码

主要结构和配置文件:


modele.com.base
--controller
--dao
--entity
--service
    --impl
    
src/main/resources
--/spring-mvc.xml
--/spring-mybatise.xml
--/config/config.properties
--/mapper/*.xml

下面是配置文件的内容,文章的最后贴上测试代码。

修改配置文件

pom.xml

主要是添加Spring和Mybaties等依赖。


  4.0.0
  com.project
  model
  war
  0.0.1-SNAPSHOT
  model Maven Webapp
  http://maven.apache.org
  
    
    
        
        4.11
    
        
        4.0.8.RELEASE
        
        
        5.1.30
        
        3.2.8
        1.2.2
        
        
        1.0.11

    

    
        
            junit
            junit
            ${junit.version}
            test
        

        
            javax.servlet
            jstl
            1.2
            jar
        
        
            javax.servlet
            servlet-api
            2.5
            provided
        
        
            javax.servlet.jsp
            jsp-api
            2.1
            provided
        

        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
        
        
            mysql
            mysql-connector-java
            ${mysql.driver.version}
        

        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
            org.mybatis
            mybatis-spring
            ${mybatis-spring.version}
        
    
        
        
            com.alibaba
            druid
            ${druid.version}
        
    
        
        
            commons-dbcp
            commons-dbcp
            1.4
        
    
        
            commons-pool
            commons-pool
            1.6
        
    
    
    
  
    model
  


web.xml

Path:src/main/webapp/WEB-INF/web.xml




    Archetype Created Web Application

    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        true
        
            encoding
            UTF-8
        
    
    
    
        encodingFilter
        /*
    
 
    
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        1
        true
    
    
    
        SpringMVC
        
        /
    
    
    
        /index.jsp
      
    


config.properties

注意不要有空格。

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
username=root
password=root
#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570  
initialSize=0  
#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570  
maxActive=20  
#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2  
maxIdle=20  
#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2  
minIdle=1  
#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4  
maxWait=60000 

spring-mvc.xml

Path:src/main/resources/spring-mvc.xml




    
    

    
    

    
    
    
    
    
    
    
    
        
        
    


spring-mybatise.xml

Path: src\main\resources\spring-mybatise.xml

  
  
      
      
          
      
  
      
          
          
          
          
          
          
          
          
          
          
          
          
          
          
      
  
      
      
          
          
          
      
  
      
      
        
        
          
      
  
      
      
          
      
  
 

测试

实现功能:根据id查询记录的详细信息。

数据库表:

CREATE TABLE `user_t` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(40) NOT NULL,
  `password` varchar(255) NOT NULL,
  `age` int(4) NOT NULL,
  PRIMARY KEY (`id`)
)

测试Entity

UserEntity.java

package model.com.base.entity;

public class UserEntity {
    
    private int id;
    private String name;
    private String password;
    private int age;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        
        return "id:"+this.id+",name:"+this.name+",age:"+this.age;
    }
    
}

查询 sql

UserDao.java

package model.com.base.dao;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import model.com.base.entity.UserEntity;

@Repository
public interface UserDao {

    UserEntity selectByPrimaryKey(@Param("id")int userId);
}

user.xml





    
        
        
        
        
    
    
    
    

Service

UserService.java

package model.com.base.service;

import model.com.base.entity.UserEntity;

public interface UserService {

    public UserEntity getUserById(int userId);
}

UserServiceImpl.java

package model.com.base.service.impl;

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

import model.com.base.dao.UserDao;
import model.com.base.entity.UserEntity;
import model.com.base.service.UserService;

@Service
public class UserServiceImpl implements UserService{
    
    @Autowired
    private UserDao userDao;

    public UserEntity getUserById(int userId) {
        return this.userDao.selectByPrimaryKey(userId);  
    }

}

请求处理

UserController.java

package model.com.base.controller;

import java.util.HashMap;
import java.util.Map;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import model.com.base.entity.UserEntity;
import model.com.base.service.impl.UserServiceImpl;

@Controller
public class UserController{

    @Autowired
    private UserServiceImpl userService;
    
    /**
     * 根据id查询用户信息
     * 
     * @param id
     * @return
     */
    @RequestMapping(value="system/select")
    @ResponseBody
    public String selectDate(@RequestParam("id")int id){
        System.out.println("------------request----------id:"+id);
        
        UserEntity user = userService.getUserById(id);
        
        
        return user.toString();
    }
    
}


结果

浏览器地址栏输入:

http://localhost:8080/model/system/select?id=1

展示结果:

id:1,name:test,age:24

你可能感兴趣的:(Spring MVC + Mybaties 集成)