1、环境

a.  jar包

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

 

b.工程目录

 mybatis和spring mvc整合

 

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>

 

 理解

 mybatis和spring mvc整合

mybatis和spring mvc整合

 

 

 

2.原始dao开发

 

a. dao

 mybatis和spring mvc整合

 

 

b.user.xml

 mybatis和spring mvc整合

 

 

c.SqlMapConfig.xml

 mybatis和spring mvc整合

 

d.impl(要继承SqlSessionDaoSupport)

 mybatis和spring mvc整合

 

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

 

e.applicationContext配置bean

 mybatis和spring mvc整合

 

f.测试程序

 

 mybatis和spring mvc整合

 

3.mapper代理

a.UserMapper.java

 mybatis和spring mvc整合

 

 

 

b.UserMapper.xml

 mybatis和spring mvc整合

 

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

 

d.applicationContext配置

方法一,单个

 mybatis和spring mvc整合

 

方法二,批量

 mybatis和spring mvc整合

 

 

e.测试程序

 mybatis和spring mvc整合

 

相关文章:

  • 2020-07-07
  • 2021-12-05
  • 2021-11-01
  • 2021-12-02
  • 2020-09-27
  • 2021-02-27
  • 2018-04-19
  • 2018-09-08
猜你喜欢
  • 2021-09-21
  • 2019-11-05
  • 2019-02-07
  • 2018-03-09
  • 2020-12-25
  • 2018-03-19
  • 2018-03-28
  • 2018-06-13
相关资源
相似解决方案