目录结构
数据库
1、product_表
插入数据:
建表sql文:
use mybatistest1;
create table product_(
id int NOT NULL AUTO_INCREMENT,
name varchar(30) DEFAULT NULL,
price float DEFAULT 0,
cid int ,
PRIMARY KEY (id)
)AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
3、创建order_表
建表sql:
create table order_(
id int(11) NOT NULL AUTO_INCREMENT,
code VARCHAR(32) DEFAULT NULL,
PRIMARY KEY(id)
)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
4、order_item表
建表sql:
CREATE table order_item_(
id int(11) not null auto_increment,
oid int,
pid int,
number int,
PRIMARY KEY(id)
)AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
代码
Order实体类
package com.mybatis.pojo;
import java.util.List;
public class Order {
private int id;
private String code;
List<OrderItem> orderitems; //一对多
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<OrderItem> getOrderitems() {
return orderitems;
}
public void setOrderitems(List<OrderItem> orderitems) {
this.orderitems = orderitems;
}
}
OrderItem实体类
package com.mybatis.pojo;
public class OrderItem {
private int id;
private int number;
//private Order order;
private Product product; //多对一
/*public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
Product实体类
package com.mybatis.pojo;
public class Product {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
OrdeMapper类
package com.mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.mybatis.pojo.OrderItem;
public interface OrderItemMapper {
@Select("select * from order_item_ where oid=#{oid}")
@Results({
//@Result(property="id",column="id"),
//@Result(property="number",column="number"),
@Result(property="product",column="pid",one=@One(select="com.mybatis.mapper.ProductMapper.getByProduct"))
})
public List<OrderItem> getByOrder(int oid);
}
OrderItemMapper类
package com.mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.mybatis.pojo.Order;
public interface OrderMapper {
@Select("select * from order_")
@Results({
//@Result(property="id",column="id"),
//@Result(property="code",column="code"),
@Result(property="orderitems",column="id",many=@Many(select="com.mybatis.mapper.OrderItemMapper.getByOrder"))
}) //column当作参数传入Many中的方法
public List<Order> listOrder();
}
ProductMapper类
package com.mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.mybatis.pojo.Product;
public interface ProductMapper {
@Select("select * from product_ where id=#{id}")
public Product getByProduct(int id);
}
测试主类
package com.mybatis;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 com.mybatis.mapper.OrderMapper;
import com.mybatis.pojo.*;
public class testMybatis {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml"; //获取全局配置信息
InputStream inputStream = Resources.getResourceAsStream(resource); //全局配置信息装入输入流
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //建立sql会话工厂,将输入流装入
SqlSession session=sqlSessionFactory.openSession(); //开启一个会话
//CategoryMapper categorymapper=session.getMapper(CategoryMapper.class);
OrderMapper ordermapper=session.getMapper(OrderMapper.class);
listAll(ordermapper);
session.commit(); //更新到数据库
session.close();
}
private static void listAll(OrderMapper mapper) {
List<Order> os=mapper.listOrder();
for(Order o:os) {
System.out.println(o.getCode());
List<OrderItem> ois=o.getOrderitems();
if(null!=ois) {
for(OrderItem oi:ois) {
System.out.format("\t%s\t%f\t%d%n", oi.getProduct().getName(),oi.getProduct().getPrice(),oi.getNumber());
}
}
}
}
}
全局配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.mybatis.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatistest1?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="zhao8888"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.mybatis.mapper.OrderMapper"/>
<mapper class="com.mybatis.mapper.OrderItemMapper"/>
<mapper class="com.mybatis.mapper.ProductMapper"/>
</mappers>
</configuration>
结果
过程和总结
Order类包括OrderItem类list集合------>一对多
OrderItem类包括Product类list集合------->多对一
一对多对一都等于多对多
s