1.Mybatis比IBatis比较大的几个改进是什么?
1.全局文件的配置
a.两个版本的约束不同:
直接影响到配置文件中的根元素ibatis是<sqlMapConfig></sqlMapConfig>
mybatis是<configuration></configuration>
b.配置事务管理器和数据源的方式不同:
c.指定映射文件的方式不同:
d.mybatis除了通过了xml方式实现全局配置还提供了代码方式进行配置的方式;
<?xml version="1.0" encoding="UTF-8" ?>
<!-- ibatis DTD文件 -->
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!--ibatis 配置事务管理器和数据源 方式-->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:orcl" />
<property name="JDBC.Username" value="test" />
<property name="JDBC.Password" value="test" />
</dataSource>
</transactionManager>
<!-- ibatis 指定映射文件方式 -->
<sqlMap resource="mapping/HyczyMapper.xml" />
</sqlMapConfig>
mybatis
<?xml version="1.0" encoding="UTF-8" ?>
<!-- mybatis DTD约束文件 -->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- mybatis 配置事务管理器和数据源 -->
<properties resource="jdbc.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- mybatis 指定映射文件 -->
<mappers>
<mapper resource="mapping/ZymdMapper.xml"/>
</mappers>
</configuration>
2.映射文件配置SQL语句
1.namespace :ibatis不是必须的,且无意义;
mybatis有意义;
2.一系列的属性名的改变,但是用法和含义没有发生改变
a.由于dtd约束文件发生变化,根元素由<sqlMap></sqlMap>变为<mapper></mapper>
b.<select> 等元素的 parameterClass 属性改为了 parameterType 属性;
c.<select> 等元素的 resultClasss 属性改为了 resultType 属性;
d.<result> 元素的 columnIndex 属性被移除了;
e.嵌套参数由 #value# 改为了 #{value};
<?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>
<resultMap class="com.test.ibatistest.model.Hyczy" id="baseResult">
<result property="hydm" column="HYDM"/>
<result property="loginname" column="LOGINNAME"/>
<result property="mobile" column="MOBILE"/>
<result property="email" column="EMAIL"/>
<result property="createrq" column="CREATERQ"/>
<result property="zhxgczy" column="ZHXGCZY"/>
</resultMap>
<typeAlias alias="hyczy" type="com.test.ibatistest.model.Hyczy" />
<select id="getAllUsers" resultClass="hyczy" parameterClass="hyczy">
select * from T_M_HYCZY where 1=1 and hydm=#hydm#
</select>
</sqlMap>
mybatis
<?xml version="1.0" encoding="UTF-8"?>
<!-- mybatis DTD约束文件 -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.dao.ZymdMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.test.model.Zymd">
<id column="ID" property="id" jdbcType="NUMERIC"/>
<result column="YWWYH" property="ywwyh" />
<result column="WXTWYH" property="wxtwyh" />
<result column="CKWYH" property="ckwyh" />
<result column="CKFLAG" property="ckflag" />
<result column="CKRETURN" property="ckreturn" />
<result column="YJPZID" property="yjpzid" />
</resultMap>
<select id="selectOnes" resultType="com.test.model.Zymd" parameterType="com.test.model.Zymd">
select * from t_r_zymd where 1=1
<if test="id!=null and id!=''">
and id=#{id}
</if>
</select>
</mapper>
3.iBatis/MyBatis 对存储过程的支持
https://blog.csdn.net/Rudolf__/article/details/83578615