有2个实体:用户、会员卡,一个用户只能办理一张会员卡,即一对一。
user_tb :
需要在一方引入另一方的主键作为外键。
card_tb:
使用扩展类
(1)在pojo包下新建User类:
package com.chy.pojo; public class User { private Integer id; //主键 private String name; //姓名 private String tel; //手机号 private String address; //地址 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", tel='" + tel + '\'' + ", address='" + address + '\'' + '}'; } }
(2)在pojo包下新建User的扩展类UserExt,继承User,并把Card的属性添加进来,提供对应的getter、setter方法。
package com.chy.pojo; public class UserExt extends User { private Integer no; private float money; public Integer getNo() { return no; } public void setNo(Integer no) { this.no = no; } public float getMoney() { return money; } public void setMoney(float money) { this.money = money; } @Override public String toString() { return super.toString()+",Card{" + "no=" + no + ", money=" + money + '}'; } }
先alt+insert插入toString(),再拼接上User的toString(),然后修改下就ok。
(3)编写UserMapper接口、UserMapper.xml
package com.chy.mapper; import com.chy.pojo.UserExt; public interface UserMapper { public UserExt queryUserExtById(Integer id); }
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
(4)使用
package com.chy.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class MyBatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
SqlSession sqlSession = MyBatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); UserExt userExt = mapper.queryUserExtById(1); System.out.println(userExt); sqlSession.close();
使用扩展类可以实现一对一的关联查询,但没有体现实体之间的关联关系(一个模型中包含另一个模型)。
使用嵌套查询
(1)给2个“一”都编写pojo类,需要在一个“一”中关联另一个“一”
package com.chy.pojo; public class User { private Integer id; //主键 private String name; //姓名 private String tel; //手机号 private String address; //地址 private Card card; //与之关联的Card public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Card getCard() { return card; } public void setCard(Card card) { this.card = card; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", tel='" + tel + '\'' + ", address='" + address + '\'' + ", card=" + card + '}'; } }
package com.chy.pojo; public class Card { private Integer no; //会员卡编号 private Float money; //余额 public Integer getNo() { return no; } public void setNo(Integer no) { this.no = no; } public Float getMoney() { return money; } public void setMoney(Float money) { this.money = money; } @Override public String toString() { return "Card{" + "no=" + no + ", money=" + money + '}'; } }
外键是用来辅助sql操作的,并不是实体的属性,所以pojo类一般不包含外键字段。
(2)给这2个pojo类都编写Mapper接口、xml映射文件
public interface CardMapper { public Card queryCardByUserId(Integer no); }
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
package com.chy.mapper; import com.chy.pojo.User; public interface UserMapper { public User queryUserById(Integer id); }
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
sql语句都是查询当前pojo类对应的数据表,但UserMapper使用
- property属性指定当前pojo类中表示另一个“一”的属性名
- column属性指定当前数据表中关联另一个“一”的列(外键)
- javaType属性指定与当前pojo类关联的另一个“一”的数据类型。
- select属性指定要使用的另一个“一”的哪个sql元素关联(namespace+id),执行当前
(3)使用
package com.chy.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class MyBatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
package com.chy.test; import com.chy.mapper.UserMapper; import com.chy.pojo.User; import com.chy.utils.MyBatisUtils; import org.apache.ibatis.session.*; import java.io.IOException; public class Test { public static void main(String[] args) { SqlSession sqlSession = MyBatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.queryUserById(1); System.out.println(user); sqlSession.close(); } }
使用嵌套查询体现了实体之间的关联关系,但一条查询会触发另一个与之关联的查询,另一个查询如果有与之关联的查询,也会触发.....链式反应,极大地降低了查询效率和数据库的性能,不推荐。
这2种方式都不推荐,了解即可。
推荐使用关联结果,mybatis的一对一、一对多、多对多基本都是使用关联结果来实现,因为关联结果比较常用,所以后续单独用一篇随笔列出来。