1.一对一查询
1.1数据库查询
数据库准备
USE mybatis;
#创建一个名称为tb_user的表
CREATE TABLE tb_user (
id int(32) PRIMARY KEY AUTO_INCREMENT,
username varchar(32),
address varchar(256)
);
插入3条数据
INSERT INTO tb_user VALUES ('1', '小明', '北京');
INSERT INTO tb_user VALUES ('2', '李华', '上海');
INSERT INTO tb_user VALUES ('3', '李刚', '上海');
创建一个名称为tb_orders的表
CREATE TABLE tb_orders (
id int(32) PRIMARY KEY AUTO_INCREMENT,
number varchar(32) NOT NULL,
user_id int(32) NOT NULL,
FOREIGN KEY(user_id) REFERENCES tb_user(id)
);
插入3条数据
INSERT INTO tb_orders VALUES ('1', '1000011', '1');
INSERT INTO tb_orders VALUES ('2', '1000012', '1');
INSERT INTO tb_orders VALUES ('3', '1000013', '2');
1.2 实体类
1.2.1 IdCard.java
// An highlighted block
package com.biem.pojo;
import lombok.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class IdCard {
private Integer id;
private String code;
}
1.2.2 Person.java
package com.biem.pojo;
import lombok.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Person {
private Integer id;
private String name;
private Integer age;
private String sex;
private IdCard card;
}
1.3 用户配置接口
1.3.1 IdCardMapper.java
package com.biem.mapper;
public interface IdCardMapper{
}
1.3.2 Person.java
package com.biem.mapper;
public interface PersonMapper {
}
1.4 用户配置文件
1.4.1 IdCardMapper.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.biem.mapper.IdCardMapper">
</mapper>
1.4.2 PersonMapper.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.biem.mapper.PersonMapper">
</mapper>
1.5解决办法
嵌套查询
1.5.1.1 IdCardMapper.java
package com.biem.mapper;
import com.biem.pojo.IdCard;
public interface IdCardMapper {
public IdCard findCodeById(Integer id);
}
PersonMapper.java
package com.biem.mapper;
import com.biem.pojo.Person;
public interface PersonMapper {
public Person findPersonById(Integer id);
}
IdCardMapper.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.biem.mapper.IdCardMapper">
<!--public IdCard findCodeById(Integer id);-->
<select id="findCodeById" parameterType="Integer" resultType="IdCard">
select * from tb_idcard where id=#{id}
</select>
</mapper>
PersonMapper.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.biem.mapper.PersonMapper">
<!-- public Person findPersonById(Integer id); -->
<resultMap id="IdCardWithPersonResult" type="Person">
<id property="id" column="id"></id>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<!-- 一对一:association使用select 属性引入另外一条SQL语句 -->
<association property="card" column="card_id" javaType="IdCard"
select="com.biem.mapper.IdCardMapper.findCodeById"/>
</resultMap>
<select id="findPersonById" parameterType="Integer" resultMap="IdCardWithPersonResult">
select * from tb_person where id = #{id}
</select>
</mapper>
PersonTest.java
package com.biem.test;
import com.biem.mapper.PersonMapper;
import com.biem.pojo.Person;
import com.biem.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class PersonTest {
@Test
public void testFindPersonById(){
SqlSession session = MyBatisUtil.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
Person person = mapper.findPersonById(1);
System.out.println(person);
session.close();
}
}
1.5.1. 测试结果
2.一对多查询
2.1 数据库准备
USE mybatis;
# 创建一个名称为tb_product的表
CREATE TABLE tb_product (
id INT(32) PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(32),
price DOUBLE
);
# 插入3条数据
INSERT INTO tb_product VALUES ('1', 'Java基础入门', '44.5');
INSERT INTO tb_product VALUES ('2', 'Java Web程序开发入门', '38.5');
INSERT INTO tb_product VALUES ('3', 'SSM框架整合实战', '50');
# 创建一个名称为tb_ordersitem 的中间表
CREATE TABLE tb_ordersitem (
id INT(32) PRIMARY KEY AUTO_INCREMENT,
orders_id INT(32),
product_id INT(32),
FOREIGN KEY(orders_id) REFERENCES tb_orders(id),
FOREIGN KEY(product_id) REFERENCES tb_product(id)
);
# 插入3条数据
INSERT INTO tb_ordersitem VALUES ('1', '1', '1');
INSERT INTO tb_ordersitem VALUES ('2', '1', '3');
INSERT INTO tb_ordersitem VALUES ('3', '3', '3');
# 订单表tb_orders由于在上面已经创建,所以这里不用再次创建
2.2 Orders.java
private List<Product> productList;
2.3 Product.java
package com.biem.pojo;
import lombok.*;
import java.util.List;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Product {
private Integer id;
private String name;
private Double price;
private List<Orders> ordersList;
}
2.4 用户配置接口
2.5ProductMapper.java
package com.biem.mapper;
public interface ProductMapper {
}
2.6 ProductMapper.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.biem.mapper.ProductMapper">
</mapper>
2.7 OrdersMapper.java 添加
public List findOrdersWithProduct(Integer id);
2.8 ProductMapper.java 添加
public List findProductById(Integer id);
2.9 OrdersMapper.xml 添加
<select id="findOrdersWithProduct" parameterType="Integer" resultMap="OrdersWithProductResult">
select * from tb_orders where id = #{id}
</select>
<resultMap id="OrdersWithProductResult" type="Orders">
<id property="id" column="id"></id>
<result property="number" column="number"></result>
<collection property="productList" column="id" ofType="Product" select="com.biem.mapper.ProductMapper.findProductById">
</collection>
</resultMap>
2.10 ProductMapper.xml 添加
<!--public List<Product> findProductById(Integer id);-->
<select id="findProductById" parameterType="Integer" resultType="Product">
select * from tb_product where id in(
select product_id from tb_ordersitem where orders_id = #{id}
)
</select>
2.12 OrderMapper.java 添加
public List findOrdersWithProductByNestedResult(Integer id);
2.13 OrdersMapper.xml 添加
<!-- public List<Orders> findOrdersWithProductByNestedResult(Integer id); -->
<select id="findOrdersWithProductByNestedResult" parameterType="Integer" resultMap="OrdersWithProductResult2">
select o.*, p.id as pid, p.name, p.price
from tb_orders o, tb_product p, tb_ordersitem oi
where oi.orders_id=o.id and oi.product_id=p.id and o.id=#{id}
</select>
<resultMap id="OrdersWithProductResult2" type="Orders">
<id property="id" column="id"></id>
<result property="number" column="number"></result>
<collection property="productList" ofType="product">
<id property="id" column="pid"/>
<result property="name" column="name"/>
<result property="price" column="price"/>
</collection>
</resultMap>
2.14 OrderTest.java 添加
@Test
public void findOrdersWithProductByNestedResult(){
SqlSession session = MyBatisUtil.openSession();
OrdersMapper mapper = session.getMapper(OrdersMapper.class);
List<Orders> ordersList = mapper.findOrdersWithProductByNestedResult(1);
System.out.println("ordersList = " + ordersList);
}
2.15测试结果
DEBUG 05-12 23:38:47,365 ==> Preparing: select o.*, p.id as pid, p.name, p.price from tb_orders o, tb_product p, tb_ordersitem oi where oi.orders_id=o.id and oi.product_id=p.id and o.id=? (BaseJdbcLogger.java:159)
DEBUG 05-12 23:38:47,438 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:159)
DEBUG 05-12 23:38:47,485 <== Total: 2 (BaseJdbcLogger.java:159)
ordersList = [Orders(id=1, number=1000011, userId=null, users=null, productList=[Product(id=1, name=Java基础入门, price=44.5, ordersList=null), Product(id=3, name=java基础入门, price=44.5, ordersList=null)])]