spring整合mybatis

主要就是一个配置问题,以前单独学mybatis和spring的时候,都需要在各自的xml里配置,利用spring整合mybatis就不需要在mybatis的xml文件里去配置,只需要建出一个mybatis的xml文件,导入mybatis的dtd即可,相应的配置会放在spring-mybatis(可自定义命名)的xml文件里,然后在spring的beans.xml里引入spring-mybatis文件即可。相应的配置步骤如下:

1.首先创建一个mybatis.xml文件(如果没需要也不需要建),导入需要的dtd(约束文件)。里面什么也不需要配置。

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

2.创建一个spring的beans(可自定义名)文件,因为为用的STS,而不是eclipse,所以当创建这个beans的时候,勾选了相应的选项就会自动补充xmlns头文件,而eclipse没有这个功能。

                                                                                                 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                                                                               
    xmlns:aop="http://www.springframework.org/schema/aop"                                                                               
    xmlns:context="http://www.springframework.org/schema/context"                                                                       
    xmlns:tx="http://www.springframework.org/schema/tx"                                                                                 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd                          
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd              
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">                           
                                                                                                                     
                                                                          
                                                                                                                 
                                                                                                            
                                                                                                                         
                                                                                                 
   

3.创建并配置spring-mybatis文件,相当于以前配置在mybatis的配置现在用bean来管理。配置也无非那么几个步骤:

①.引入连接数据库的文件

②创建数据源,也就是创建一个bean

③将数据源映射到sqlSession中,配置自动扫描mapper文件。也就是创建一个bean

④再建一个bean来管理dao层,将这个bean来和上面一个bean中的mapper文件来相映射,sprin会自动去扫描dao层中的类和mapper文件映射。

 

                                                                                               
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                                                                             
    xmlns:aop="http://www.springframework.org/schema/aop"                                                                             
    xmlns:context="http://www.springframework.org/schema/context"                                                                     
    xmlns:tx="http://www.springframework.org/schema/tx"                                                                               
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd      
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd                        
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd            
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">                         
                                                                                                                                      
                                                                        
                                                                                                                 
                                                                  
                                                                                                                        
                                            
                                                                             
                                                                                            
                                                                                  
                                                                                  
    
                                                                                                                          
                                                                                                    
                                                        
                                                                
                                                            
                                                                                        
                                                                                                                 
                                                                    
    
                                                                                                                          
                                                                                                   
                                                                      
                                                                      
                                                      
    
                                                                                                                          
                                                                                                   
                              
                                                                                                                       
                                                                             
   
                                                                                                                          
                                                                                             
   

4.建dao层,实体来测试                                                                                                                         
                                                                                                                                                                                                                                                                 
                                                                                                                                        

你可能感兴趣的:(Spring)