According to the order id to query the order and product information

number: Order Number

Wrapped package com.itcast.mapperxml;

com.itcast.mapperxml Package Penalty for;
Import org.apache.ibatis.annotations.Param;
Import com.itcast.pojo.Person;
public interface PersonMapper {
// one: According id Query user information and identification numbers
public Person queryPersonWithIdCardById ( @param ( "ID") ID Integer);
}

package com.itcast.pojo;
package com.itcast.pojo;

public class Person {
private Integer id;
private String name;
private Integer age;
private String sex;
private IdCard idCard;
…Set与Get方法,构造方法
}

package com.itcast.pojo;

import java.util.List;

/ *
* Order form
*

  • */
    public class Orders {

    private Integer id; // order the above mentioned id
    Private Number The String; // Order number
    // Declare Product
    Private List productList;

    public List getProductList() {
    return productList;
    }
    public void setProductList(List productList) {
    this.productList = productList;
    }

    public Orders(Integer id, String number, List productList) {
    super();
    this.id = id;
    this.number = number;
    this.productList = productList;
    }
    public Orders() {
    super();
    // TODO Auto-generated constructor stub
    }
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public String getNumber() {
    return number;
    }
    public void setNumber(String number) {
    this.number = number;
    }
    @Override
    public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append(“Orders [id=”);
    builder.append(id);
    builder.append(", number=");
    builder.append(number);
    builder.append(", productList=");
    builder.append(productList);
    builder.append("]");
    return builder.toString();
    }
    }

xml file

<?xml version="1.0" encoding="UTF-8"?>
<resultMap type="Orders" id="ordersWithProductMap"
	autoMapping="true">
	<!-- 对应sql表中的id -->
	<id column="id" property="id" />
	<!-- colection:多对多映射 -->
	<collection property="productList" ofType="Product"	autoMapping="true">
		<id column="pid" property="id" />
	</collection>

</resultMap>
<select id="queryOrdersWithProductById" resultMap="ordersWithProductMap">

	SELECT
	a.*,
	b.id AS pid,
	b.name,
	b.price
	FROM
	tb_orders a,
	tb_product b,
	tb_ordersitem c
	WHERE
	c.orders_id=a.id
	AND
	c.product_id=b.id
	AND
	a.id="3"

</select>

MyBatis core profile

<?xml version="1.0" encoding="UTF-8" ?>

Defined test class
package com.itcast.test;

import java.io.InputStream;

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.Before;
import org.junit.Test;

import com.itcast.mapperxml.OrdersMapper;
import com.itcast.pojo.Orders;

public class OrdersMapperTest {
private OrdersMapper ordersMapper;

@Before
public void setUp() throws Exception {
	String resource = "MyBatis.xml";
	InputStream inputStream = Resources.getResourceAsStream(resource);
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
			.build(inputStream);
	SqlSession sqlSession = sqlSessionFactory.openSession(true);
	// 实例化UserMapper
	this.ordersMapper = sqlSession.getMapper(OrdersMapper.class);
}

The order id // query and the order item information
@Test
public void testqueryOrdersWithProductById () {
the Orders Orders this.ordersMapper.queryOrdersWithProductById = (. 3);
System.out.println (Orders);
}
}

The results show
Here Insert Picture Description
refer to the relevant database
# Create a name for tb_user
the CREATE TABLE tb_user (
the above mentioned id INT (32) PRIMARY KEY AUTO_INCREMENT,
username VARCHAR (32),
address VARCHAR (256)
);

Data insertion bar 3

INSERT INTO tb_user VALUES ( '1' , ' James',' Cleveland ');
the INSERT the INTO tb_user the VALUES (' 2 ',' Bryant ',' LA ');
the INSERT the INTO tb_user the VALUES ('. 3 ',' Paul ',' LA ');
the CREATE TABLE tb_idcard (
ID a PRIMARY KEY the AUTO_INCREMENT the INT,
CODE VARCHAR (18 is)
);

Data insertion bar 2

INSERT INTO tb_idcard(CODE) VALUES(‘123453188908572121’);
INSERT INTO tb_idcard(CODE) VALUES(‘589453188908572121’);

CREATE TABLE tb_person(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(32),
age INT,
sex VARCHAR(8),
card_id INT UNIQUE,
FOREIGN KEY(card_id) REFERENCES tb_idcard(id)

);

Data insertion bar 2

INSERT INTO tb_person(NAME,age,sex,card_id) VALUES(‘Rose’,29,‘女’,1);

INSERT INTO tb_person(NAME,age,sex,card_id) VALUES(‘Tom’,27,‘男’,2);

# Create a name for tb_product
the CREATE TABLE tb_product (
the above mentioned id INT (32) PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR (32),
. Price DOUBLE
);

# Create a name for tb_orders
- Number The order number
the CREATE TABLE tb_orders (
the above mentioned id INT (32) PRIMARY KEY AUTO_INCREMENT,
Number The VARCHAR (32),
user_id INT (32) the NOT NULL,
a FOREIGN KEY (user_id) the REFERENCES tb_user (the above mentioned id)
);

Data insertion bar 3

INSERT INTO tb_orders VALUES(‘1’,‘1000011’,‘1’);
INSERT INTO tb_orders VALUES(‘2’,‘1000012’,‘1’);
INSERT INTO tb_orders VALUES(‘3’,‘1000013’,‘3’);

Data insertion bar 3

INSERT INTO tb_product VALUES ( '1' , ' introductory Java foundation', '44 .5 ');
the INSERT the INTO tb_product the VALUES (' ',' entry Java Web application 2 development ', '38 .5');
the INSERT the INTO tb_product the VALUES ( '. 3' , 'SSM actual frame integration', '50 ');
the INSERT the INTO tb_product the VALUES ('. 4 ',' the MySQL ',' 30.0 ');
the INSERT the INTO tb_product the VALUES ('. 5 ',' Eclispe ',' 25.0 ');

# Create a name for 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)
);

Data insertion bar 3

INSERT INTO tb_ordersitem VALUES(‘1’,‘1’,‘1’);
INSERT INTO tb_ordersitem VALUES(‘2’,‘2’,‘2’);
INSERT INTO tb_ordersitem VALUES(‘3’,‘3’,‘3’);
INSERT INTO tb_ordersitem VALUES(‘4’,‘3’,‘1’);
INSERT INTO tb_ordersitem VALUES(‘5’,‘3’,‘5’);

Guess you like

Origin blog.csdn.net/weixin_44174536/article/details/90201937
Recommended