|   1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 | package com.springboot.data.jdbc.app.dao;
import com.springboot.data.jdbc.app.model.OrderStatus;
import com.springboot.data.jdbc.app.model.request.CustomerRequest;
import com.springboot.data.jdbc.app.model.request.ItemRequest;
import com.springboot.data.jdbc.app.model.request.OrderRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Repository
public class CustomerDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcOperations namedParameterJdbcOperations;
    public UUID createCustomer(CustomerRequest customerRequest) {
        String sqlCustomerQuery =
                           "INSERT INTO customers(                          "
                +          "id,                                             "   /*1*/
                +          "address,                                        "   /*2*/
                +          "dob,                                            "   /*3*/
                +          "email,                                          "   /*4*/
                +          "full_name,                                      "   /*5*/
                +          "gender,                                         "   /*6*/
                +          "phone)                                          "   /*7*/
                +          "VALUES (?, ?, ?, ?, ?, ?, ?)                    "
                ;
        UUID uuid = this.getRandomUUID();
        this.jdbcTemplate.update(sqlCustomerQuery,
                uuid.toString(),
                customerRequest.getAddress(),
                customerRequest.getDob(),
                customerRequest.getEmail(),
                customerRequest.getFullName(),
                customerRequest.getGender().toString(),
                customerRequest.getPhone());
        return uuid;
    }
    public Map<UUID, OrderRequest> createOrders(UUID customerId, List<OrderRequest> orderRequestList) {
        Map<UUID, OrderRequest> map = new HashMap<>();
        final int size = orderRequestList.size();
        String sqlOrderQuery =
                         "INSERT INTO orders(                                "
                 +       "id,                                                " /*1*/
                 +       "created_date,                                      " /*2*/
                 +       "last_updated_date,                                 " /*3*/
                 +       "order_name,                                        " /*4*/
                 +       "order_status,                                      " /*5*/
                 +       "customer_id)                                       " /*6*/
                 +       "VALUES(                                            "
                 +       ":id,                                               " /*1*/
                 +       ":created_date,                                     " /*2*/
                 +       ":last_updated_date,                                " /*3*/
                 +       ":order_name,                                       " /*4*/
                 +       ":order_status,                                     " /*5*/
                 +       ":customer_id)                                      " /*6*/
                ;
        List<SqlParameterSource> sqlParameterSources = new ArrayList<>(size);
        for (OrderRequest orderRequest: orderRequestList) {
            UUID uuid = this.getRandomUUID();
            MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
            LocalDateTime now = LocalDateTime.now();
            mapSqlParameterSource.addValue("id", uuid.toString());
            mapSqlParameterSource.addValue("created_date",now);
            mapSqlParameterSource.addValue("last_updated_date", now);
            mapSqlParameterSource.addValue("order_name", orderRequest.getOrderName());
            mapSqlParameterSource.addValue("order_status", OrderStatus.CREATED.toString());
            mapSqlParameterSource.addValue("customer_id", customerId.toString());
            sqlParameterSources.add(mapSqlParameterSource);
            map.put(uuid, orderRequest);
        }
        this.namedParameterJdbcOperations.batchUpdate(sqlOrderQuery, sqlParameterSources.toArray(new SqlParameterSource[size]));
        return map;
    }
    public void createItems(UUID orderId, List<ItemRequest> itemRequests) {
        final int size = itemRequests.size();
        String itemSqlQuery =
                                "INSERT INTO items(                                 "
                        +       "id,                                                " /*1*/
                        +       "item_name,                                         " /*2*/
                        +       "price,                                             " /*3*/
                        +       "quantity,                                          " /*4*/
                        +       "order_id)                                          " /*5*/
                        +       "VALUES(                                            "
                        +       ":id,                                               " /*1*/
                        +       ":item_name,                                        " /*2*/
                        +       ":price,                                            " /*3*/
                        +       ":quantity,                                         " /*4*/
                        +       ":order_id)                                         " /*5*/
                        ;
        UUID uuid = this.getRandomUUID();
        List<SqlParameterSource> sqlParameterSources = new ArrayList<>(size);
        for (ItemRequest itemRequest: itemRequests) {
            MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
            mapSqlParameterSource.addValue("id", uuid.toString());
            mapSqlParameterSource.addValue("item_name", itemRequest.getItemName());
            mapSqlParameterSource.addValue("price", itemRequest.getPrice());
            mapSqlParameterSource.addValue("quantity", itemRequest.getQuantity());
            mapSqlParameterSource.addValue("order_id", orderId.toString());
            sqlParameterSources.add(mapSqlParameterSource);
        }
        this.namedParameterJdbcOperations.batchUpdate(itemSqlQuery, sqlParameterSources.toArray(new SqlParameterSource[size]));
    }
    private UUID getRandomUUID() {
        return UUID.randomUUID();
    }
}
 |