SpringMVC和Mybatis的整合的工作原理


4.SpringMVC和Mybatis的整合
在做这个项目之前,我们的前提条件是:你已经有一个能运行的SpringMVC的最简单的项目了。请同学们参考我的SpringMVC那章。做这个项目关键在于导包,请参考下图:




SpringMVC和Mybatis的整合的工作原理

马克-to-win: org.mybatis.spring.mapper.MapperScannerConfigurer会自动往Spring容器注入com.mapper目录下所有的Mapper比如RegisterMapper(这就是为什么在Controller当中你能用@Resource注解找到RegisterMapper的实例的原因),条件是org.mybatis.spring.SqlSessionFactoryBean类的实例已经在Spring容器中(实际上id是多少都无所谓,这里就是sqlSessionFactoryqqqq,因为反正也没有人调用它,只要有实例在容器中就行),有了它,MapperScannerConfigurer才能正常工作,因为SqlSessionFactoryBean的一个属性是dataSource,它掌握着连接数据库的密码等。


例 1.4



    Spring 3.0


    Say Hello




spring-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
            base-package="com" />
 
            class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                    value="org.springframework.web.servlet.view.JstlView" />
       
       
   

   
 
更多内容请见原文,原文转载自:https://blog.csdn.net/qq_44639795/article/details/100163517