一对一查询
场景描述
一张银行卡只能有一个用户持有,那么银行卡和用户之间的关系就是一对一的关系。
操作步骤
1.创建一个名为infoManage数据库,并创建用户表User、用户银行卡账号信息表Card。代码如下:
#创建infoManage数据库
create database infoMange default character set utf8 collate utf8_general_ci;
#切换数据库
use infoMange;
#创建用户表User
create table if not exists user(
id int not null auto_increment primary key,
username varchar(20) not null unique,
mobile varchar(11) not null unique,
gender varchar(6) not null default 'male',
age int not null
);
#创建银行卡账号信息表Card
create table if not exists card(
id int not null auto_increment primary key,
cardNum varchar(20) not null unique,
userId int not null,
foreign key(userId) references user(id)
);
#插入用户信息
insert into
user(username,mobile,gender,age)
values
('张三','13888888888','male',18),
('李四','13888888889','male',19),
('小花','13888888887','female',18),
('小柳','13888888886','female',21);
#插入银行卡信息
insert into
card(cardNum,userId)
values
(6227888825370110828,1),
(6227888825370110827,4),
(6227888825370110825,3),
(6227888825370110824,2),
(6227888825370110823,1),
(6227888825370110821,3);
2.创建用户表实体类User及银行卡表实体类Card,代码如下:
用户表实体类User
//用户表实体类User
package com.zhq.pojo
public class User {
private int id;
private String username;
private String mobile;
private String gender;
private int age;
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return username;
}
public void setMobile(String mobile){
this.mobile = mobile;
}
public String getMobile(){
return mobile;
}
public void setGender(String gender){
this.gender = gender;
}
public String getGender(){
return gender;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
银行卡信息表card实体类Card
package com.zhq.pojo
public class Card {
private int id;
private String cardNum;
private User user;
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setCardNum(String cardNum){
this.cardNum = cardNum;
}
public String getCardNum(){
return cardNum;
}
public void setUser(User user){
this.user = user;
}
public User getUser(){
return user;
}
}
3.配置XML映射文件CardMapper.xml、UserMapper.xml。配置信息如下:
CardMapper.xml配置信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhq.dao.CardDao">
<!--执行两次查询-->
<resultMap type="card" id="cardBaseMap">
<id property="id" column="id" jdbcType=INTEGER/>
<result property="cardNum" column="cardNum" jdbcType=VARCHAR/>
<association property="user" column="user_id" javaType="user" select="getUser"/>
</resultMap>
<!--分两步查询-->
<select id="getUser" parameterType="int" resultType="user">
select * from user where id = #{id};
</select>
<select id="selectById" parameterType="int" resultMap="cardBaseMap">
select * from card where id = #{id};
</select>
<!--执行一次查询-->
<resultMap type="card" id="cardJoinUserBaseMap">
<id property="id" column="id" jdbcType=INTEGER/>
<result property="cardNum" column="cardNum" jdbcType=VARCHAR/>
<association property="user" javaType="user">
<id property="id" column="cid" jdbcType=INTEGER/>
<result property="username" column="username" jdbcType=VARCHAR/>
<result property="mobile" column="mobile" jdbcType=VARCHAR/>
<result property="gender" column="gender" jdbcType=VARCHAR/>
<result property="age" column="age" jdbcType= INTEGER/>
</association>
</resultMap>
<!---联合查询->
<select id="selectByJoinUser" parameterType="int" resultType="cardJoinUserBaseMap">
select *,u.id uid from user u,card c where c.id = #{id} and c.userId=user.id;
</select>
<select id="selectAll" resultType="card">
select * from card;
</select>
</mapper>
UserMapper.xml配置信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhq.dao.UserDao">
<resultMap type="user" id="userBaseMap">
<id property="id" column="id" jdbcType=INTEGER/>
<result property="username" column="username" jdbcType=VARCHAR/>
<result property="mobile" column="mobile" jdbcType=VARCHAR/>
<result property="gender" column="gender" jdbcType=VARCHAR/>
<result property="age" column="age" jdbcType= INTEGER/>
</resultMap>
<select id="selectById" parameterType="int" resultMap="userBaseMap">
select * from user where id = #{id};
</select>
<select id="selectAll" resultType="user">
select * from user;
</select>
</mapper>
4.配置mybatis_config文件,注册CardMapper.xml、UserMapper.xml。配置信息如下:
<mapper resource="com/zhq/mapper/CardMapper.xml"/>
<mapper resource="com/zhq/mapper/UserMapper.xml"/>
5.在com.zhq.test包下新创建一个名为CardTest测试类,测试代码如下:
package com.zhq.test;
import java.io.IOException;
import java.io.Reader;
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 org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.zhq.dao.CardDao;
import com.zhq.pojo.Card;
public class CardTest {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
@Before
public void setUp() throws Exception {
try {
reader = Resources.getResourceAsReader("mybatis_config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
//测试嵌套查询
@Test
public void select() {
SqlSession session = sqlSessionFactory.openSession();
CardDao cardDao = session.getMapper(cardDao.class);
Card card = cardDao.selectById(1);
session.commit();
System.out.println(card);
session.close();
}
//测试联合查询
@Test
public void select() {
SqlSession session = sqlSessionFactory.openSession();
CardDao cardDao = session.getMapper(CardDao.class);
Card card = cardDao.selectByJoinUser(1);
session.commit();
System.out.println(card);
session.close();
}
}
一对多查询
场景描述
一个用户可以持有多张银行卡,那么用户和银行卡之间的关系就是一对多的关系。
操作步骤
1.修改User类,在User类中增加一个List<Card> cards属性表示该用户持有的银行卡,代码如下:
//用户表实体类User
package com.zhq.pojo
public class User {
private int id;
private String username;
private String mobile;
private String gender;
private int age;
private List<Card> cards;
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return username;
}
public void setMobile(String mobile){
this.mobile = mobile;
}
public String getMobile(){
return mobile;
}
public void setGender(String gender){
this.gender = gender;
}
public String getGender(){
return gender;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void setCards(List<Card> cards){
this.cards = cards;
}
public List<Card> getCards(){
return cards;
}
}
2.修改UserMapper.xml配置文件。如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhq.dao.UserDao">
<resultMap type="user" id="userBaseMap">
<id property="id" column="id" jdbcType=INTEGER/>
<result property="username" column="username" jdbcType=VARCHAR/>
<result property="mobile" column="mobile" jdbcType=VARCHAR/>
<result property="gender" column="gender" jdbcType=VARCHAR/>
<result property="age" column="age" jdbcType= INTEGER/>
<collection property="cards" ofType="card" column="id" select="getCards"></collection>
</resultMap>
<select id="getCards" parameterType="int" resultType="card">
select * from card where userId = #{id};
</select>
<select id="selectById" parameterType="int" resultMap="userBaseMap">
select * from user where id = #{id};
</select>
<select id="selectAll" resultType="user">
select * from user;
</select>
</mapper>
3.在com.zhq.test包下新创建一个名为UserTest测试类,测试代码如下:
package com.zhq.test;
import java.io.IOException;
import java.io.Reader;
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 org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.zhq.dao.UserDao;
import com.zhq.pojo.User;
public class UserTest {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
@Before
public void setUp() throws Exception {
try {
reader = Resources.getResourceAsReader("mybatis_config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
//测试嵌套查询
@Test
public void select() {
SqlSession session = sqlSessionFactory.openSession();
UserDao userDao = session.getMapper(UserDao.class);
User user = userDao.selectById(1);
session.commit();
System.out.println(user);
session.close();
}
}
多对多的查询
多对多的查询其实就是两个一对多的查询
网友评论