SpringMVC和Mybatis的整合的工作原理

SpringMVC和Mybatis的整合 
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203
在做这个项目之前,我们的前提条件是:你已经有一个能运行的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

<html>
<head>
    <title>Spring 3.0</title>
</head>
<body>
    <a href="helloa.do">Say Hello</a>
</body>
</html>


spring-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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">
 
    <context:component-scan
        base-package="com" />
 
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
         <property name="driverClassName"
                      value="com.mysql.jdbc.Driver"></property>
         <property name="url"
                      value="jdbc:mysql://localhost:3306/test"></property>
         <property name="username"
                      value="root"></property>
         <property name="password"
                      value="1234"></property>
    </bean>
<!-- 通过扫描的模式,扫描目录在com.mapper目录下,把所有mapper都注入到Spring当中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mapper" />
    </bean>
<!-- 没有以下的话,所有的mapper都不能创建,服务器启动过程报错-->  
    <bean id="sqlSessionFactoryqqqq" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>






package com;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mapper.RegisterMapper;
 
@Controller
public class HelloWorldController {
    @Resource
    private RegisterMapper registerMapper;
    @RequestMapping("/helloa")
    public void helloWorld(HttpServletResponse res) throws IOException {
        Register register = registerMapper.selectByPrimaryKey(1);
        System.out.println("register is "+register.toString());
        res.sendRedirect("index.jsp");
    }
}






运行index.jsp以后,console输出结果是:

register is id:1
name:张三
age:13