zoukankan      html  css  js  c++  java
  • mybatis一对一关联查询

    1.

     1 package com.test.entity;
     2 
     3 import java.util.Date;
     4 
     5 /**
     6  * 
     7  * @author Mr
     8  * 跟数据库表一一对应
     9  */
    10 public class User {
    11     
    12     private int id;
    13     private String username;
    14     private Date birthday;
    15     private String sex;
    16     private String address;
    17     public int getId() {
    18         return id;
    19     }
    20     public void setId(int id) {
    21         this.id = id;
    22     }
    23     public String getUsername() {
    24         return username;
    25     }
    26     public void setUsername(String username) {
    27         this.username = username;
    28     }
    29     public Date getBirthday() {
    30         return birthday;
    31     }
    32     public void setBirthday(Date birthday) {
    33         this.birthday = birthday;
    34     }
    35     public String getSex() {
    36         return sex;
    37     }
    38     public void setSex(String sex) {
    39         this.sex = sex;
    40     }
    41     public String getAddress() {
    42         return address;
    43     }
    44     public void setAddress(String address) {
    45         this.address = address;
    46     }
    47     public User() {
    48         super();
    49         // TODO Auto-generated constructor stub
    50     }
    51     public User(String username, Date birthday, String sex,
    52             String address) {
    53         super();
    54         this.username = username;
    55         this.birthday = birthday;
    56         this.sex = sex;
    57         this.address = address;
    58     }
    59     public User(int id, String username, Date birthday, String sex,
    60             String address) {
    61         super();
    62         this.id = id;
    63         this.username = username;
    64         this.birthday = birthday;
    65         this.sex = sex;
    66         this.address = address;
    67     }
    68     @Override
    69     public String toString() {
    70         return "User [id=" + id + ", username=" + username + ", birthday="
    71                 + birthday + ", sex=" + sex + ", address=" + address + "]";
    72     }
    73     
    74 }

    2.

     1 package com.test.entity;
     2 
     3 import java.util.Date;
     4 
     5 /**
     6  * 订单实体类
     7  * @author Mr
     8  * 因为这个类不能完成下面的sql语句的映射,要新建pojo
     9  */
    10 public class Orders {
    11     /* SELECT orders.*,userss.username,userss.sex,userss.address FROM orders,userss
    12      *  WHERE orders.user_id=userss.id*/
    13     private Integer id;
    14     private Integer userId;
    15     private String numbers;
    16     private Date createtime;
    17     private String note;
    18     
    19     //使用resultMap添加user
    20     private User user;
    21     
    22     public User getUser() {
    23         return user;
    24     }
    25     public void setUser(User user) {
    26         this.user = user;
    27     }
    28     @Override
    29     public String toString() {
    30         return "Orders [id=" + id + ", userId=" + userId + ", numbers="
    31                 + numbers + ", createtime=" + createtime + ", note=" + note
    32                 + "]";
    33     }
    34     public Orders(Integer id, Integer userId, String numbers, Date createtime,
    35             String note) {
    36         super();
    37         this.id = id;
    38         this.userId = userId;
    39         this.numbers = numbers;
    40         this.createtime = createtime;
    41         this.note = note;
    42     }
    43     public Orders() {
    44         super();
    45         // TODO Auto-generated constructor stub
    46     }
    47     public Integer getId() {
    48         return id;
    49     }
    50     public void setId(Integer id) {
    51         this.id = id;
    52     }
    53     public Integer getUserId() {
    54         return userId;
    55     }
    56     public void setUserId(Integer userId) {
    57         this.userId = userId;
    58     }
    59     public String getNumbers() {
    60         return numbers;
    61     }
    62     public void setNumbers(String numbers) {
    63         this.numbers = numbers;
    64     }
    65     public Date getCreatetime() {
    66         return createtime;
    67     }
    68     public void setCreatetime(Date createtime) {
    69         this.createtime = createtime;
    70     }
    71     public String getNote() {
    72         return note;
    73     }
    74     public void setNote(String note) {
    75         this.note = note;
    76     }
    77 }

    3.

     1 package com.test.entity;
     2 
     3 import java.util.Date;
     4 
     5 /**
     6  * 它的存在是为了解决下面的sql语句的映射
     7  * SELECT orders.*,userss.username,userss.sex,userss.address 
     8  * FROM orders,userss WHERE orders.user_id=userss.id
     9  * 
    10  * @author Mr
    11  *
    12  */
    13 public class OrdersCustom extends Orders{
    14     //解决这些映射问题
    15     //userss.username,userss.sex,userss.address
    16     private String username;
    17     private String sex;
    18     private String address;
    19     public String getUsername() {
    20         return username;
    21     }
    22     public void setUsername(String username) {
    23         this.username = username;
    24     }
    25     public String getSex() {
    26         return sex;
    27     }
    28     public void setSex(String sex) {
    29         this.sex = sex;
    30     }
    31     public String getAddress() {
    32         return address;
    33     }
    34     public void setAddress(String address) {
    35         this.address = address;
    36     }
    37     @Override
    38     public String toString() {
    39         return "OrdersCustom [username=" + username + ", sex=" + sex
    40                 + ", address=" + address + "]";
    41     }
    42 }

    4.

     1 <!--  -->
     2 <resultMap type="com.test.entity.Orders" id="OrdersUserResultMap">
     3     <id column="id" property="id"/>
     4     <result column="user_id" property="userId"/>
     5     <result column="numbers" property="numbers"/>
     6     <result column="createtime" property="createtime"/>
     7     <result column="note" property="note"/>
     8         <!-- 配置关联映射
     9         property 将关联查询的信息映射到每个order的属性
    10         user到orders:一个用户可以下多个订单:一对多
    11         orders到user:一个订单只能由一个用户创建:一对一
    12          -->
    13         <association property="user" javaType="com.test.entity.User">
    14             <id column="id" property="id"/>
    15             <result column="username" property="username"/>
    16             <result column="sex" property="sex"/>
    17             <result column="address" property="address"/>
    18         </association>
    19 </resultMap>
    20 <!-- 引用 resultMap-->
    21 <select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap">
    22     SELECT orders.*,
    23     userss.username,
    24     userss.sex,
    25     userss.address 
    26     FROM orders,userss 
    27     WHERE orders.user_id=userss.id
    28 </select>

    5.

     1 package com.test.demo;
     2 
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 import java.util.List;
     6 
     7 import org.apache.ibatis.io.Resources;
     8 import org.apache.ibatis.session.SqlSession;
     9 import org.apache.ibatis.session.SqlSessionFactory;
    10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    11 
    12 import com.test.dao.OrdersMapperCustom;
    13 import com.test.entity.Orders;
    14 
    15 /**
    16  * 使用resultMap配置关联查询
    17  * @author Mr
    18  *
    19  */
    20 public class Test10 {
    21 
    22     public static void main(String[] args) throws IOException {
    23         //mybatis核心配置文件
    24         String resource = "SqlMapConfig.xml";
    25         //读取配置文件
    26         InputStream inputStream = Resources.getResourceAsStream(resource);
    27         //获取工厂
    28         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    29         //获取数据库操作对象
    30         SqlSession sqlSession = sqlSessionFactory.openSession();
    31         
    32         OrdersMapperCustom omc = sqlSession.getMapper(OrdersMapperCustom.class);
    33         
    34         List<Orders> list = omc.findOrdersUserResultMap();
    35         
    36         for (Orders orders : list) {
    37             System.out.println(orders.getUser().getUsername());
    38             System.out.println(orders.getUserId());
    39         }
    40 
    41     }
    42 
    43 }
  • 相关阅读:
    【转】正则基础之——/b 单词边界
    【转】空格变成问号的怪问题
    【转】正则基础之——NFA引擎匹配原理
    【转】 .NET正则基础之——平衡组
    【转】正则基础之——环视
    【转】正则应用之——日期正则表达式
    【转】正则基础之——小数点
    【转】[ ] 字符组(Character Classes)
    【转】正则表达式30分钟入教程
    【转】正则基础之——非捕获组
  • 原文地址:https://www.cnblogs.com/myhzb/p/7651682.html
Copyright © 2011-2022 走看看