Mybatis(四)--Spring集成Mybatis

强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan

一、创建zh-root项目(用来管理依赖包的版本)

       1.使用idea开发工具新建maven项目,坐标如下:

 

  zh.root
  zh-root
  1.0-SNAPSHOT

       2.添加对应依赖及版本

 

 



  4.0.0
  zh.root
  zh-root
  1.0-SNAPSHOT
  pom

  
    
    UTF-8
    UTF-8
    
    
    UTF-8
    
    1.8
    
    1.8
    
    3.7.0
    
    4.12
    
    1.2.17
    5.0.2.RELEASE
    1.8.13
    2.9.2
    3.4.5
    1.3.1
    5.1.38
    1.1.6
  

  
    
      
      
        junit
        junit
        ${junit.version}
      
      
      
        log4j
        log4j
        ${log4j.version}
      
      
      
        org.springframework
        spring-core
        ${spring.version}
      
      
        org.springframework
        spring-context
        ${spring.version}
      
      
      
        org.springframework
        spring-jdbc
        ${spring.version}
      
      
      
        org.springframework
        spring-tx
        ${spring.version}
      
      
      
        org.springframework
        spring-aop
        ${spring.version}
      
      
      
        org.aspectj
        aspectjweaver
        ${aspectj.version}
      
      
      
        org.springframework
        spring-web
        ${spring.version}
      
      
      
        org.springframework
        spring-webmvc
        ${spring.version}
      
      
      
        com.fasterxml.jackson.core
        jackson-databind
        ${jackson.version}
      
      
      
        org.mybatis
        mybatis
        ${mybatis.version}
      
      
        org.mybatis
        mybatis-spring
        ${mybatis-spring.version}
      
      
      
        mysql
        mysql-connector-java
        ${mysql.version}
      
      
      
        com.alibaba
        druid
        ${druid.version}
      
    
  

  
    
      
        
        org.apache.maven.plugins  
        maven-compiler-plugin 
        ${maven-compiler-plugin.version}
        
            
          ${maven.compiler.source}        
          ${maven.compiler.target}
          ${project.build.sourceEncoding}      
        
      
    
  

 

二、创建mybatis-spring-zh(我使用的是quickstart)

      1.使用idea开发工具新建maven项目.坐标如下:

 

  zh.mybatis-spring
  mybatis-spring
  1.0-SNAPSHOT

     2.修改pom.xml中的打包方式为war

 

 

war

     3.新建web.xml文件(目录:src/main/webapp/WEB-INF/web.xml),添加约束如下:

 

 




     4.在pom.xml中添加相关依赖

 

 


  
    zh.root
    zh-root
    1.0-SNAPSHOT
  

  4.0.0
  zh.mybatis-spring
  mybatis-spring
  1.0-SNAPSHOT
  war

  
    
    
      junit
      junit
      test
    

    
    
      log4j
      log4j
    

    
    
      org.springframework
      spring-core
    

    
      org.springframework
      spring-context
    

    
    
      org.springframework
      spring-jdbc
      5.0.2.RELEASE
    

    
    
      org.springframework
      spring-tx
    

    
    
      org.springframework
      spring-aop
    

    
    
      org.aspectj
      aspectjweaver
    

    
    
    
      org.springframework
      spring-web
    
    
    
      org.springframework
      spring-webmvc
    

    
    
      com.fasterxml.jackson.core
      jackson-databind
    

    
    
      org.mybatis
      mybatis
    


    
      org.mybatis
      mybatis-spring
    

    
    
      mysql
      mysql-connector-java
    
    
    
      com.alibaba
      druid
    
  

     5.项目的完整路径如下,第一步先创建项目的基本结构,controller,mapper,model,service

 

                                              Mybatis(四)--Spring集成Mybatis_第1张图片
 

      6.创建springmvc的配置文件spring-context.xml,配置自动扫描

 





    

    
    

    
        
        
        
    

    
    

       7.创建db.properties文件,配置数据库连接的相关参数

 

 

jdbc_driverClassName=com.mysql.cj.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc_username=root
jdbc_password=

      8.创建dataSource.xml,连接数据库配置

 

 




    
    
        
            
        
    

    
        
        
        
    

        9.创建mybatis配置文件mybatis-config.xml,用于mybatis的常用配置

 

 




    
        
        
        
        
    

     10.创建mybatis和spring整合的配置文件,spring-mybatis.xml

 

 




    
    

    
    
        
        
        
            
                classpath:mapper/*.xml
            
        
        
    

    
    
        
        
    

       11.完善web.xml

 

 



    
        
        contextConfigLocation
        classpath:spring/spring-context.xml
    

    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        mybatis
        
            org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring/spring-context.xml
        
        1
    
    
        mybatis
        /
    

      12.写一个小例子测试一下是否成功了,以查询用户为例,分别创建UserMapper.xml,UserService,UserController,SysUser,访问该测试方法,查询出如下结果。相关程序代码已上传到码云,可以参考,https://gitee.com/ZhangHuan8/spring-mybatis.git

 

Mybatis(四)--Spring集成Mybatis_第2张图片
     关于为什么要抽出一个root项目管理版本,可以参考以下博客:http://blog.csdn.net/liutengteng130/article/details/46991829




 

 




 

       

你可能感兴趣的:(------Mybatis,Mybatis入门)