Mapper配置输入映射

       当parameterType需要传输的参数为一个特殊的数据库类型时,可以在“#{}“中添加对该类型对应的数据库JDBC类型的描述,以便MyBatis在映射时进行相应的转换:#{number,javaType=int,jdbcType=NUMERIC};这句的配置说明了名为”number“的字段对应的Java类型为基本数据类型int,对应的数据库JDBC类型为NUMERIC。

        前面提到过”Handler(类处理器)“,当该类型对应的某列为空时,需要设置处理这种类型参数的Handler,例如:#{age,javaType=NUMERIC,typeHandler=AgeTypeHandler}。

         对于一些需要保留精度的数值类型参数,可以为其添加保留小数点位数的设置,使用numericScale属性。例如,保留两位:

#{price,javaType=double,jdbcType=DECIMAL,numericScale=2}

Mapper配置输出映射

<select id="findUserById" parameterType="int" resultType="cn.com.mybatis.po.User">
		SELECT * FROM user WHERE id=#{id}
</select>
	
<select id="findUserByUsername" parameterType="java.lang.String"           
        resultType="cn.com.mybatis.po.User">
		    SELECT * FROM user WHERE username LIKE '%${value}%'
</select>

       在MyBatis中,不管输出的是JavaBean单个对象还是一个列表(list中包含JavaBean),在Mapper映射文件中resultType指定的类型是一样的。只是调用selectOne(返回单个对象)还是selectList(返回集合对象调用)。

resultMap配置稍微复杂的属性

       association-------关联的嵌套结果,若遇到映射type为Java包装类时,可能会遇到包装类中含有其他Java包装类的属性,这里resultMap提供了association标签来定义结果集中包含的结果集

public class ShoppingCart {
	//购物车id
	private int scartid;
	//购物车商品名
	private String pname;
	//购物车关联的用户
	private User user;
	//省略set和get方法
}

在对查询结果进行映射时包含了一个用户的映射配置:

<resultMap id="shoppingResult"  type="cn.com.mybatis.po.ShoppingCart" >
		<id column="cart_id" property="scartid"/>
		<result column="product_name" property="pname"/>
		<association property="user" javaType="cn.com.mybatis.po.User">
			<id column="user_id" property="id"/>
			<result column="user_username" property="username"/>
			<result column="user_gender" property="gender"/>
			<result column="user_email" property="email"/>
		</association>
</resultMap>
	
<select id="queryShoppingCart" parameterType="int" resultMap="shoppingResult">
		select 
			S.id          as  cart_id,
			S.name        as  product_name,
			S.userid      as  cart_user_id,
			U.id          as  user_id,
			U.username    as  user_username,
			U.gender      as  user_gender,
			U.email       as  user_email
		from shoppingcart S left outer join user U on S.userid = U.id where S.id = #{id}
</select>

最终通过resultMap拿到的查询结果,是一个包含user对象信息的ShoppingCart包装类。当然,若之前定义好了user的resultMap,那么可以在查询结果集配置中引入外部resultMap来使用,例:

<resultMap id="shoppingResult"  type="cn.com.mybatis.po.ShoppingCart" >
		<id column="cart_id" property="scartid"/>
		<result column="product_name" property="pname"/>
		<!-- 这个地方加的这个column不太明白 -->
		<association column="cart_user_id" property="user" 
			javaType="cn.com.mybatis.po.User" resultMap="userResult">
		</association>
	</resultMap>
	<resultMap id="userResult" type="cn.com.mybatis.po.User">
		<id column="user_id" property="id"/>
		<result column="user_username" property="username"/>
		<result column="user_gender" property="gender"/>
		<result column="user_email" property="email"/>
	</resultMap>
	<select id="queryShoppingCart" parameterType="int" resultMap="shoppingResult">
		select 
			S.id          as  cart_id,
			S.name        as  product_name,
			S.userid      as  cart_user_id,
			U.id          as  user_id,
			U.username    as  user_username,
			U.gender      as  user_gender,
			U.email       as  user_email
		from shoppingcart S left outer join user U on S.userid = U.id where S.id = #{id}
	</select>

       collection-------集合的嵌套结果,在一些查询结果包装类中,包含一些List集合属性,使用collection标签可以声明该List集合中属性的类型,例:

public class Product {
    //商品id
	private int id;
    //商品名称
	private String pname;
    //商品的评价信息
	private List<Reply> replys;
	//set和get
	
}

在定义结果映射配置时,使用collection来定义用户结果集合

<resultMap type="cn.com.mybatis.po.Product" id="productResult">
		<id column="product_id" property="pid"/>
		<result column="product_name" property="pname"/>
                    <!--column表示需要传入的id-->
		<collection column="product_id" property="replys"
                                           <!--ofType表示List的model--> 
			select="queryReplyByProductId" ofType="Reply"/>
	</resultMap>
	<select id="queryProductInfo" parameterType="int" resultMap="productResult">
		select
			P.id   as  product_id,
			P.name as product_name
		from product P where P.id = #{id}
	</select>
	<select id="queryReplyByProductId" parameterType="int" resultType="Reply">
		select * from reply R where R.pid=#{ProductId}
	</select>

当然同association标签一样,collection标签也可以引入外部resultMap配置,这里不再赘述,因为看了半天我也没看明白

 

 discriminiator-------根据某个字段的值,来决定关联哪种结果集,就需要使用discriminator(鉴别器)来实现

<resultMap id="shoppingResult"  type="cn.com.mybatis.po.ShoppingCart" >
		<id column="cart_id" property="scartid"/>
		<result column="product_name" property="pname"/>
		<association property="professionalAtrributes" javaType="java.util.HashMap">
			<discriminator javaType="int" column="profession_type">
				<!-- 1战士 -->
				<case value="1" resultMap="zhanshiResult"/>
				<!-- 2法师 -->
				<case value="2" resultMap="fashiResult"/>
			 </discriminator>
		
		</association>
	</resultMap>

Mapper自动映射

使用resultType属性时,达到的结果就是自动映射,在MyBatis全局配置文件中,在setting标签内设置自动映射模式:

<setting name="autoMappingBehavior" value="PARTIAL"/>

     在MyBatis中,自动映射有三种模式,分别为”NONE“、”PARTIAL(啪瘦)“与”FULL“。其中”NULL“表示不启用自动映射,”PARTIAL“表示只对非嵌套的resultMap进行自动映射(默认);而”FULL“表示对所有的resultMap都进行自动映射(慎用)。

        如果在某些resultMap中不想使用自动映射,则可以单独在该resultMap中设置autoMapping属性为false(可选为truefalse),此时该resultMap仅映射开发人员制定的映射字段

<select id="findUserById" parameterType="java.lang.Long" resultMap="UserResult"
    autoMapping="false">
        select id,name,email from t_user where id=#{id}
</select>

 这里autoMapping属性会忽略全局配置文件中autoMappingBehavior映射模式