Skip to content

Commit

Permalink
Add sample code for comparison with MyBatis
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamura-to committed Jan 26, 2025
1 parent 7be6754 commit 63f2a5e
Show file tree
Hide file tree
Showing 11 changed files with 504 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,97 @@ INSERT INTO NO_ID VALUES (1, 1);

INSERT INTO ID_GENERATOR VALUES('TABLE_STRATEGY_ID', 1);
INSERT INTO MY_ID_GENERATOR VALUES('TableStrategy2', 1);

-- test

CREATE TABLE m_item (
code CHAR(10),
name NVARCHAR(256),
price INTEGER,
CONSTRAINT m_item_pk PRIMARY KEY(code)
);

CREATE TABLE m_category (
code CHAR(10),
name NVARCHAR(256),
CONSTRAINT m_category_pk PRIMARY KEY(code)
);

CREATE TABLE m_item_category (
item_code CHAR(10),
category_code CHAR(10),
CONSTRAINT m_item_category_pk PRIMARY KEY(item_code, category_code),
CONSTRAINT m_item_category_fk1 FOREIGN KEY(item_code) REFERENCES m_item(code),
CONSTRAINT m_item_category_fk2 FOREIGN KEY(category_code) REFERENCES m_category(code)
);

CREATE TABLE m_coupon (
code CHAR(10),
name NVARCHAR(256),
price INTEGER,
CONSTRAINT m_coupon_pk PRIMARY KEY(code)
);

CREATE TABLE c_order_status (
code VARCHAR(10),
name NVARCHAR(256),
CONSTRAINT c_order_status_pk PRIMARY KEY(code)
);

CREATE TABLE t_order (
id INTEGER,
status_code VARCHAR(10),
CONSTRAINT t_order_pk PRIMARY KEY(id),
CONSTRAINT t_order_fk FOREIGN KEY(status_code) REFERENCES c_order_status(code)
);

CREATE TABLE t_order_item (
order_id INTEGER,
item_code CHAR(10),
quantity INTEGER,
CONSTRAINT t_order_item_pk PRIMARY KEY(order_id, item_code),
CONSTRAINT t_order_item_fk1 FOREIGN KEY(order_id) REFERENCES t_order(id),
CONSTRAINT t_order_item_fk2 FOREIGN KEY(item_code) REFERENCES m_item(code)
);

CREATE TABLE t_order_coupon (
order_id INTEGER,
coupon_code CHAR(10),
CONSTRAINT t_order_coupon_pk PRIMARY KEY(order_id, coupon_code),
CONSTRAINT t_order_coupon_fk1 FOREIGN KEY(order_id) REFERENCES t_order(id),
CONSTRAINT t_order_coupon_fk2 FOREIGN KEY(coupon_code) REFERENCES m_coupon(code)
);

-- Setup master tables
INSERT INTO m_item VALUES ('ITM0000001','Orange juice',100);
INSERT INTO m_item VALUES ('ITM0000002','NotePC',100000);

INSERT INTO m_category VALUES ('CTG0000001','Drink');
INSERT INTO m_category VALUES ('CTG0000002','PC');
INSERT INTO m_category VALUES ('CTG0000003','Hot selling');

INSERT INTO m_item_category VALUES ('ITM0000001','CTG0000001');
INSERT INTO m_item_category VALUES ('ITM0000002','CTG0000002');
INSERT INTO m_item_category VALUES ('ITM0000002','CTG0000003');

INSERT INTO m_coupon VALUES ('CPN0000001','Join coupon',3000);
INSERT INTO m_coupon VALUES ('CPN0000002','PC coupon',30000);

-- Setup code tables
INSERT INTO c_order_status VALUES ('accepted','Order accepted');
INSERT INTO c_order_status VALUES ('checking','Stock checking');
INSERT INTO c_order_status VALUES ('shipped','Item Shipped');

-- Setup transaction tables
INSERT INTO t_order VALUES (1,'accepted');
INSERT INTO t_order VALUES (2,'checking');

INSERT INTO t_order_item VALUES (1,'ITM0000001',1);
INSERT INTO t_order_item VALUES (1,'ITM0000002',2);
INSERT INTO t_order_item VALUES (2,'ITM0000001',3);
INSERT INTO t_order_item VALUES (2,'ITM0000002',4);

INSERT INTO t_order_coupon VALUES (1,'CPN0000001');
INSERT INTO t_order_coupon VALUES (1,'CPN0000002');

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.seasar.doma.it.temp;

import org.seasar.doma.Entity;
import org.seasar.doma.Id;
import org.seasar.doma.Table;

import java.io.Serializable;

@Entity
@Table(name = "m_category")
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id private String code;
private String name;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.seasar.doma.it.temp;

import org.seasar.doma.Entity;
import org.seasar.doma.Id;
import org.seasar.doma.Table;

import java.io.Serializable;

@Entity
@Table(name = "m_coupon")
public class Coupon implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String code;
private String name;
private int price;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.seasar.doma.it.temp;

import org.seasar.doma.Association;
import org.seasar.doma.Entity;
import org.seasar.doma.Id;
import org.seasar.doma.Table;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "m_item")
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id private String code;
private String name;
private int price;
@Association private List<Category> categories = new ArrayList<>();

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public List<Category> getCategories() {
return categories;
}

public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.seasar.doma.it.temp;

import org.seasar.doma.Association;
import org.seasar.doma.Entity;
import org.seasar.doma.Id;
import org.seasar.doma.Table;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "t_order")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int id;
@Association private OrderStatus orderStatus;
@Association List<OrderItem> orderItems = new ArrayList<>();
@Association List<OrderCoupon> orderCoupons = new ArrayList<>();

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public OrderStatus getOrderStatus() {
return orderStatus;
}

public void setOrderStatus(OrderStatus orderStatus) {
this.orderStatus = orderStatus;
}

public List<OrderItem> getOrderItems() {
return orderItems;
}

public void setOrderItems(List<OrderItem> orderItems) {
this.orderItems = orderItems;
}

public List<OrderCoupon> getOrderCoupons() {
return orderCoupons;
}

public void setOrderCoupons(List<OrderCoupon> orderCoupons) {
this.orderCoupons = orderCoupons;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.seasar.doma.it.temp;

import org.seasar.doma.Association;
import org.seasar.doma.Entity;
import org.seasar.doma.Id;
import org.seasar.doma.Table;

import java.io.Serializable;

@Entity
@Table(name = "t_order_coupon")
public class OrderCoupon implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int orderId;
@Id
private String couponCode;
@Association private Coupon coupon;

public int getOrderId() {
return orderId;
}

public void setOrderId(int orderId) {
this.orderId = orderId;
}

public String getCouponCode() {
return couponCode;
}

public void setCouponCode(String couponCode) {
this.couponCode = couponCode;
}

public Coupon getCoupon() {
return coupon;
}

public void setCoupon(Coupon coupon) {
this.coupon = coupon;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.seasar.doma.it.temp;

import org.seasar.doma.AssociationLinker;
import org.seasar.doma.Dao;
import org.seasar.doma.Select;

import java.util.function.BiFunction;

@Dao
public interface OrderDao {
@Select(aggregateHelper = OrderHelper.class)
Order findById(int id);
}

interface OrderHelper {

@AssociationLinker(propertyPath = "orderStatus", columnPrefix = "os_")
BiFunction<Order, OrderStatus, Order> orderStatus = (o,os)-> {
o.setOrderStatus(os);
return o;
};

@AssociationLinker(propertyPath = "orderItems", columnPrefix = "oi_")
BiFunction<Order, OrderItem, Order> orderItems = (o,oi)-> {
o.getOrderItems().add(oi);
return o;
};

@AssociationLinker(propertyPath = "orderCoupons", columnPrefix = "oc_")
BiFunction<Order, OrderCoupon, Order> orderCoupons = (o,oc)-> {
o.getOrderCoupons().add(oc);
return o;
};

@AssociationLinker(propertyPath = "orderItems.item", columnPrefix = "i_")
BiFunction<OrderItem, Item, OrderItem> orderItems$item = (oi,i)-> {
oi.setItem(i);
return oi;
};


@AssociationLinker(propertyPath = "orderCoupons.coupon", columnPrefix = "cp_")
BiFunction<OrderCoupon, Coupon, OrderCoupon> orderCoupon$coupon = (oc,cp)-> {
oc.setCoupon(cp);
return oc;
};

@AssociationLinker(propertyPath = "orderItems.item.categories", columnPrefix = "ct_")
BiFunction<Item, Category, Item> orderItems$item$categories = (i,c)-> {
i.getCategories().add(c);
return i;
};
}
Loading

0 comments on commit 63f2a5e

Please sign in to comment.