33.MyBitis多表操作

 1.创建两张数据表user,orders(uid对应user中的id)

 orders类:

package entity;


import java.util.Date;

public class Orders {

  private long id;
  private Date ordertime;
  private double total;
  private long uid;

  /*该订单属于哪个用户*/
  private User user;

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  @Override
  public String toString() {
    return "Orders{" +
            "id=" + id +
            ", ordertime=" + ordertime +
            ", total=" + total +
            ", uid=" + uid +
            ", user=" + user +
            '}';
  }


  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }


  public Date getOrdertime() {
    return ordertime;
  }

  public void setOrdertime(Date ordertime) {
    this.ordertime = ordertime;
  }


  public double getTotal() {
    return total;
  }

  public void setTotal(double total) {
    this.total = total;
  }


  public long getUid() {
    return uid;
  }

  public void setUid(long uid) {
    this.uid = uid;
  }

}

user类: 

package entity;


import java.util.Date;

public class User {

  private String username;
  private String password;
  private long id;
  private Date birthday;


  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }


  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }


  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }


  public Date getBirthday() {
    return birthday;
  }

  public void setBirthday(Date birthday) {
    this.birthday = birthday;
  }

  @Override
  public String toString() {
    return "User{" +
            "username='" + username + '\'' +
            ", password='" + password + '\'' +
            ", id=" + id +
            ", birthday=" + birthday +
            '}';
  }
}

orderMapper接口:

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

orderMapper.xml: 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="Dao.OrderMapper">

    <resultMap id="orderMap" type="entity.Orders">
        <!--手动指定字段与实体属性的映射关系-->
        <!--column:数据表的字段名称-->
        <!--property:实体的属性名称-->
        <id column="oid" property="id"></id> <!--将查询出来的字段名oid封装给orders类的id属性-->
        <result column="ordertime" property="ordertime"></result><!--将查询出来的字段名ordertime封装给orders类的ordertime属性-->
        <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>
    </resultMap>

    <select id="findAll" resultMap="orderMap">
        select *,o.id oid from user u,orders o where u.id=o.uid
    </select>
</mapper>

 测试:

    @Test
    public void test7() throws IOException
    {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);

        List<Orders> ordersList = mapper.findAll();
        for(Orders order:ordersList)
        {
            System.out.println(order);
        }
        sqlSession.close();
    }

结果:

Orders{id=1, ordertime=Thu Jan 01 08:00:02 CST 1970, total=3000.0, uid=0, user=User{username='张三', password='123', id=1, birthday=null}}
Orders{id=2, ordertime=Thu Jan 01 08:00:02 CST 1970, total=4000.0, uid=0, user=User{username='张三', password='123', id=1, birthday=null}}
Orders{id=3, ordertime=Thu Jan 01 08:00:02 CST 1970, total=6000.0, uid=0, user=User{username='李四', password='123', id=2, birthday=null}}
Orders{id=4, ordertime=Thu Jan 01 08:00:02 CST 1970, total=500.0, uid=0, user=User{username='苏七', password='123', id=3, birthday=null}}
Orders{id=5, ordertime=Thu Jan 01 08:00:02 CST 1970, total=660.0, uid=0, user=User{username='哈哈', password='888', id=6, birthday=null}}
Orders{id=6, ordertime=Thu Jan 01 08:00:02 CST 1970, total=555.0, uid=0, user=User{username='测试', password='77', id=7, birthday=Tue Oct 26 00:23:37 CST 2021}}

也可以集中把数据表的user属性配到orders类的user中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="Dao.OrderMapper">

    <resultMap id="orderMap" type="entity.Orders">
        <!--手动指定字段与实体属性的映射关系-->
        <!--column:数据表的字段名称-->
        <!--property:实体的属性名称-->
        <id column="oid" property="id"></id> <!--将查询出来的字段名oid封装给orders类的id属性-->
        <result column="ordertime" property="ordertime"></result><!--将查询出来的字段名ordertime封装给orders类的ordertime属性-->
        <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中的user)
            javaType: 当前实体中的属性类型(private User user中的User)
        -->
        <association property="user" javaType="entity.User">
            <result column="uid" property="id"></result><!--将查询出来的字段名uid封装给user类的id属性-->
            <result column="username" property="username"></result>
            <result column="password" property="password"></result>
            <result column="birthday" property="birthday"></result>
        </association>
    </resultMap>

    <select id="findAll" resultMap="orderMap">
        select *,o.id oid from user u,orders o where u.id=o.uid
    </select>
</mapper>

代码实现:查询所有用户,包括订单信息

需要修改User表,添加订单集合属性

package entity;


import java.util.Date;
import java.util.List;

public class User {

  private String username;
  private String password;
  private long id;
  private Date birthday;

  //当前用户存在哪些订单
  private List<Orders> ordersList;

  public List<Orders> getOrdersList() {
    return ordersList;
  }

  public void setOrdersList(List<Orders> ordersList) {
    this.ordersList = ordersList;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }


  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }


  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public Date getBirthday() {
    return birthday;
  }

  public void setBirthday(Date birthday) {
    this.birthday = birthday;
  }

  @Override
  public String toString() {
    return "User{" +
            "username='" + username + '\'' +
            ", password='" + password + '\'' +
            ", id=" + id +
            ", birthday=" + birthday +
            ", ordersList=" + ordersList +
            '}';
  }
}

 userMapper接口:

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

 userMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="Dao.UserMapper">
    <resultMap id="userMap" type="entity.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:集合名称(private List<Orders> ordersList中的ordersList)
            ofType:当前集合中的数据类型private List<Orders> ordersList中的Orders)
        -->
        <collection property="ordersList" ofType="entity.Orders">
            <id column="oid" property="id"></id>
            <result column="ordertime" property="ordertime"></result>
            <result column="total" property="total"></result>
            <result column="uid" property="uid"></result>
        </collection>
    </resultMap>
    
    <select id="findAll" resultMap="userMap">
        select *,o.id oid from user u,orders o where u.id=o.uid
    </select>
</mapper>

 测试:

    @Test
    public void test8() throws IOException
    {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List<User> userList = mapper.findAll();
        for(User u:userList)
        {
            System.out.println(u);
        }
        sqlSession.close();
    }

 sys_user类:

package entity;


import java.util.List;

public class SysUser {

  private long id;
  private String username;
  private String email;
  private String password;
  private String phoneNum;

  //描述的是当前用户具备哪些角色
  private List<Role> roleList;

  public List<Role> getRoleList() {
    return roleList;
  }

  public void setRoleList(List<Role> roleList) {
    this.roleList = roleList;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }


  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }


  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }


  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }


  public String getPhoneNum() {
    return phoneNum;
  }

  public void setPhoneNum(String phoneNum) {
    this.phoneNum = phoneNum;
  }

  @Override
  public String toString() {
    return "SysUser{" +
            "id=" + id +
            ", username='" + username + '\'' +
            ", email='" + email + '\'' +
            ", password='" + password + '\'' +
            ", phoneNum='" + phoneNum + '\'' +
            ", roleList=" + roleList +
            '}';
  }
}

sys_role类:

package entity;


public class SysRole {

  private long id;
  private String roleName;
  private String roleDesc;


  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }


  public String getRoleName() {
    return roleName;
  }

  public void setRoleName(String roleName) {
    this.roleName = roleName;
  }


  public String getRoleDesc() {
    return roleDesc;
  }

  public void setRoleDesc(String roleDesc) {
    this.roleDesc = roleDesc;
  }

  @Override
  public String toString() {
    return "SysRole{" +
            "id=" + id +
            ", roleName='" + roleName + '\'' +
            ", roleDesc='" + roleDesc + '\'' +
            '}';
  }
}

SysUserMapper接口:

public interface SysUserMapper
{
    public List<SysUser> findUserAndRole();
}

SysUserMappper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="Dao.SysUserMapper">
    
    <resultMap id="userRoleMap" type="entity.SysUser">
        <id column="userId" property="id"></id>
        <result column="username" property="username"></result>
        <result column="email" property="email"></result>
        <result column="password" property="password"></result>
        <result column="phoneNum" property="phoneNum"></result>
        <collection property="roleList" ofType="entity.SysRole">
            <id column="roleId" property="id"></id>
            <result column="roleName" property="roleName"></result>
            <result column="roleDesc" property="roleDesc"></result>
        </collection>
    </resultMap>
    
    <select id="findUserAndRole" resultMap="userRoleMap">
        /*select * from sys_user u,sys_user_role ur,sys_role r where u.id=ur.userid and r.id=ur.roleId*/
        select * from sys_user u,sys_user_role ur,sys_role r where u.id=ur.userid and r.id=ur.roleId
    </select>

</mapper>

测试:

    @Test
    public void test9() throws IOException
    {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        SysUserMapper mapper = sqlSession.getMapper(SysUserMapper.class);

        List<SysUser> userList = mapper.findUserAndRole();
        for(SysUser u:userList)
        {
            System.out.println(u);
        }
        sqlSession.close();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值