首页 > 代码库 > ibatis->mybatis升级过程

ibatis->mybatis升级过程

最终目录结构

resources    spring        applicationContext.xml    sqlmap        mapper            aaamapper.xml            bbbmapper.xml        mybatis-config.xml

pom

  • 升级前
<dependency>    <groupId>org.ibatis</groupId>    <artifactId>ibatis</artifactId>    <version>2.3.3.720</version></dependency>
  • 升级后
<dependency>    <groupId>org.mybatis</groupId>    <artifactId>mybatis</artifactId>    <version>3.4.1</version></dependency><dependency>    <groupId>org.mybatis</groupId>    <artifactId>mybatis-spring</artifactId>    <version>1.3.0</version></dependency>

applicationContext.xml

  • 升级后
<bean id="xxsqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="dataSource" ref="dataSource_agencyjizhong"/>    <property name="mapperLocations" value=http://www.mamicode.com/"classpath:sqlmap/mapper/*.xml"/>    <property name="configLocation" value=http://www.mamicode.com/"classpath:sqlmap/mybatis-config.xml"/></bean>

如果id为默认的sqlSessionFactory,会导致MapperScannerConfigurer在加载bean时,加载PropertyPlaceholderConfigurer还没来得及替换定义中的变量,其中就包含数据源中的配置(例如:${env})

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">    <constructor-arg index="0" ref="xxsqlSessionFactory"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    <!--映射器接口文件的包路径-->    <property name="basePackage" value=http://www.mamicode.com/"com.maosheng.mapper"/>    <property name="sqlSessionFactoryBeanName" value=http://www.mamicode.com/"xxsqlSessionFactory"/></bean>

mybatis-config.xml

目录结构

<configuration>    <settings></settings>    <typeAliases></typeAliases>    <mappers></mappers></configuration>
  1. dtd
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
  2. 根节点
    • 升级前 sqlMapConfig
    • 升级后 configuration
  3. settings
    • 升级前 <settings x="y" foo="bar"/>
    • 升级后 
      <settings><setting name="x" value="y"/><setting name="foo" value="bar"/></settings>
  4. typeAlias

    将每个***mapper.xml中下的typeAlias全部移至sqlMapConfig.xml中下 

***mapper.xml

  1. dtd
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  2. mapper
  3. parameterType
    • 升级前 parameterClass
    • 升级后 parameterType
  4. resultType
    • 升级前 resultClass
    • 升级后 resultType
  5. 内联参数
    • 升级前 #value#或者value
    • 升级后 #{value}
注意和${value}的区别

 

ibatis->mybatis升级过程