您的位置:首页 > 其它

MyBatis学习第三天--08-11

2020-08-11 17:58 218 查看

关联映射,注解开发

关联映射

MyBatis中默认会根据从数据库查询出的字段名与指定的类型的属性名进行一一映射关系,但是如果此类型中包含
对象、集合等特殊的属性,MyBatis则无法识别,这时需要我们手动去指定表与属性的映射关系
出现此现象的情况为三种 1.一对一 2. 一对多 3. 多对多

示例说明
表之间的关系
order(1)----user(n)----user_role中间表---role(n)

** OrderMapper接口
public interface OrderMapper {
List<Order> findAll(); // 查询所有订单信息和该订单对应的用户信息
}

** UserMapper接口
public interface UserMapper {
List<User> findAll(); // 查询所有用户信息和该用户对应的订单信息
List<User> findUserRoleAll(); // 查询所有用户信息和该用户对应的角色信息
}

** orderMapper映射文件
<mapper namespace="dao.OrderMapper">
<resultMap id="orderMap" type="order">
//  映射关系配置(一对一的情况) type为该映射的java类型
<id column="oid" property="id"/>
// id为主键映射 column对应数据库查询的字段 property对应JavaBean的属性名
<result column="order_time" property="order_time"/> // 普通属性的配置
//     <result column="uid" property="user.id"/>
//    <result column="username" property="user.username"/>
//    <result column="password" property="user.password"/>
// 这三行是对user属性的另一种方法配置映射
<association property="user" javaType="user">
// 封装user属性 property为属性名 javaType为需要封装的java类型(已定义别名)
// 内置的配置和上面一致
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</association>
</resultMap>
<select id="findAll" resultMap="orderMap"> // resultMap引用映射关系,对应映射的id
select *,orders.id oid from user,orders where orders.uid=user.id
</select>
</mapper>

** userMapper映射文件
<mapper namespace="dao.UserMapper">
<resultMap id="userMapper" type="user">
// 映射关系配置 一对多的情况
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<collection property="orderList" ofType="order">
//collection 为属性为集合时的配置 property 属性名 ofType 集合的泛型类型
// 内置配置与一对一一致
<id column="oid" property="id"/>
<result column="order_time" property="order_time"/>
</collection>
</resultMap>
<select id="findAll" resultMap="userMapper"> // 引用映射关系id
select *,orders.id oid from orders,user where user.id=orders.uid
</select>

<resultMap id="userRoleMapper" type="user">
// 映射关系配置 多对多的情况(与一对多类似)
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<collection property="roleList" ofType="role">
<id column="rid" property="id"/>
<result column="name" property="name"/>
</collection>
</resultMap>
<select id="findUserRoleAll" resultMap="userRoleMapper">
select * from user,user_role ur,role where user.id=ur.uid and role.id=ur.rid
</select>
</mapper>

注解开发

常用注解
@Select @Insert @Delete @Update
** 作用在方法上 对应增删改查 属性value为SQL语句
@Results 封装多个结果集,属性为@Result类型数组
@Result 封装结果集
** 属性 column 指定数据库查询的字段  property 指定JavaBean属性名
@One 实现一对一结果集的封装
@Many 实现一对多或多对多结果集的封装

示例说明
表的关系与上面一致
需要首先在核心配置文件中引入来加载映射关系
<mappers>
<package name="dao"/> // 加载dao包下的映射关系
</mappers>

** RoleDao接口
public interface RoleDao {
@Select("select * from role,user_role ur where #{uid}=ur.uid and role.id=ur.rid")
List<Role> findRoleByUid(Integer id);
// 根据用户id来查询用户对应的角色信息
}

** OrderDao接口
public interface OrderDao {
@Select("select * from orders where uid=#{id}")
List<Order> findOrderByUid(Integer id);
// 通过用户id来查询对应订单
//    @Select("select *,orders.id oid from user,orders where orders.uid=user.id")
//    @Results({
//            @Result(column = "oid",property = "id"),
//            @Result(column = "order_time",property = "order_time"),
//            @Result(column = "uid",property = "user.id"),
//            @Result(column = "username",property = "user.username"),
//            @Result(column = "password",property = "user.password")
//    })
// 此为一对一关系的另一种配置方式
@Select("select * from orders")
@Results({
@Result(column = "id",property = "id"),
// 这俩个属性与xml代表同样的意思,可添加 id=true 来注明主键
@Result(column = "order_time",property = "order_time"),
@Result(
property = "user", // JavaBean属性名
javaType = User.class, // JavaBean属性的java类型
column = "uid", // 来标注此字段将作为参数进行二次查询
one = @One(select = "dao.UserDao.findUserById")
// 通过 column 属性来借助别的方法来进行二次查询,select为全限定名
)
})
List<Order> findAll();
}

** UserDao接口
public interface UserDao {
@Select("select * from user") // 查询所有
List<User> findAll();

@Select("select * from user where id=#{id}") // 通过id查询
User findUserById(Integer id);

@Insert("insert into user values (#{id},#{username},#{password})") // 添加用户
void save(User user);

@Delete("delete from user where id=#{id}") // 删除用户
void delete(Integer id);

@Update("update user set username=#{username},password=#{password} where username=#{username}")
void update(User user); // 修改用户

@Select("select * from user")
@Results({               // 一对多的映射配置
@Result(id = true,column = "id",property = "id"),
@Result(column = "username",property = "username"),
@Result(column = "password",property = "password"),
@Result(
property = "orderList",
column = "id",
javaType = List.class,
many = @Many(select = "dao.OrderDao.findOrderByUid")
)
})
List<User> findUserAndOrderAll();

@Select("select * from user")
@Results({              // 多对多的映射配置
@Result(column = "id",property = "id"),
@Result(column = "username",property = "username"),
@Result(column = "password",property = "password"),
@Result(
property = "roleList",
javaType = List.class,
column = "id",
many = @Many(select = "dao.RoleDao.findRoleByUid")
)
})
List<User> findUserAndRoleAll();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: