Mybatis和Ibatis区别

1. ibatis3.*版本以后正式改名为mybaits,它也从apache转到了google code下;也就是说ibatis2.*,mybatis3.*。

2. 映射文件的不同

 

ibatis的配置文件如下

 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	<!-- 公共配置 -->
	<settings useStatementNamespaces="true" maxRequests="3000"
		maxSessions="1000" maxTransactions="3000" />
		
	<!-- 配置文件		begin -->	
	<sqlMap resource="com/test/biz/dao/sql/AA_SqlMap.xml" />
<pre name="code" class="html">	<sqlMap resource="com/test/biz/dao/sql/BB_SqlMap.xml" />

</sqlMapConfig>

 



mybatis的配置文件如下

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<settings>
		<!-- changes from the defaults for testing -->
		<setting name="cacheEnabled" value="false" />
		<setting name="useGeneratedKeys" value="true" />
		<setting name="defaultExecutorType" value="REUSE" />
		<!-- 延迟加载 -->
		<setting name="lazyLoadingEnabled" value="true" />
		<setting name="aggressiveLazyLoading" value="false" />
	</settings>
	<mappers>
		<mapper resource="com/test/biz/dao/sql/AAMapper.xml"/>
	</mappers>
</configuration>

 

 

从以上两个配置文件,大致分析有哪些不同点:

2.1 dtd约束文件不同

2.2 ibatis中根元素是sqlMapConfig,mybatis中是configuration;

2.3 settings属性的不同配置

ibatis中是

<settings 属性1="属性值1" 属性2="属性值2"  属性x="属性值x"/>

mybatis中是

<settings>

<setting name="属性1" value="属性值1"/>

 

<setting name="属性2" value="属性值2"/>

 

<setting name="属性x" value="属性值x"/>

 

</settings>
2.4 ibatis中是使用sqlMap元素,mybatis中是使用mappers元素;

2.5 数据库表的映射区别

ibatis中某表的映射文件

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="PP_CLASS_PROPERTY">
	<resultMap id="BaseResultMap" class="com.test.biz.dto.PpClassProperty">
		<result column="ID" property="id" jdbcType="VARCHAR" />
		<result column="CLASS_ID" property="classId" jdbcType="VARCHAR" />
		<result column="PROPERTY_ID" property="propertyId" jdbcType="VARCHAR" />
		<result column="INPUT_TYPE" property="inputType" jdbcType="VARCHAR" />
		<result column="SORT_NUM" property="sortNum" jdbcType="DECIMAL" />
		<result column="DESCRIPTION" property="description" jdbcType="VARCHAR" />
		<result column="CREATED_DATE" property="createdDate" jdbcType="TIMESTAMP" />
		<result column="CREATED_BY" property="createdBy" jdbcType="VARCHAR" />
		<result column="UPDATED_DATE" property="updatedDate" jdbcType="TIMESTAMP" />
		<result column="UPDATED_BY" property="updatedBy" jdbcType="VARCHAR" />
		<result column="STATUS" property="status" jdbcType="DECIMAL" />
		<result column="IS_KEY" property="isKey" jdbcType="DECIMAL" />
		<result column="IS_SPU" property="isSpu" jdbcType="DECIMAL" />
		<result column="IS_SALE" property="isSale" jdbcType="DECIMAL" />
		<result column="IS_PRODUCT" property="isProduct" jdbcType="DECIMAL" />
		<result column="CHANNEL_ID" property="channelId" jdbcType="VARCHAR" />
		<result column="COMPANY_ID" property="companyId" jdbcType="VARCHAR" />
	</resultMap>

	<resultMap id="ResultMapWithClassName" class="com.test.biz.dto.PpClassProperty"
		extends="PP_CLASS_PROPERTY.BaseResultMap">
		<result property="propertyName" jdbcType="VARCHAR" />
		<result property="className" jdbcType="VARCHAR" />
	</resultMap>

	<resultMap id="ResultMapWithClassNameForPage" class="com.test.biz.dto.PpClassProperty"
		extends="PP_CLASS_PROPERTY.BaseResultMap">
		<result property="propertyName" jdbcType="VARCHAR" />
		<result property="className" jdbcType="VARCHAR" />
		<result column="rnum" property="rnum" jdbcType="VARCHAR" />
	</resultMap>

	<sql id="Base_Column_List">
		ID, CLASS_ID, PROPERTY_ID, INPUT_TYPE, SORT_NUM,
		DESCRIPTION,
		CREATED_DATE, CREATED_BY,
		UPDATED_DATE, UPDATED_BY, STATUS,
		IS_KEY, IS_SPU, IS_SALE, IS_PRODUCT,CHANNEL_ID,COMPANY_ID
	</sql>

	<sql id="p_Base_Column_List">
		P.ID, P.CLASS_ID, P.PROPERTY_ID, P.INPUT_TYPE, P.SORT_NUM,
		P.DESCRIPTION,
		P.CREATED_DATE,
		P.CREATED_BY, P.UPDATED_DATE,
		P.UPDATED_BY, P.STATUS, P.IS_KEY, P.IS_SPU,
		P.IS_SALE,
		P.IS_PRODUCT,P.CHANNEL_ID,P.COMPANY_ID
	</sql>

	<!-- 0:未删除 1:已删除 -->
	<select id="selectByPrimaryKey" resultMap="BaseResultMap"
		parameterClass="com.test.biz.dto.PpClassProperty">
		SELECT
		<include refid="PP_CLASS_PROPERTY.Base_Column_List" />
		FROM PP_CLASS_PROPERTY
		WHERE ID = #id:VARCHAR# AND
		<include refid="COMMON_JUDGEMENT_RULE.Judge_Status_ChannelId_CompanyId" />
	</select>

	<!-- 删除执行的是修改状态为1,表示已删除 -->
	<update id="deleteByPrimaryKey" parameterClass="com.test.biz.dto.PpClassProperty">
		UPDATE
		PP_CLASS_PROPERTY
		SET STATUS = 1
		where ID = #id:VARCHAR# AND
		<include refid="COMMON_JUDGEMENT_RULE.Judge_Status_ChannelId_CompanyId" />
	</update>

	<!-- 全字段 添加 -->
	<insert id="insert" parameterClass="com.test.biz.dto.PpClassProperty">
		INSERT INTO PP_CLASS_PROPERTY
		(
		<include refid="PP_CLASS_PROPERTY.Base_Column_List" />
		)
		VALUES (#id:VARCHAR#, #classId:VARCHAR#,
		#propertyId:VARCHAR#,
		#inputType:VARCHAR#,
		#sortNum:DECIMAL#,
		#description:VARCHAR#,
		#createdDate:TIMESTAMP#,
		#createdBy:VARCHAR#,
		#updatedDate:TIMESTAMP#,
		#updatedBy:VARCHAR#, 0,#isKey:DECIMAL#,
		#isSpu:DECIMAL#,
		#isSale:DECIMAL#, #isProduct:DECIMAL#,
		#channelId:VARCHAR#,
		#companyId:VARCHAR#)
	</insert>

	<!-- 动态修改 -->
	<update id="updateByPrimaryKeySelective" parameterClass="com.test.biz.dto.PpClassProperty">
		UPDATE PP_CLASS_PROPERTY
		<dynamic prepend="set">
			<isNotNull prepend="," property="classId">
				CLASS_ID =
				#classId:VARCHAR#
			</isNotNull>
			<isNotNull prepend="," property="propertyId">
				PROPERTY_ID =
				#propertyId:VARCHAR#
			</isNotNull>
			<isNotNull prepend="," property="inputType">
				INPUT_TYPE =
				#inputType:VARCHAR#
			</isNotNull>
			<isNotNull prepend="," property="sortNum">
				SORT_NUM =
				#sortNum:DECIMAL#
			</isNotNull>
			<isNotNull prepend="," property="description">
				DESCRIPTION =
				#description:VARCHAR#
			</isNotNull>
			<isNotNull prepend="," property="createdDate">
				CREATED_DATE =
				#createdDate:TIMESTAMP#
			</isNotNull>
			<isNotNull prepend="," property="createdBy">
				CREATED_BY =
				#createdBy:VARCHAR#
			</isNotNull>
			<isNotNull prepend="," property="updatedDate">
				UPDATED_DATE =
				#updatedDate:TIMESTAMP#
			</isNotNull>
			<isNotNull prepend="," property="updatedBy">
				UPDATED_BY =
				#updatedBy:VARCHAR#
			</isNotNull>
			<isNotNull prepend="," property="status">
				STATUS = #status:DECIMAL#
			</isNotNull>
			<isNotNull prepend="," property="isKey">
				IS_KEY = #isKey:DECIMAL#
			</isNotNull>
			<isNotNull prepend="," property="isSpu">
				IS_SPU = #isSpu:DECIMAL#
			</isNotNull>
			<isNotNull prepend="," property="isSale">
				IS_SALE = #isSale:DECIMAL#
			</isNotNull>
			<isNotNull prepend="," property="isProduct">
				IS_PRODUCT =
				#isProduct:DECIMAL#
			</isNotNull>
		</dynamic>
		WHERE ID = #id:VARCHAR# AND
		<include refid="COMMON_JUDGEMENT_RULE.Judge_Status_ChannelId_CompanyId" />
	</update>

	<!--查询类目已经关联的属性 0:未删除 1:已删除 -->
	<select id="selectByClassId" resultMap="BaseResultMap"
		parameterClass="com.test.biz.dto.PpClassProperty">
		SELECT
		<include refid="PP_CLASS_PROPERTY.Base_Column_List" />
		FROM PP_CLASS_PROPERTY
		WHERE CLASS_ID = #classId:VARCHAR# AND
		<include refid="COMMON_JUDGEMENT_RULE.Judge_Status_ChannelId_CompanyId" />
	</select>

	<!-- 统计 查询类目已经关联的属性的数量 0:未删除 1:已删除 -->
	<select id="countByClassId" resultClass="integer"
		parameterClass="com.test.biz.dto.PpClassProperty">
		SELECT
		COUNT(ID)
		FROM PP_CLASS_PROPERTY
		WHERE CLASS_ID =
		#classId:VARCHAR# AND
		<include refid="COMMON_JUDGEMENT_RULE.Judge_Status_ChannelId_CompanyId" />
	</select>

	<!--根据类目id查询类目属性(包含类目名称和属性名称) 0:未删除 1:已删除 -->
	<select id="selectWithClassNameByClassId" resultMap="ResultMapWithClassName"
		parameterClass="map">
		SELECT
		<include refid="p_Base_Column_List" />
		,PP.PROPERTY_NAME PROPERTYNAME,PC.CLASS_NAME CLASSNAME
		FROM
		PP_CLASS_PROPERTY P,PP_PROPERTY PP,PP_CLASS PC
		WHERE P.CLASS_ID =
		#classId# AND P.CHANNEL_ID = #channelId# AND P.COMPANY_ID =
		#companyId#
		<isNotNull property="likeName">
			AND PP.PROPERTY_NAME LIKE '%'||#likeName#||'%'
		</isNotNull>
		AND P.CLASS_ID = PC.ID AND P.PROPERTY_ID = PP.ID
		AND P.CHANNEL_ID =
		PC.CHANNEL_ID AND P.CHANNEL_ID = PP.CHANNEL_ID
		AND P.COMPANY_ID =
		PC.COMPANY_ID AND P.COMPANY_ID = PP.COMPANY_ID
		AND P.STATUS=0 AND
		PP.STATUS=0 AND PC.STATUS=0
	</select>


	<!--根据类目id查询类目属性(包含类目名称和属性名称) 查询总数 -->
	<select id="countPageFindPpClassPropertyByClassId" resultClass="integer"
		parameterClass="map">
		SELECT
		COUNT(1)
		FROM
		PP_CLASS_PROPERTY P,PP_PROPERTY PP,PP_CLASS PC
		WHERE P.CLASS_ID =
		#classId# AND P.CHANNEL_ID = #channelId# AND P.COMPANY_ID =
		#companyId#
		<isNotNull property="likeName">
			AND PP.PROPERTY_NAME LIKE '%'||#likeName#||'%'
		</isNotNull>
		AND P.CLASS_ID = PC.ID AND P.PROPERTY_ID = PP.ID
		AND P.CHANNEL_ID =
		PC.CHANNEL_ID AND P.CHANNEL_ID = PP.CHANNEL_ID
		AND P.COMPANY_ID =
		PC.COMPANY_ID AND P.COMPANY_ID = PP.COMPANY_ID
		AND P.STATUS=0 AND
		PP.STATUS=0 AND PC.STATUS=0
	</select>

	<!-- 根据类目id查询该条记录时关键属性并且时type类型是input的值的信息 -->
	<select id="findInputClassProperty" resultClass="java.util.HashMap"
		parameterClass="com.test.biz.dto.PpClassProperty">
		SELECT INPUT_TYPE AS "inputType" FROM PP_CLASS_PROPERTY
		WHERE
		CLASS_ID=#classId# AND IS_KEY=1 
		AND (INPUT_TYPE='input' OR INPUT_TYPE='textarea') AND
		<include refid="COMMON_JUDGEMENT_RULE.Judge_Status_ChannelId_CompanyId" />
	</select>


</sqlMap>


mybatis的数据库表映射文件

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.ilvyou.system.dao.SysUserMapper" >
	<resultMap id="BaseResultMap" type="com.ilvyou.system.entity.SysUserEntity" >
		<id property="id" column="ID" jdbcType="VARCHAR"/>
		<result property="loginName" column="LOGIN_NAME" jdbcType="VARCHAR"/>
		<result property="loginPass" column="LOGIN_PASS" jdbcType="VARCHAR"/>
		<result property="userType" column="USER_TYPE" jdbcType="VARCHAR"/>
		<result property="regDate" column="REG_DATE" jdbcType="CHAR"/>
		<result property="email" column="EMAIL" jdbcType="VARCHAR"/>
		<result property="mobile" column="MOBILE" jdbcType="VARCHAR"/>
		<result property="qq" column="QQ" jdbcType="VARCHAR"/>
		<result property="photo" column="PHOTO" jdbcType="VARCHAR"/>
		<result property="cardType" column="CARD_TYPE" jdbcType="VARCHAR"/>
		<result property="cardValue" column="CARD_VALUE" jdbcType="VARCHAR"/>
		<result property="userName" column="USER_NAME" jdbcType="VARCHAR"/>
		<result property="userSex" column="USER_SEX" jdbcType="VARCHAR"/>
		<result property="userBir" column="USER_BIR" jdbcType="CHAR"/>
		<result property="userAddr" column="USER_ADDR" jdbcType="VARCHAR"/>
		<result property="jobName" column="JOB_NAME" jdbcType="VARCHAR"/>
		<result property="jobAddr" column="JOB_ADDR" jdbcType="VARCHAR"/>
		<result property="jobTel" column="JOB_TEL" jdbcType="VARCHAR"/>
		<result property="bornAddr" column="BORN_ADDR" jdbcType="VARCHAR"/>
		<result property="userStatus" column="USER_STATUS" jdbcType="INTEGER"/>
		<result property="roleType" column="ROLE_TYPE" jdbcType="VARCHAR"/>
	</resultMap>

    <sql id="Base_Column_List">
	ID          ,
	LOGIN_NAME  ,
	LOGIN_PASS  ,
	USER_TYPE   ,
	REG_DATE    ,
	EMAIL       ,
	MOBILE      ,
	QQ          ,
	PHOTO       ,
	CARD_TYPE   ,
	CARD_VALUE  ,
	USER_NAME   ,
	USER_SEX    ,
	USER_BIR    ,
	USER_ADDR   ,
	JOB_NAME    ,
	JOB_ADDR    ,
	JOB_TEL     ,
	BORN_ADDR   ,
	USER_STATUS ,
	ROLE_TYPE
    </sql>

    <sql id="select_by_page_outter_orderby_sql" >
      <if test="orderByClause != null">    order by ${orderByClause}   </if> 
    </sql>


    <!--select mothed-->
    <select id="selectByPrimaryKey" parameterType="String" resultMap="SysUserEntityResultMap">
    select 
    <include refid="Base_Column_List" />
    from SYS_USER
    where ID = #{id,jdbcType=VARCHAR}
    </select>
    
    <!--insert mothed-->
    <insert id="insert" parameterType="com.ilvyou.system.entity.SysUserEntity">
    insert into SYS_USER (
		<include refid="Base_Column_List" />
	  )
    values (
	#{id,jdbcType=VARCHAR},
	#{loginName,jdbcType=VARCHAR},
	#{loginPass,jdbcType=VARCHAR},
	#{userType,jdbcType=VARCHAR},
	#{regDate,jdbcType=CHAR},
	#{email,jdbcType=VARCHAR},
	#{mobile,jdbcType=VARCHAR},
	#{qq,jdbcType=VARCHAR},
	#{photo,jdbcType=VARCHAR},
	#{cardType,jdbcType=VARCHAR},
	#{cardValue,jdbcType=VARCHAR},
	#{userName,jdbcType=VARCHAR},
	#{userSex,jdbcType=VARCHAR},
	#{userBir,jdbcType=CHAR},
	#{userAddr,jdbcType=VARCHAR},
	#{jobName,jdbcType=VARCHAR},
	#{jobAddr,jdbcType=VARCHAR},
	#{jobTel,jdbcType=VARCHAR},
	#{bornAddr,jdbcType=VARCHAR},
	#{userStatus,jdbcType=INTEGER},
	#{roleType,,jdbcType=VARCHAR}
	)
    </insert>

    <insert id="batchInsert" >    
	insert into SYS_USER (<include refid="Base_Column_List" />)  
	values     
	<foreach collection="list" item="item" index="index" separator=",">
	(
	#{item.id,jdbcType=VARCHAR},
	#{item.loginName,jdbcType=VARCHAR},
	#{item.loginPass,jdbcType=VARCHAR},
	#{item.userType,jdbcType=VARCHAR},
	#{item.regDate,jdbcType=CHAR},
	#{item.email,jdbcType=VARCHAR},
	#{item.mobile,jdbcType=VARCHAR},
	#{item.qq,jdbcType=VARCHAR},
	#{item.photo,jdbcType=VARCHAR},
	#{item.cardType,jdbcType=VARCHAR},
	#{item.cardValue,jdbcType=VARCHAR},
	#{item.userName,jdbcType=VARCHAR},
	#{item.userSex,jdbcType=VARCHAR},
	#{item.userBir,jdbcType=CHAR},
	#{item.userAddr,jdbcType=VARCHAR},
	#{item.jobName,jdbcType=VARCHAR},
	#{item.jobAddr,jdbcType=VARCHAR},
	#{item.jobTel,jdbcType=VARCHAR},
	#{item.bornAddr,jdbcType=VARCHAR},
	#{item.userStatus,jdbcType=INTEGER},
	#{item.roleType,jdbcType=VARCHAR}
	)
	</foreach>
    </insert>


    <update id="updateByPrimaryKeySelective" parameterType="com.ilvyou.system.entity.SysUserEntity">
    update SYS_USER
    <set>
      <if test="id != null">
       ID = #{id,jdbcType=VARCHAR},
      </if>
      <if test="loginName != null">
       LOGIN_NAME = #{loginName,jdbcType=VARCHAR},
      </if>
      <if test="loginPass != null">
       LOGIN_PASS = #{loginPass,jdbcType=VARCHAR},
      </if>
      <if test="userType != null">
       USER_TYPE = #{userType,jdbcType=VARCHAR},
      </if>
      <if test="regDate != null">
       REG_DATE = #{regDate,jdbcType=CHAR},
      </if>
      <if test="email != null">
       EMAIL = #{email,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null">
       MOBILE = #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="qq != null">
       QQ = #{qq,jdbcType=VARCHAR},
      </if>
      <if test="photo != null">
       PHOTO = #{photo,jdbcType=VARCHAR},
      </if>
      <if test="cardType != null">
       CARD_TYPE = #{cardType,jdbcType=VARCHAR},
      </if>
      <if test="cardValue != null">
       CARD_VALUE = #{cardValue,jdbcType=VARCHAR},
      </if>
      <if test="userName != null">
       USER_NAME = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="userSex != null">
       USER_SEX = #{userSex,jdbcType=VARCHAR},
      </if>
      <if test="userBir != null">
       USER_BIR = #{userBir,jdbcType=CHAR},
      </if>
      <if test="userAddr != null">
       USER_ADDR = #{userAddr,jdbcType=VARCHAR},
      </if>
      <if test="jobName != null">
       JOB_NAME = #{jobName,jdbcType=VARCHAR},
      </if>
      <if test="jobAddr != null">
       JOB_ADDR = #{jobAddr,jdbcType=VARCHAR},
      </if>
      <if test="jobTel != null">
       JOB_TEL = #{jobTel,jdbcType=VARCHAR},
      </if>
      <if test="bornAddr != null">
       BORN_ADDR = #{bornAddr,jdbcType=VARCHAR},
      </if>
      <if test="userStatus != null">
       USER_STATUS = #{userStatus,jdbcType=INTEGER},
      </if>
      <if test="roleType != null">
       ROLE_TYPE = #{roleType,jdbcType=VARCHAR},
      </if>
     </set>
	where ID = #{id,jdbcType=VARCHAR}
     </update>


    <update id="batchUpdateByPrimaryKey" >    
	<foreach collection="list" item="item" index="index">        
	update SYS_USER set 
	  LOGIN_NAME = #{item.loginName,jdbcType=VARCHAR},
	  LOGIN_PASS = #{item.loginPass,jdbcType=VARCHAR},
	  USER_TYPE = #{item.userType,jdbcType=VARCHAR},
	  REG_DATE = #{item.regDate,jdbcType=CHAR},
	  EMAIL = #{item.email,jdbcType=VARCHAR},
	  MOBILE = #{item.mobile,jdbcType=VARCHAR},
	  QQ = #{item.qq,jdbcType=VARCHAR},
	  PHOTO = #{item.photo,jdbcType=VARCHAR},
	  CARD_TYPE = #{item.cardType,jdbcType=VARCHAR},
	  CARD_VALUE = #{item.cardValue,jdbcType=VARCHAR},
	  USER_NAME = #{item.userName,jdbcType=VARCHAR},
	  USER_SEX = #{item.userSex,jdbcType=VARCHAR},
	  USER_BIR = #{item.userBir,jdbcType=CHAR},
	  USER_ADDR = #{item.userAddr,jdbcType=VARCHAR},
	  JOB_NAME = #{item.jobName,jdbcType=VARCHAR},
	  JOB_ADDR = #{item.jobAddr,jdbcType=VARCHAR},
	  JOB_TEL = #{item.jobTel,jdbcType=VARCHAR},
	  BORN_ADDR = #{item.bornAddr,jdbcType=VARCHAR},
	  USER_STATUS = #{item.userStatus,jdbcType=INTEGER},
	  ROLE_TYPE = #{item.roleType,jdbcType=VARCHAR}
	where ID = #{item.id,jdbcType=VARCHAR}
	</foreach>
    </update>



    <!--delete mothed-->
    <delete id="deleteByPrimaryKey" parameterType="String">
		delete from SYS_USER
		where ID = #{id,jdbcType=VARCHAR}
    </delete>

    <delete id="batchDelete">
		delete from SYS_USER 
		where ID in (
		<foreach collection="list" item="item" index="index" separator=",">
			#{item.id,jdbcType=VARCHAR}
		</foreach>
		)
	</delete>

</mapper>


两者的区别有:

 

2.5.1 ibatis中根元素是sqlMap,mybatis中是mapper;

2.5.2 在 iBatis 中,namespace 不是必需的,且它的存在没有实际的意义。在 MyBatis 中,namespace 终于派上用场了,它使得映射文件与接口绑定变得非常自然。

2.5.3 ibatis中有resultMap和resultClass两种返回类型,resultMap是我们在ibatis的配置文件中定义的,也就是在配置文件中使用resultMap元素定义的;resultClass是指java语言中内置的类型,如:integer、java.util.HashMap等等;

mybatis中将两者统一为resultType,这样挺好的,开发者不用再记两个属性了。

2.5.4 ibatis中有parameterClass,mybatis中有parameterType,两者区别不大。

2.5.5 参数的写法比较

ibatis中写法,如代码片段:

 

WHERE ID = #id:VARCHAR#

 

mybatis中写法,如代码片段:

 

where ID = #{id,jdbcType=VARCHAR}

2.5.6 iBatis/MyBatis 调用存储过程的写法


iBatis 调用存储过程的方法,通过使用 <procedure> 元素进行存储过程的调用:

<parameterMap id="swapParameters" class="map" >     
  
 <parameter property="contactId" jdbcType="INTEGER" javaType="java.lang.Integer" mode="IN"/>     
  
 <parameter property="firstName" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>       
  
 <parameter property="lastName" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>     
  
</parameterMap>     
  
     
  
<procedure id="swapContactName" parameterMap="swapParameters" >     
  
{call swap_contact_name (?, ?,?)}      
  
</procedure>   



在 MyBatis 中,<proccedure> 元素已经被移除,通过 <select>、<insert> 和 <update> 进行定义:

  <select id="swapContactName" parameterMap="swapParameters" statementType="CALLABLE">  
    { ? = call swap_contact_name (?,?,?)}   
  </select>


如上所示,通过 statementType 属性将该语句标识为存储过程而非普通 SQL 语句。

 

2.6 ibatis和mybatis与spring的集成配置

ibatis的配置

 

	<!--===================================================================== -->
	<!-- iBATIS 配置文件定义 -->
	<!--===================================================================== -->
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation" value="classpath:sqlMap-config.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>

 

 

mybatis的配置

 

	<!-- define the MyBatis SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:com/ilvyou/core/config/mybatis-config.xml" />
		<property name="mapperLocations" value="classpath*:com/ilvyou/**/*Mapper.xml" />
	</bean>



 

通过前面的示例可以看出,MyBatis 在编码中的最大的改变就是将一个最常用的 API 由 SqlMapClient 改为了 SqlSessionFactory。另外,类型处理器接口也由原来的 TypeHandlerCallback 改为了 TypeHandler。最后 DataSourceFactory 也进行了调整,移动到 org.apache.ibatis.datasource 包下,其中的方法也作了微调。总之,代码层面公开的部分改动较少,不会给开发者造成较大的移植成本。

上一篇:SQL语句


下一篇:JdbcType类型和Java类型的对应关系