Mybatis笔记(3)

一、多表查询

1.1 一对一查询

订单用户(一个订单属于一个)

Order实体类有user属性

配置resultMap(OrderMap)

<select id="findAll" resultMap="orderMap">
         SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
</select>
<resultMap id="orderMap" type="order">
        <!--手动指定字段与实体属性的映射关系
            column: 数据表的字段名称
            property:实体的属性名称
        -->
        <id column="oid" property="id"></id>
        <result column="ordertime" property="ordertime"></result>
        <result column="total" property="total"></result>
        <!--<result column="uid" property="user.id"></result>
        <result column="username" property="user.username"></result>
        <result column="password" property="user.password"></result>
        <result column="birthday" property="user.birthday"></result>-->

        <!--
            property: 当前实体(order)中的属性名称(private User user)
            javaType: 当前实体(order)中的属性的类型(User)
        -->
        <association property="user" javaType="user">
            <id column="uid" property="id"></id>
            <result column="username" property="username"></result>
            <result column="password" property="password"></result>
            <result column="birthday" property="birthday"></result>
        </association>

    </resultMap>

查询结果表:

1.2 一对多查询

订单用户(一个用户下达多个订单)

User实体类有List属性

配置resultMap(UserMap)

<select id="findAll" resultMap="userMap">
        SELECT *,o.id oid FROM USER u,orders o WHERE u.id=o.uid
</select>
<resultMap id="userMap" type="user">
        <id column="uid" property="id"></id>
        <result column="username" property="username"></result>
        <result column="password" property="password"></result>
        <result column="birthday" property="birthday"></result>
        <!--配置集合信息
            property:集合名称
            ofType:当前集合中的数据类型
        -->
        <collection property="orderList" ofType="order">
            <!--封装order的数据-->
            <id column="oid" property="id"></id>
            <result column="ordertime" property="ordertime"></result>
            <result column="total" property="total"></result>
        </collection>
</resultMap>

1.3 多对多查询

比一对多查询多一张表

<select id="findUserAndRoleAll" resultMap="userRoleMap">
        SELECT * FROM USER u,sys_user_role ur,sys_role r WHERE u.id=ur.userId AND ur.roleId=r.id
    </select>
<resultMap id="userRoleMap" type="user">
        <!--user的信息-->
        <id column="userId" property="id"></id>
        <result column="username" property="username"></result>
        <result column="password" property="password"></result>
        <result column="birthday" property="birthday"></result>
        <!--user内部的roleList信息-->
        <collection property="roleList" ofType="role">
            <id column="roleId" property="id"></id>
            <result column="roleName" property="roleName"></result>
            <result column="roleDesc" property="roleDesc"></result>
        </collection>
    </resultMap>

小结

MyBatis多表配置方式:

一对一配置:使用做配置

一对多配置:使用+做配置

多对多配置:使用+做配置

二、MyBatis注解开发

2.1 常用注解

注解 目标 对应的XML标签
@CacheNamespace
@CacheNamespaceRef
@Results 方法
@Result 方法
@One 方法
@Many 方法
@Insert@Update@Delete 方法
@InsertProvider@UpdateProvider@DeleteProvider@SelectProvider 方法
@Param 参数 N/A
@Options 方法 映射语句的属性
@select 方法

2.1 简单查询

@Insert 简单插入

@Insert(" insert into user(name,sex,age) values(#{name},#{sex},#{age} " )

int saveUser(User user);

@Update 简单更新

@Update("update user set name= #{name} ,sex = #{sex},age =#{age} where id = #{id}")

void updateUserById(User user);

@Delete 简单删除

@Delete("delete from  user  where id =#{id} ")

void deleteById(Integer id);

@Select 简单查询

一对一第一种方法)

@Select(" Select * from user ")
@Results({
    //id = true代表主键
    @Result(id = true, column = "id", property = "id"),
    @Result(column = "name", property = "name"),
    @Result(column = "tel", property = "tel"),
    @Result(column = "birth", property = "birth"),
    @Result(column = "address", property = "address")
})
List<User> queryAllUser()

@One

一对一第二种方法)

@Select(" select * from orders ")
    @Results({
        @Result(column = "id",property = "id"),
        @Result(column = "ordertime",property = "ordertime",
                @Result(
                    property = "user", //要封装的属性名
                    column="uid",// 根据哪个字段去查询user表数据
                    javaType = User.class,//要封装的实体类型
                    //select属性 代表查询哪个接口的方法获得数据
                    //one指示我们,查询出来的结果只有一个。
                    one = @One(select="gyb.UserMapper.findById")
                 )                    
                      
    })
    public List<Order> findAll();

@Many

一对多

User内含有orderList

	@Select(" select * from dept")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
        	@Result(
                //封装user内的userlist属性
                property = "orderList",
                //数据库内字段
                column = "id",
                //结果类型
             	javaType = "List.class",
                //使用方法,代表查询多个结果
                many = @Many(select = "gyb.OrderMapper.findById")
                
            )
            
    })
    public List<User> findUserAndOrderAll();
posted @ 2021-08-26 19:01  橡皮筋儿  阅读(38)  评论(0编辑  收藏  举报