@Autowired public ProductRepository(DataSource dataSource) { this.db = new NamedParameterJdbcTemplate(dataSource); this.productInsertion = new SimpleJdbcInsert(dataSource).withTableName("product").usingGeneratedKeyColumns("id"); this.productMapper = (rs, rowNum) -> { Product product = new Product(); product.setId(rs.getInt("product_id")); // PK는 필수값. getInt를 써도 문제는 없음 product.setName(rs.getString("product_name")); product.setPrice((Long) rs.getObject("price")); product.setDescription(rs.getString("desc")); LocalDateTime regTime = LocalDateTime.parse(rs.getString("reg_time"), formatter); product.setRegisteredTime(regTime); Seller seller = new Seller(); seller.setId((Integer) rs.getObject("seller_id")); seller.setName(rs.getString("seller_name")); seller.setHomepage(rs.getString("homepage")); seller.setAddress(rs.getString("address")); product.setSeller(seller); return product; }; }
private Map<String, Object> mapColumns(Product product) { Map<String, Object> params = new HashMap<>(); params.put("id", product.getId()); params.put("name", product.getName()); params.put("desc", product.getDescription()); params.put("price", product.getPrice()); params.put("seller_id", product.getSeller().getId()); params.put("reg_time", product.getRegisteredTime().format(formatter)); return params; }