mybatis的Mapper配置输出映射小结

版权声明:原创 https://blog.csdn.net/qq_39115469/article/details/85106029

         之前对这块掌握的不是很好,有些模糊,今天把Mapper配置进行总结一下。

         在mybatis的Mapper映射文件中,SQL语句查询返回的结果 ,会映射到配置标签的输出映射属性对应的java类型。Mapper

的输出映射有两种配置:resultType和resultMap。

resultType:

<!--resultType用了别名,表示Retailer实体类-->
<select id="find"  resultType="retailer">
	select * from retailer where 
</select>

           resultType所指的输出类型是一种java的原始类型或包装类型,并且从数据库去除的字段名称无需任何转换。

resultmMap:

            如果在SQL映射文件中配置的SQL语句返回的结果为多个值,且没有一个完全与返回结果值一一匹配的封装类去接收,

针对这种情况,mybatis提供了一种SQL结果集输出映射类型,即resultMap。可以通过定义一个resultMap在列名和java包装类

属性名之间创建映射关系。

<resultMap type="user" id="userMap">
	<id column="_id" property="id"/>
	<result column="_username" property="username"/>
</resultMap>
<select id="find" resultMap="userMap">
	select id _id,username _username from user
</select>

               代码说明:userMap是resultMap的唯一标识,user是resultMap最终所要映射的java对象类型,可以使用别名,

id指主键,column属性值对应sql语句查出来的列名,property是对应的要映射的java对象的属性,其他一般属性就用result。

上面的代码表示:sql语句查询出的'_id'映射到User类的id属性,'_username'映射到User类的username属性。

resultMap+association:

            在定义的resultMap中使用association来进行关联查询:购物车关联用户查询。

public class ShoppingCart {
	//购物车id
	private int cartid;
	//购物车商品名
	private String name;
	//购物车关联的用户
	private User user;
}
<resultMap type="ShoppingCart" id="shopMap">
	<id property="cartid" column="cart_id"/>
	<result property="name" column="_name"/>
	<!-- property属性值对应(private User user)中的user属性,javaType属性值对应User这个类型,可以用别名 -->
	<association property="user" javaType="online.lmlrt.User">
	    <!-- 关联查询的实体类的主键 -->
	    <id property="id" column="user_id"/>
	    <!-- 关联查询的实体类的其他属性 -->
	    <result property="username" column="user_username"/>
	    <result property="email" column="user_email"/>
	    <result property="gender" column="user_gender"/>
	 </association>
</resultMap>
	
<select id="query" resultMap="shopMap">
	select 
	S.userid       as  cart_id,
	S.name     as  _name,
	U.id       as  user_id,
	U.username as  user_username,
	U.gender   as  user_gender,
	U.email    as  user_email
	from shopingcart S join user U on S.userid=U.id
 </select>

Mapper配置文件中配置动态SQL语句:

            这个点也是常用的,我的项目中很多查询用到了这个知识点。比如第一次点一个连接,进去后显示查询的所有的记录,当通过页面进行条件搜索时,还可以用原来的sql语句,只是在原来的sql语句后面加了if配置,构成动态sql。代码如下:

<!-- 查询 -->
<select id="find" parameterType="java.util.HashMap" resultType="retailer">
	select * from retailer where 1=1 
	<if test="name != null"> and name like #{name}</if>
        <if test="address != null"> and address like #{address}</if>
	<if test="telphone != null"> and telphone like #{telphone}</if>
</select>

         说明:第一次点进去,没有任何条件,即条件为空,则不会拼接if之间的sql,当发起搜索时,有了条件,会进行拼接sql语句,即条件不再为空,在原来的查询基础上加了条件查询。

${}和#{}:

          #{}代表占位符,可以接受参数,例如在jdbc中用?表示占位符。 

          有时候需要进行拼接SQL语句,例如模糊查询时,就需要在查询条件的两侧拼接两个'%',这个时候使用#{}是不行的,可以使用${},'${}'表示拼接符号。  在${value}只能使用value代表其中的参数。

<select id="find" resultType="user">
	<!-- 在${value}只能使用value代表其中的参数 -->
	select * from user where name like '%${value}%'
 </select>

          

  先总结这5点吧。

@猎码

业精于勤,荒于嬉;行成于思,毁于随。

猜你喜欢

转载自blog.csdn.net/qq_39115469/article/details/85106029