mybatis中的动态sql语句和多表查询

一、当查询条件不确定时,就可使用动态sql语句进行查询。
1、if、where标签的使用:

<!--使用动态sql语句进行查询,可以添加多个if标签-->
<select id="findByCondition" parameterType="user" resultType="user">
    select * from user
    <where>
        <if test="username != null">
            and username = #{username}
        </if>
    </where>
</select>

2、当查询条件在某个区间时还可以使用foreach标签:

<!--当查询条件在一个区间时-->
<select id="findInIds" resultType="User" parameterType="QueryVo">
    select * from user
    <where>
        <if test="ids != null and ids.size()>0">
            <foreach collection="ids" open="and id in (" close=")" item="uid" separator=",">
                #{uid}
            </foreach>
        </if>
    </where>
</select>

foreach标签中的collection属性的值是参数实体类QueryVo中的属性名,item属性的值则决定了接下来取值时的写法,即uid->#{uid}。

注:当我们使用动态sql语句时,会发现每个查询都有一段重复的sql:select * from user,这时我们可以使用sql标签将重复语句提取出来,并在select标签中使用include标签进行调用:

<sql id="defaultSql" >
    select * from user
</sql>
<!--使用动态sql语句进行查询,可以添加多个if标签-->
<select id="findByCondition" parameterType="user" resultType="user">
    <include refid="defaultSql"></include>
    <where>
        <if test="username != null">
            and username = #{username}
        </if>
    </where>
</select>

二、多表间的关系有一对一、一对多、多对多,在mybatis中,它把多对一的情况也看做一对一。当我们需要一次查询出多个表的内容,就可以使用多表查询。
一对一或多对一查询操作:从表实体应该包含一个主表实体的对象引用,并在映射配置文件的resultMap标签中使用association标签,配置封装主表实体,映射配置文件中的代码:

<mapper namespace="com.itmao.dao.IAccountDao" >
    <!--定义封装account和user的resultMap-->
    <resultMap id="accountUserMap" type="Account">
        <id property="id" column="id"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <!--配置封装user的内容,javaType属性是指被封装对象的类型-->
        <association property="user" column="uid" javaType="User">
            <id property="id" column="id"></id>
            <result property="username" column="username"></result>
            <result property="address" column="address"></result>
            <result property="sex" column="sex"></result>
            <result property="birthday" column="birthday"></result>
        </association>
    </resultMap>
    <!--配置查询所有-->
    <select id="findAllAccountUser" resultMap="accountUserMap">
        select u.*,a.id as aid,a.uid,a.money from account_user a,user u where u.id = a.uid
    </select>
</mapper>

一对多和多对多操作:主表实体应该包含从表实体的集合引用,并在映射配置文件的resultMap标签中使用collection标签,配置从表实体集合的映射,映射配置文件中的代码:

<mapper namespace="com.itmao.dao.IRoleDao" >
    <!--定义User的ResultMap-->
    <resultMap id="roleUserMap" type="Role">
        <id property="id" column="rid"></id>
        <result property="roleName" column="role_Name"></result>
        <result property="roleDesc" column="role_Desc"></result>
        <collection property="users" ofType="User">
            <id property="id" column="id"></id>
            <result property="username" column="username"></result>
            <result property="birthday" column="birthday"></result>
            <result property="sex" column="sex"></result>
            <result property="address" column="address"></result>
        </collection>
    </resultMap>
    <!--配置查询所有-->
    <select id="findAllRoleUser" resultMap="roleUserMap">
        SELECT u.*,r.id as rid,r.ROLE_NAME,r.ROLE_DESC from
         role r left outer join user_role ur on r.id = ur.rID LEFT JOIN user u on ur.uID = u.ID
    </select>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值