Mybatis框架详解2

输入映射和输出映射

输入映射

  • 基本类型的输入

int,string,double,等,这些mybatis框架都设置了别名。具体看上一篇博客

  • pojo类型的输入

Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称。

  • 包装的pojo类型的输入

就是对象中有引用类型的属性

  1. 创建包装的pojo类型的对象
public class QueryVo {
    private User user;
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
}
  1. 创建映射文件与sql
<mapper namespace="com.dao.UserDao">
<!-- 1、resultType:如果要返回数据集合,只需设定为每一个元素的数据类型
		 2、 包装的pojo取值通过 "."来获取
	-->
    <select id="getUserByName" parameterType="queryVo" resultType="com.pojo.User">
        select * from user where username like '%${user.username}%'
    </select>
</mapper>
  • Map类型的输入

paramrterType属性是Map类型,传入的参数是map类型。

输出映射

  • 基本类型的输出

resultType属性为基本类型,可以使用别名

  • pojo类型的输出

resultType属性为pojo类型,可以使用别名

  • pojo列表的输出

resultType属性为集合中每个元素的类型,可以使用别名

  • Map类型的输出

resultType属性为Map类型,可以使用别名

  • 输出resultMap

当pojo对象中的属性名和数据库中表的字段名不一样的时候,无法将数据库查询到的数据通过相应的set方法保存在pojo对象中去。为解决这一问题,所以引出resultMap标签的使用。将pojo中的属性与数据库中的表的字段之间进行映射。

问题:当表中的字段与pojo对象中的属性不一样的时候,使用resultMap
映射文件与sql

<mapper namespace="com.dao.OrderDao">
    <!--结果集的映射
        id:唯一表示,用来引用resultMap
        type:射成的pojo的类型。
    -->
    <resultMap id="orderResultMap" type="order">
        <!--用于主键与pojo对象属性的映射-->
        <id property="id" column="id"></id>

        <!--用于普通字段与pojo属性的映射-->
        <result property="userId" column="user_id"></result>
        <result property="number" column="number"></result>
        <result property="createtime" column="createtime"></result>
        <result property="note" column="note"></result>
    </resultMap>
    <!--resultMap属性不能与resultType属性一块使用-->
    <select id="getOrderAll" resultMap="orderResultMap">
        SELECT  * FROM `order`
    </select>
</mapper>

动态sql

  • if标签的使用

if标签的使用,有点像jsp中标签库中if标签的使用。

<!--
        if标签的使用,test属性是判断的条件,
        id是属性parameterType属性值的pojo对象的属性
    -->
<select id="getUserByIf" parameterType="user" resultType="user">
        select * from user where 1=1
        <if test="id != null">
            and id = #{id}
        </if>
        <if test="username != null and username !=''">
            and username like '%${username}%'
        </if>
    </select>

当把id改成id2,会报以下错误
在这里插入图片描述

  • where标签的使用

当使用where标签,就不能使用where关键字。
where标签的优点是不论你where标签后输出的条件是什么,mybatis都会很智能的处理,如果每个条件都不满足,就会查询所有,如果当输出的第一个条件是以and或者or开头的,会省略第一个and或者or,在where条件中,不需要考虑空格的问题,mybatis会智能的加上。

 <select id="getUserByWhere" resultType="user">
        select * from user
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="username != null and username !=''">
                and username like '%${username}%'
            </if>
        </where>
    </select>
  • foreach
 <select id="getUserByForeach" parameterType="queryvo" resultType="user">
        select * from user
        <where>
            <!-- collection: 要遍历的集合,来源于入参,mybatis框架不支持传入集合参数,使用包装的pojo
                 item:遍历集合中的每一项起的别名
                 open:表示该语句以什么开始
                 separator:每次迭代之间的分隔符
                 close:表示语句以什么结束
                 index:指定一个名字,其值表示遍历到每一项的索引
            -->
            <foreach collection="ids" item="uid" open="id in (" separator="," close=")" index="aa">
              #{uid}
            </foreach>
        </where>
    </select>
  • sql块语句

使用sql标签,之后使用include标签引入sql语句块,提高代码复用性

 	<sql id="sql_columns">
        `id`, `username`, `birthday`,`sex`,`address`
    </sql>
    <select id="getUserByName" parameterType="queryVo" resultType="com.pojo.User">
        select <include refid="sql_columns"/> from user where username like '%${user.username}%'
    </select>

关联查询

  • 一对一

根据订单表查询用户表,订单是主表,一个订单对应一个用户。是一对一的关系。

  1. 第一种方法:使用resultType
    创建OrderUser对象
  2. 第二种方法:结果集映射,resultMap
  • 一对多

Mybatis整合Spring的开发

逆向工程

发布了60 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44142032/article/details/94623617