首页 > 其他分享 >mybatis学习第五部分:Mybatis复杂映射开发

mybatis学习第五部分:Mybatis复杂映射开发

时间:2022-11-19 22:34:26浏览次数:57  
标签:映射 List private 查询 mybatis user Mybatis id User

5.1 ⼀对⼀查询

5.1.1 ⼀对⼀查询的模型

⽤户表和订单表的关系为,⼀个⽤户有多个订单,⼀个订单只从属于⼀个⽤户

⼀对⼀查询的需求:查询⼀个订单,与此同时查询出该订单所属的⽤户

5.1.2⼀对⼀查询的语句

对应的sql语句:select * from orders o,user u where o.uid=u.id;

查询的结果如下:

5.1.3  创建OrderUser实体

public class Order {
private int id;
private Date ordertime;
private double total;
//代表当前订单从属于哪⼀个客户
private User user;
}
public class User {
private int id;
private String username;
private String password;
private Date birthday;
}

5.1.3  创建OrderMapper接⼝

public interface OrderMapper { 
    List<Order> findAll();
}

5.1.3  配置OrderMapper.xml

<mapper namespace="com.lagou.mapper.OrderMapper">
<resultMap id="orderMap" type="com.lagou.domain.Order">
<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>
</resultMap>
<select id="findAll" resultMap="orderMap">
select * from orders o,user u where o.uid=u.id
</select>
</mapper>

其中还可以配置如下:

<resultMap id="orderMap" type="com.lagou.domain.Order">
<result property="id" column="id"></result>
<result property="ordertime" column="ordertime"></result>
<result property="total" column="total"></result>
<association property="user" javaType="com.lagou.domain.User">
<result column="uid" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
</association>
</resultMap>

5.1.6 测试结果

OrderMapper mapper = sqlSession.getMapper(OrderMapper.class); 
List<Order> all = mapper.findAll(); for(Order order : all){ System.out.println(order); }

5.2 ⼀对多查询

5.2.1 ⼀对多查询的模型

⽤户表和订单表的关系为,⼀个⽤户有多个订单,⼀个订单只从属于⼀个⽤户

⼀对多查询的需求:查询⼀个⽤户,与此同时查询出该⽤户具有的订单

5.2.2 ⼀对多查询的语句

对应的sql语句:select *,o.id oid from user u left join orders o on u.id=o.uid;

查询的结果如下:

5.2.3  修改User实体

public class Order {
private int id;
private Date ordertime;
private double total;
//代表当前订单从属于哪⼀个客户
private User user;
}
public class User {
private int id;
private String username;
private String password;
private Date birthday;
//代表当前⽤户具备哪些订单
private List<Order> orderList;
}

5.2.3  创建UserMapper接⼝

public interface UserMapper { 
    List<User> findAll();
}

5.2.3  配置UserMapper.xml

<mapper namespace="com.lagou.mapper.UserMapper">
<resultMap id="userMap" type="com.lagou.domain.User">
<result column="id" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<collection property="orderList" ofType="com.lagou.domain.Order">
<result column="oid" property="id"></result><result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="userMap">
select *,o.id oid from user u left join orders o on u.id=o.uid
</select>
</mapper>

测试代码:

UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();   for(User user : all){
    System.out.println(user.getUsername());
    List<Order> orderList = user.getOrderList();

    for(Order order : orderList){       System.out.println(order);     }   System.out.println("----------------------------------"); }

5.2.6 测试结果

5.3 多对多查询

5.3.1 多对多查询的模型

⽤户表和⻆⾊表的关系为,⼀个⽤户有多个⻆⾊,⼀个⻆⾊被多个⽤户使⽤ 多对多查询的需求:查询⽤户同时查询出该⽤户的所有⻆⾊

5.3.2 多对多查询的语句

对应的sql语句:select u.,r.,r.id rid from user u left join user_role ur on u.id=ur.user_id inner join role r on ur.role_id=r.id;

查询的结果如下:

5.3.3 创建Role实体,修改User实体

public class User {
private int id;
private String username;
private String password;
private Date birthday;
//代表当前⽤户具备哪些订单
private List<Order> orderList;
//代表当前⽤户具备哪些⻆⾊
private List<Role> roleList;
}
public class Role {
private int id;
private String rolename;
}

5.3.4  添加UserMapper接⼝⽅法

List<User> findAllUserAndRole();

5.3.5  配置UserMapper.xml

<resultMap id="userRoleMap" type="com.lagou.domain.User">
<result column="id" property="id"></result>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<collection property="roleList" ofType="com.lagou.domain.Role">
<result column="rid" property="id"></result>
<result column="rolename" property="rolename"></result>
</collection>
</resultMap>
<select id="findAllUserAndRole" resultMap="userRoleMap">
select u.*,r.*,r.id rid from user u left join user_role ur on
u.id=ur.user_id
inner join role r on ur.role_id=r.id
</select>

5.3.6 测试代码

UserMapper mapper = sqlSession.getMapper(UserMapper.class); 
List<User> all = mapper.findAllUserAndRole(); for(User user : all){
  System.out.println(user.getUsername());
  List<Role> roleList = user.getRoleList();
  for(Role role : roleList){   System.out.println(role); } System.out.println("----------------------------------");

5.3.7 测试结果:

 

5.4 知识⼩结

MyBatis多表配置⽅式:

⼀对⼀配置:使⽤做配置

⼀对多配置:使⽤+做配置 多对多配置:使⽤+做配置

标签:映射,List,private,查询,mybatis,user,Mybatis,id,User
From: https://www.cnblogs.com/eagle888/p/16907379.html

相关文章

  • mybatis学习第四部分: Mybatis配置⽂件深⼊
    4.1  核⼼配置⽂件SqlMapConfig.xml4.1.1 MyBatis核⼼配置⽂件层级关系   4.2  MyBatis常⽤配置解析1)environments标签数据库环境的配置,⽀持多环境配置......
  • Mybatis出现Caused by: net.sf.jsqlparser.parser.ParseException: ....异常
    今天在开发项目中遇到了一个奇怪的异常,记录一下把!异常信息如下(截取了主要的部分)Causedby:net.sf.jsqlparser.parser.ParseException:Encountered""TOP""top""at......
  • mybatis学习第⼆部分:Mybatis相关概念
    2.1 对象/关系数据库映射(ORM)ORM全称Object/Relation Mapping:表示对象-关系映射的缩写ORM完成⾯向对象的编程语⾔到关系数据库的映射。当ORM框架完成映射后,程序员既可......
  • mybatis学习第三部分:Mybatis基本应⽤
    3.1 快速⼊⻔MyBatis官⽹地址:http://www.mybatis.org/mybatis-3/ 3.1.1 开发步骤:①添加MyBatis的坐标②创建user数据表③编写User实体类④编写映射⽂件UserMappe......
  • mybatis 之 sql语句写法集锦
    1、mapper接口中的方法存在批量数据操作,接口参数是List类型   在mybatis的xml文件中,使用foreach动态标签拼接SQL语句,每一条数据的更新语句对应一条update语句,......
  • mybatis源码解析1
    一、mybatis的简单使用根据mybatis官网提供的例子,需要这么几个步骤1、获取mybatis配置文件的输入流对象2、创建SqlSessionFactoryBuilder对象3、调用SqlSessionFactor......
  • MyBatisPlus 多数据源动态切换
    一、官方文档https://baomidou.com/pages/a61e1b/#%E6%96%87%E6%A1%A3-documentation二、操作步骤1、引入依赖版本号在下面 懒得单独复制了 一起贴出来了<!......
  • MyBatis练习(初)
    目录环境准备库,表idea准备题目一、简单查询环境准备MySQL,JDK17,Maven,MyBatis库,表库:CREATEDATABASEStuDB;学生表:Student(Sno,Sname,Ssex,Sage,Sdept)其中Sno为......
  • Mybatis中的${}和#{}区别(转载)
    动态sql是mybatis的主要特性之一,在mapper中定义的参数传到xml中之后,在查询之前,mybatis会对其进行动态解析。mybatis为我们提供了两种支持动态sql的语法:#{}以及......
  • mybatis中公共字段的自动填充
    在需要自动填充的字段上添加注解@TableField@ApiModelProperty(value="创建时间")@TableField(fill=FieldFill.INSERT)privateDategmtCreate;@A......