Search

2024๋…„10์›”22์ผ - ๋ฏธ๋‹ˆํ”„๋กœ์ ํŠธ1 Day2

๋ฏธ๋‹ˆํ”„๋กœ์ ํŠธ1 Day2

โ€ข
Github ๋ ˆํฌ์ง€ํ† ๋ฆฌ ๋งŒ๋“ค๊ธฐ
โ—ฆ
Branch ๋งŒ๋“ค๊ธฐ
โ—ฆ
Git Clone
โ—ฆ
๊ธฐ๋ณธ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
โ–ช
๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
โ€ข
mysql jdbc ๋“œ๋ผ์ด๋ฒ„
โ€ข
jstl
โ€ข
alcl-jdbc
โ€ข
lombok
โ€ข
jbcrypt
โ—ฆ
๊ธฐ๋ณธ ํ”„๋กœ์ ํŠธ PUSH
โ—ฆ
ํŒ€์› ๋ณ„ clone or pull
โ—ฆ
switch branch
โ—ฆ
๊ฐœ๋ฐœ ์‹œ์ž‘
alcl-jdbc
ALOHA-CLASS

Github ๋ ˆํฌ์ง€ํ† ๋ฆฌ ๋งŒ๋“ค๊ธฐ

alcl-jdbc ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•ด์„œ DAO CRUD ์ž๋™ํ™”ํ•˜๊ธฐ

1.
Table ๋งŒ๋“ค๊ธฐ
2.
DTO ๋งŒ๋“ค๊ธฐ
3.
DAO ๋งŒ๋“ค๊ธฐ
a.
BaseDAOImpl<?> ์ƒ์† ๋ฐ›๊ธฐ
b.
์˜ค๋ฒ„๋ผ์ด๋”ฉ
i.
map()
ii.
pk()
iii.
table()
4.
Service ์—์„œ DAO CRUD ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜๊ธฐ
a.
list()
b.
page()
c.
select()
d.
insert()
e.
update()
f.
delete()

Table ๋งŒ๋“ค๊ธฐ

โ€ข
test.sql
DROP TABLE IF EXISTS test; CREATE TABLE test ( `no` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(100) NOT NULL, `age` INT NULL DEFAULT 1, `main_title` TEXT NULL );
SQL
๋ณต์‚ฌ

DTO ๋งŒ๋“ค๊ธฐ

โ€ข
Test.java
package DTO; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor @Builder public class Test { private int no; private String name; private int age; private String mainTitle; }
Java
๋ณต์‚ฌ

DAO ๋งŒ๋“ค๊ธฐ

1.
BaseDAOImpl<?> ์ƒ์† ๋ฐ›๊ธฐ
2.
์˜ค๋ฒ„๋ผ์ด๋”ฉ
a.
map()
b.
pk()
c.
table()

map()

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ์„ ์ž๋ฐ” DTO ๊ฐ์ฒด๋กœ ๋งคํ•‘ํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

pk()

PK ์ปฌ๋Ÿผ ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ ์ง€์ •ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
CREATE TABLE test ( `no` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(100) NOT NULL, `age` INT NULL DEFAULT 1, `main_title` TEXT NULL );
SQL
๋ณต์‚ฌ
return "no";
Java
๋ณต์‚ฌ

table()

CREATE TABLE test ( `no` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(100) NOT NULL, `age` INT NULL DEFAULT 1, `main_title` TEXT NULL );
SQL
๋ณต์‚ฌ
return "test";
Java
๋ณต์‚ฌ
โ€ข
TestDAO.java
package DAO; import java.sql.ResultSet; import com.alohaclass.jdbc.dao.BaseDAOImpl; import DTO.Test; public class TestDAO extends BaseDAOImpl<Test> { @Override public Test map(ResultSet rs) throws Exception { Test test = new Test(); test.setName( rs.getString("no") ); test.setName( rs.getString("name") ); test.setAge( rs.getInt("age") ); test.setAge( rs.getInt("main_title") ); return test; } @Override public String pk() { return "no"; } @Override public String table() { return "test"; } }
Java
๋ณต์‚ฌ

Service ์—์„œ DAO CRUD ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜๊ธฐ

1.
list()
2.
page()
3.
select()
4.
insert()
5.
update()
6.
delete()
โ€ข
TestService.java
package Service; import java.util.List; import com.alohaclass.jdbc.dto.PageInfo; import DTO.Test; public interface TestService { // C.R.U.D public List<Test> list(); public PageInfo<Test> page(); public Test select(int no); public int insert(Test test); public int update(Test test); public int delete(int no); }
Java
๋ณต์‚ฌ
โ€ข
TestServiceImpl.java
package Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alohaclass.jdbc.dto.Page; import com.alohaclass.jdbc.dto.PageInfo; import DAO.TestDAO; import DTO.Test; public class TestServiceImpl implements TestService { TestDAO testDAO = new TestDAO(); @Override public List<Test> list() { List<Test> list = null; try { list = testDAO.list(); } catch (Exception e) { e.printStackTrace(); } return list; } @Override public PageInfo<Test> page() { PageInfo<Test> pageInfo = null; try { // ๋ฐฉ๋ฒ•1 // pageInfo = testDAO.page(); // ๋ฐฉ๋ฒ•2 Page page = new Page(); page.setPage(1); page.setSize(10); // pageInfo = testDAO.page(page); // ๋ฐฉ๋ฒ•3 String keyword = "๊ฒ€์ƒ‰์–ด"; List<String> searchOptions = new ArrayList<String>(); searchOptions.add("name"); searchOptions.add("age"); // pageInfo = testDAO.page(page, keyword, searchOptions); // ๋ฐฉ๋ฒ•4 Map<String, String> filterOptions = new HashMap<String, String>() {{ put("name", "ASC"); put("age", "DESC"); }}; testDAO.page(page, keyword, searchOptions, filterOptions); } catch (Exception e) { e.printStackTrace(); } return pageInfo; } @Override public Test select(int no) { Test test = null; try { test = testDAO.select(no); } catch (Exception e) { e.printStackTrace(); } return test; } @Override public int insert(Test test) { int result = 0; try { result = testDAO.insert(test); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int update(Test test) { int result = 0; try { result = testDAO.update(test); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int delete(int no) { int result = 0; try { result = testDAO.delete(no); } catch (Exception e) { e.printStackTrace(); } return result; } }
Java
๋ณต์‚ฌ