INTP의 멋대로 개발 세상

[📚상품 구매 사이트 3단계] 구매자 서버 만들기 - 2. 모델링 (model, Repository, xml 생성) 본문

KDT 풀스택 국비 과정/파이널 프로젝트(미니)

[📚상품 구매 사이트 3단계] 구매자 서버 만들기 - 2. 모델링 (model, Repository, xml 생성)

인팁구름 2023. 4. 17. 03:08

 

쇼핑몰 구매자 서버

 

👇 프로젝트 설명 및 테이블 설계 보러가기 👇

 

[연습📚상품 구매 사이트] 구매자 서버 만들기 - 1. 테이블, 더미데이터, view 생성&연결

쇼핑몰 구매자 서버 🏆 목표 🏆 구매자 입장에서 회원가입, 로그인, 상품구매, 구매취소 기능을 구현 데이터는 MySQL에 연동되어 있기 때문에 테이블이나 더미데이터는 MySQL에 작성함 [연습📚상

whiteclouds-dev.tistory.com

 

💃 모델링 🕺

 

구매자 입장에서 필요한 테이블은 3개가 있다.

 

 

1. 유저(구매자)
- 유저의 정보에 대한 테이블

2. 상품

- 쇼핑몰에 있는 상품에 대한 테이블

3. 주문

- 주문정보에 대한 테이블

 

 

 

테이블에 기초해서  Model, Repository => xml 파일를 생성하자

 

 

User

 

package shop.mtcoding.productapp_buyer.model.user;

import java.sql.Timestamp;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class User {

    private Integer usersId;
    private String username;
    private String password;
    private Timestamp createdAt;
}
package shop.mtcoding.productapp_buyer.model.user;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserRepository {

    public void insert(User users);

    public User findById(Integer usersId);

    public User findByUsername(String username);

    public List<User> findAll();

    public void update(User user);

    public void delete(Integer usersId);
}
<?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="shop.mtcoding.productapp_buyer.model.user.UserRepository">

	<select id="findByUsername"
		resultType="shop.mtcoding.productapp_buyer.model.user.User">
		SELECT * FROM users WHERE user_name = #{userName}
	</select>


	<select id="findById"
		resultType="shop.mtcoding.productapp_buyer.model.user.User">
		SELECT * FROM users WHERE user_id = #{userId}

	</select>

	<insert id="insert">
		INSERT INTO users(user_name, user_password,createdAt)
		VALUES(#{userName}, #{userPassword}, now())
	</insert>


	<select id="findAll"
		resultType="shop.mtcoding.productapp_buyer.model.user.User">
		SELECT * FROM users ORDER BY user_id DESC
	</select>


	<update id="update">
		UPDATE users SET user_password = #{userPassword}, user_email =
		#{userEmail}
		WHERE user_id = #{userId}
	</update>

	<delete id="deleteById">
		DELETE FROM users WHERE user_id = #{userId}
	</delete>

</mapper>

 

Product

package shop.mtcoding.productapp_buyer.model.product;

import java.sql.Timestamp;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Product {

    private Integer productId;
    private String productName;
    private Integer productPrice;
    private Integer productQty;
    private Timestamp createdAt;
}
package shop.mtcoding.productapp_buyer.model.product;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ProductRepository {

    public Product findById(Integer productId);

    public List<Product> findAll();

    public Product findByProductName(String productName);

    public void insert(Product product);

    public void update(Product product);

    public void deleteById(Integer productId);

    // public void productQtyUpdate(OrdersProductDto ordersProductDto);
}
<?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="shop.mtcoding.productapp_buyer.model.product.ProductRepository">

	<select id="findById"
		resultType="shop.mtcoding.productapp_buyer.model.product.Product">
		SELECT * FROM product WHERE product_id = #{productId}
	</select>

	<select id="findByProductName"
		resultType="shop.mtcoding.productapp_buyer.model.product.Product">
		SELECT * FROM product WHERE product_name = #{productName}
	</select>

	<select id="findAll"
		resultType="shop.mtcoding.productapp_buyer.model.product.Product">
		SELECT * FROM product ORDER BY product_id DESC
	</select>

	<insert id="insert">
		INSERT INTO product(product_name, product_price, product_qty, created_at)
		VALUES(#{productName}, #{productPrice}, #{productQty}, NOW())
	</insert>

	<update id="update">
		UPDATE product SET 
		product_name = #{productName},
		product_price = #{productPrice},
		product_qty = #{productQty}
		WHERE product_id = #{productId}
	</update>

	<delete id="deleteById">
		DELETE FROM product WHERE product_id = #{productId}
	</delete>

</mapper>

 

Order

package shop.mtcoding.productapp_buyer.model.order;

import java.sql.Timestamp;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Order {

    private Integer ordersId;
    private String ordersName;
    private Integer ordersPrice;
    private Integer ordersCount; // 주문수량
    private Integer productId;
    private Integer usersId;
    private Timestamp createdAt;
}
package shop.mtcoding.productapp_buyer.model.order;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface OrderRepository {

    public void insert(Order orders);

    public Order findById(Integer ordersId);

    public List<Order> findAll(Integer usersId);

    public void deleteById(Integer ordersId);
}
<?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="shop.mtcoding.productapp_buyer.model.order.OrderRepository">

	<select id="findById"
		resultType="shop.mtcoding.productapp_buyer.model.order.Order">
		SELECT * FROM orders WHERE orders_id=#{ordersId}
	</select>

	<select id="findAll"
		resultType="shop.mtcoding.productapp_buyer.model.order.Order">
		SELECT *
		FROM orders 
		WHERE users_id=#{usersId}
	</select>

	<insert id="insert">
		INSERT
		INTO orders(orders_name, orders_price, orders_count, users_id, product_id, created_at)
		VALUES(#{ordersName}, #{ordersPrice}, #{ordersCount},#{usersId},#{productId}, now())
	</insert>


	<delete id="deleteById">
		DELETE FROM orders WHERE id = #{ordersId}
	</delete>

</mapper>

 

 

 

Comments