public User(String name, int age, String email, String phoneNumber, String address ...)
public User(String name, int age, String referer, String email, String PhoneNumber, String Address ...)
전체 코드: https://github.com/ji-jjang/Learning/commit/b0c39e36b8f339079d5539d0d3399489ef9dbfec
public class User {
private String name;
...
private class User() {} // private으로 제한할 수도 있음
public static class Builder {
private String name;
...
public Builder(String name) {
this.name = name;
}
public Builder(int age) {
this.age = age;
}
...
public User build() {
return new User(this);
}
}
}
public static void main(String[] args) {
User user = new Builder("홍길동")
.age(20)
.email("juny@gmail.com")
.address("seoul")
.build();
}
public interface Builder {
public void makeTitle(String title);
public void makeString(String string);
public void makeItems(String[] items);
public void close();
}
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public void construct() {
builder.makeTitle("Greeting");
builder.makeString("일반적인 인사");
builder.makeItems(new String[]{
"How are you?",
"Hello.",
"Hi."
});
builder.makeString("시간대별 인사");
builder.makeItems(new String[]{
"Good morning.",
"Good afternoon.",
"Good evening.",
});
builder.close();
}
}
public class TextBuilder implements Builder {
private StringBuilder sb = new StringBuilder();
@Override
public void makeTitle(String title) {
sb.append("===============================\n");
sb.append("[");
sb.append(title);
sb.append("]\n\n");
}
@Override
public void makeString(String str) {
sb.append("*");
sb.append(str);
sb.append("\n\n");
}
@Override
public void makeItems(String[] items) {
for (String s : items) {
sb.append(".");
sb.append(s);
sb.append("\n");
}
sb.append("\n");
}
@Override
public void close() {
sb.append("================================\n");
}
public String getTextResult() {
return sb.toString();
}
}
public class HTMLBuilder implements Builder {
private String filename = "untitled.html";
private StringBuilder sb = new StringBuilder();
@Override
public void makeTitle(String title) {
filename = title + ".html";
sb.append("<!DOCTYPE html>\n");
sb.append("<html>\n");
sb.append("<head>\n\t<title>");
sb.append(title);
sb.append("</title>\n");
sb.append("\t<meta charset=\"UTF-8\">\n");
sb.append("</head>\n");
sb.append("<body>\n");
sb.append("<h1>");
sb.append(title);
sb.append("</h1>\n\n");
}
@Override
public void makeString(String str) {
sb.append("<p>");
sb.append(str);
sb.append("</p>\n\n");
}
@Override
public void makeItems(String[] items) {
sb.append("<ul>\n");
for (String s: items) {
sb.append("<li>");
sb.append(s);
sb.append("</li>\n");
}
sb.append("</ul>\n\n");
}
@Override
public void close() {
sb.append("</body>");
sb.append("</html>\n");
try {
Writer writer = new FileWriter(filename);
writer.write(sb.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getHTMLResult() {
return filename;
}
}
public class Main {
public static void main(String[] args){
TextBuilder textbuilder = new TextBuilder();
Director director = new Director(textbuilder);
director.construct();
String result = textbuilder.getTextResult();
System.out.println(result);
HTMLBuilder htmlbuilder = new HTMLBuilder();
director = new Director(htmlbuilder);
director.construct();
String filename = htmlbuilder.getHTMLResult();
System.out.println("HTML파일 " + filename + "이 작성되었습니다.");
}
}
전체 코드: https://github.com/ji-jjang/ebrainsoft/tree/main/JspBoard
@Override
public List<Board> getBoardSearchList(int page, Map<String, String> searchConditions) {
List<Board> boards = new ArrayList<>();
StringBuilder sql =
new StringBuilder(
"""
SELECT
b.id, b.title, b.view_count, b.created_at, b.updated_at
FROM
boards b
LEFT JOIN
categories c
ON
b.category_id = c.id
""");
int limit = Constants.BOARD_LIST_PAGE_SIZE;
int offset = (page - 1) * limit;
boolean hasWhere = false;
if (searchConditions.containsKey(Constants.START_DATE)
&& searchConditions.containsKey(Constants.END_DATE)) {
sql.append("WHERE b.created_at BETWEEN ? AND ? ");
hasWhere = true;
}
if (searchConditions.containsKey(Constants.CATEGORY)) {
String connector = hasWhere ? "AND" : "WHERE";
sql.append(connector).append(" c.name = ? ");
}
if (searchConditions.containsKey(Constants.KEYWORD)) {
String connector = hasWhere ? "AND" : "WHERE";
sql.append(connector).append(" (b.title LIKE ? OR b.created_by LIKE ? OR b.content LIKE ?) ");
}
sql.append(" ORDER BY b.created_at DESC ");
sql.append(" LIMIT ? OFFSET ?");
try (Connection conn = DriverManagerUtils.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql.toString())) {
int index = 0;
if (searchConditions.containsKey(Constants.START_DATE)
&& searchConditions.containsKey(Constants.END_DATE)) {
pstmt.setString(
++index, searchConditions.get(Constants.START_DATE) + Constants.START_DATE_START_TIME);
pstmt.setString(
++index, searchConditions.get(Constants.END_DATE) + Constants.END_DATE_END_TIME);
}
if (searchConditions.containsKey(Constants.CATEGORY)) {
pstmt.setString(++index, searchConditions.get(Constants.CATEGORY));
}
if (searchConditions.containsKey(Constants.KEYWORD)) {
String keyword =
Constants.PERSENT_SIGN
+ searchConditions.get(Constants.KEYWORD)
+ Constants.PERSENT_SIGN;
pstmt.setString(++index, keyword);
pstmt.setString(++index, keyword);
pstmt.setString(++index, keyword);
}
pstmt.setInt(++index, limit);
pstmt.setInt(++index, offset);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
boards.add(
new Board(
rs.getLong(Constants.ID_COLUMN),
rs.getString(Constants.TITLE_COLUMN),
rs.getString(Constants.CONTENT_COLUMN),
rs.getString(Constants.PASSWORD_COLUMN),
rs.getInt(Constants.VIEW_COUNT_COLUMN),
rs.getTimestamp(Constants.CREATED_AT_COLUMN).toLocalDateTime(),
rs.getString(Constants.CREATED_BY_COLUMN),
rs.getTimestamp(Constants.UPDATED_AT_COLUMN) != null
? rs.getTimestamp(Constants.UPDATED_AT_COLUMN).toLocalDateTime()
: null,
rs.getLong(Constants.CATEGORY_ID_COLUMN)));
}
}
} catch (SQLException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
return boards;
}
public class QueryBuilder {
private final StringBuilder sql;
private final List<Object> parameters;
private QueryBuilder(String baseQuery) {
this.sql = new StringBuilder(baseQuery);
this.parameters = new ArrayList<>();
}
public static QueryBuilder create(String baseQuery) {
return new QueryBuilder(baseQuery);
}
public QueryBuilder addCondition(String condition, Object... params) {
if (!Objects.isNull(condition)) {
sql.append(parameters.isEmpty() ? " WHERE " : " AND ").append(condition);
if (!Objects.isNull(params)) {
parameters.addAll(Arrays.asList(params));
}
}
return this;
}
public QueryBuilder orderBy(String orderBy) {
if (!Objects.isNull(orderBy)) {
sql.append(" ORDER BY ").append(orderBy);
}
return this;
}
public QueryBuilder limitOffset(int limit, int offset) {
sql.append(" LIMIT ? OFFSET ?");
parameters.add(limit);
parameters.add(offset);
return this;
}
public String buildQuery() {
return sql.toString();
}
public List<Object> getParameters() {
return parameters;
}
}
@Override
public List<Board> getBoardList(int page, Map<String, String> searchConditions) {
List<Board> boards = new ArrayList<>();
String baseQuery = """
SELECT
b.id, b.title, b.content, b.view_count, b.created_at, b.created_by, b.updated_at, b.category_id
FROM
boards b
LEFT JOIN
categories c ON b.category_id = c.id
""";
int limit = Constants.BOARD_LIST_PAGE_SIZE;
int offset = (page - 1) * limit;
QueryBuilder queryBuilder = QueryBuilder.create(baseQuery);
String startDate = searchConditions.getOrDefault(Constants.START_DATE, TimeFormatterUtils.getDefaultStartDate()) + Constants.START_DATE_START_TIME;
String endDate = searchConditions.getOrDefault(Constants.END_DATE, TimeFormatterUtils.getDefaultEndDate()) + Constants.END_DATE_END_TIME;
queryBuilder.addCondition("b.created_at BETWEEN ? AND ?", startDate, endDate);
if (searchConditions.containsKey(Constants.CATEGORY)) {
queryBuilder.addCondition("c.name = ?", searchConditions.get(Constants.CATEGORY));
}
if (searchConditions.containsKey(Constants.KEYWORD)) {
String keyword = Constants.PERSENT_SIGN + searchConditions.get(Constants.KEYWORD) + Constants.PERSENT_SIGN;
queryBuilder.addCondition("(b.title LIKE ? OR b.created_by LIKE ? OR b.content LIKE ?)",
keyword, keyword, keyword);
}
queryBuilder.orderBy("b.created_at DESC").limitOffset(limit, offset);
String sql = queryBuilder.buildQuery();
List<Object> parameters = queryBuilder.getParameters();
try (Connection conn = DriverManagerUtils.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
for (int i = 0; i < parameters.size(); i++) {
pstmt.setObject(i + 1, parameters.get(i));
}
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
boards.add(
new Board(
rs.getLong(Constants.ID_COLUMN),
rs.getString(Constants.TITLE_COLUMN),
rs.getString(Constants.CONTENT_COLUMN),
null,
rs.getInt(Constants.VIEW_COUNT_COLUMN),
rs.getTimestamp(Constants.CREATED_AT_COLUMN).toLocalDateTime(),
rs.getString(Constants.CREATED_BY_COLUMN),
rs.getTimestamp(Constants.UPDATED_AT_COLUMN) != null
? rs.getTimestamp(Constants.UPDATED_AT_COLUMN).toLocalDateTime()
: null,
rs.getLong(Constants.CATEGORY_ID_COLUMN)));
}
}
} catch (SQLException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
return boards;
}
public class SQLQueryBuilder implements QueryBuilder {
}
public class QueryDSLQueryBuilder implements QueryBuilder {
}