zoukankan      html  css  js  c++  java
  • mybatis和spring mvc整合

    1、环境

    a.  jar包

    (mybatis+spring mvc运行包+两者整合包mybatis-spring.jar)

    b.工程目录

     

    c. 配置文件

    mybatis:SqlMapConfig.xml、mapper.xml等

    spring mvc: applicationContext.xml

       a) applicationContext.xml

       <beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

          http://www.springframework.org/schema/mvc

          http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

          http://www.springframework.org/schema/context

          http://www.springframework.org/schema/context/spring-context-3.2.xsd

          http://www.springframework.org/schema/aop

          http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

          http://www.springframework.org/schema/tx

          http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

     

       <!-- 加载配置文件 db.properties -->

       <context:property-placeholder location="classpath:db.properties" />

     

       <!-- 使用dbcp数据源 -->

       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

          destroy-method="close">

          <property name="driverClassName" value="${jdbc.driver}" />

          <property name="url" value="${jdbc.url}" />

          <property name="username" value="${jdbc.username}" />

          <property name="password" value="${jdbc.password}" />

          <property name="maxActive" value="15" />

          <property name="maxIdle" value="3" />

       </bean>

     

       <!-- sqlSessionFactory -->

       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

          <!-- 加载mybatis的配置文件 -->

          <property name="configLocation" value="mybatis/SqlMapConfig.xml" />

          <!-- 数据源 -->

          <property name="dataSource" ref="dataSource" />

       </bean>

     

    </beans>

     理解

     

    2.原始dao开发

    a. dao

     

    b.user.xml

     

    c.SqlMapConfig.xml

     

    d.impl(要继承SqlSessionDaoSupport)

     

    纯mybatis的话,这里是注入一个SqlSession来操作的。整合spring后通过SqlSessionDaoSupport的getSqlSession来操作。

    e.applicationContext配置bean

     

    f.测试程序

     

    3.mapper代理

    a.UserMapper.java

     

    b.UserMapper.xml

     

    c.SqlMapConfig.xml(不需要配置Mapper和数据源)

    d.applicationContext配置

    方法一,单个

     

    方法二,批量

     

    e.测试程序

     

  • 相关阅读:
    剑指offer:面试题25、二叉树中和为某值的路径
    剑指offer:面试题24、二叉搜索树的后续遍历序列
    剑指offer:面试题23、从上往下打印二叉树
    剑指offer:面试题22、栈的压入、弹出序列
    剑指offer:面试题21、包含min函数的栈
    剑指offer:面试题20、顺时针打印矩阵
    剑指offer:面试题19、二叉树的镜像
    剑指offer:面试题18、树的子结构
    剑指offer:面试题17、合并两个排序的链表
    剑指offer:面试题16、反转链表
  • 原文地址:https://www.cnblogs.com/jway1101/p/5773584.html
Copyright © 2011-2022 走看看