SpringMVC集成mybatis

参考链接:https://blog.csdn.net/ljheee/article/details/76618762
在上篇的基础上集成mybatis。
1.在resources目录下新建文件夹mapper,spring。
2.添加pom依赖:


    UTF-8
    1.7
    1.7
    2.5.4

  

  
    
      com.fasterxml.jackson.core
      jackson-core
      ${jackson.version}
    
    
      com.fasterxml.jackson.core
      jackson-databind
      ${jackson.version}
    

    
      junit
      junit
      4.11
      test
    

    
      mysql
      mysql-connector-java
      5.1.39
      runtime
    
    
      c3p0
      c3p0
      0.9.1.1
    

    
    
      org.mybatis
      mybatis
      3.4.1
    
    
      org.mybatis
      mybatis-spring
      1.3.0
    

    
      org.slf4j
      slf4j-log4j12
      1.7.21
    
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
    
    
      javax.servlet.jsp
      jsp-api
      2.2
    
    
      javax.servlet
      jstl
      1.2
    
    
      org.springframework
      spring-web
      4.2.6.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.2.6.RELEASE
    
    
      org.springframework
      spring-context
      4.2.6.RELEASE
    
    
      org.springframework
      spring-test
      4.2.6.RELEASE
    
    
      org.springframework
      spring-jdbc
      4.2.6.RELEASE
    
    
      com.github.stefanbirkner
      system-rules
      1.16.1
      test
    
    
      org.aspectj
      aspectjweaver
      1.8.9
    
    
    
      org.apache.commons
      commons-lang3
      3.4
    
    
      commons-fileupload
      commons-fileupload
      1.3.1
    
  

  
    HelloWorld3
    
      
        
          maven-clean-plugin
          3.0.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.7.0
        
        
          maven-surefire-plugin
          2.20.1
        
        
          maven-war-plugin
          3.2.0
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

3.添加数据库jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mytest
username=root
password=123456

idleConnectionTestPeriod=60
maxIdleTime=240
acquireIncrement=5
initialPoolSize=10
maxPoolSize=30
minPoolSize=10
autoCommitOnClose=false
checkoutTimeout=1000
acquireRetryAttempts=2

4.建数据库表


SpringMVC集成mybatis_第1张图片
image.png

5.在resources目录下添加mybatis设置mybatis-config.xml





    
    
        
        

        

        
        
    


6.在resource/spring下建立spring-dao.xml文件



    
    
        
            
            
        
    

    

    
    

    


    

    
    
        
        

        

        
    

    
    

    
    
    
        
        
        
        
    


7.修改web.xml文件




    Archetype Created Web Application

    
    
        index.jsp
    

    
    
        springMVC
        org.springframework.web.servlet.DispatcherServlet
        
            
            contextConfigLocation
            classpath:spring/spring-dao.xml
            

        
        1
        true
    
    
        springMVC
        /
    
    
    
        contextConfigLocation
        /WEB-INF/applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
        
            forceEncoding
            true
        
    

    
        encodingFilter
        springMVC
    

8.实体类映射
先建立实体类UserModel:

package com.lml.helloworld3.pojo;

public class UserModel {
    private int id;
    private String  userName;
    private int userAge;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }
}

dao层:

package com.lml.helloworld3.dao;

import com.lml.helloworld3.pojo.UserModel;

public interface UserDao {

    UserModel findById(int id);
}

Controller:

@RestController
@RequestMapping(value = "/myuser")
public class UserController {

    @Autowired(required = false)
    private UserDao userDao;

    @RequestMapping(value = "/getUser",method = RequestMethod.GET)
    public UserModel getUserById(){
        return userDao.findById(1);
    }
}

在mapper下建立sql:







    

9.目录结构如下


SpringMVC集成mybatis_第2张图片
image.png

10.测试


SpringMVC集成mybatis_第3张图片
image.png

你可能感兴趣的:(SpringMVC集成mybatis)