๋ฏธ๋ํ๋ก์ ํธ1 Day2
โข
Github ๋ ํฌ์งํ ๋ฆฌ ๋ง๋ค๊ธฐ
โฆ
Branch ๋ง๋ค๊ธฐ
โฆ
Git Clone
โฆ
๊ธฐ๋ณธ ํ๋ก์ ํธ ์์ฑ
โช
๋ผ์ด๋ธ๋ฌ๋ฆฌ
โข
mysql jdbc ๋๋ผ์ด๋ฒ
โข
jstl
โข
alcl-jdbc
โข
lombok
โข
jbcrypt
โฆ
๊ธฐ๋ณธ ํ๋ก์ ํธ PUSH
โฆ
ํ์ ๋ณ clone or pull
โฆ
switch branch
โฆ
๊ฐ๋ฐ ์์
โข
DAO ์๋ CRUD ๋ผ์ด๋ธ๋ฌ๋ฆฌ - alcl-jdbc
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
๋ณต์ฌ