Skip to content

Commit 3c36fee

Browse files
committed
Lesson07
1 parent b88aea8 commit 3c36fee

258 files changed

Lines changed: 6773 additions & 153 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Lesson07/WebContent/project/ProjectList.jsp

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%-- 2. 정렬 링크 추가 --%>
12
<%@ page
23
language="java"
34
contentType="text/html; charset=UTF-8"
@@ -15,11 +16,61 @@
1516
<p><a href='add.do'>신규 프로젝트</a></p>
1617
<table border="1">
1718
<tr>
18-
<th>번호</th>
19-
<th>제목</th>
20-
<th>시작일</th>
21-
<th>종료일</th>
22-
<th>상태</th>
19+
<th><c:choose>
20+
<c:when test="${orderCond == 'PNO_ASC'}">
21+
<a href="list.do?orderCond=PNO_DESC">번호↑</a>
22+
</c:when>
23+
<c:when test="${orderCond == 'PNO_DESC'}">
24+
<a href="list.do?orderCond=PNO_ASC">번호↓</a>
25+
</c:when>
26+
<c:otherwise>
27+
<a href="list.do?orderCond=PNO_ASC">번호︎</a>
28+
</c:otherwise>
29+
</c:choose></th>
30+
<th><c:choose>
31+
<c:when test="${orderCond == 'TITLE_ASC'}">
32+
<a href="list.do?orderCond=TITLE_DESC">제목↑</a>
33+
</c:when>
34+
<c:when test="${orderCond == 'TITLE_DESC'}">
35+
<a href="list.do?orderCond=TITLE_ASC">제목↓</a>
36+
</c:when>
37+
<c:otherwise>
38+
<a href="list.do?orderCond=TITLE_ASC">제목︎</a>
39+
</c:otherwise>
40+
</c:choose></th>
41+
<th><c:choose>
42+
<c:when test="${orderCond == 'STARTDATE_ASC'}">
43+
<a href="list.do?orderCond=STARTDATE_DESC">시작일↑</a>
44+
</c:when>
45+
<c:when test="${orderCond == 'STARTDATE_DESC'}">
46+
<a href="list.do?orderCond=STARTDATE_ASC">시작일↓</a>
47+
</c:when>
48+
<c:otherwise>
49+
<a href="list.do?orderCond=STARTDATE_ASC">시작일</a>
50+
</c:otherwise>
51+
</c:choose></th>
52+
<th><c:choose>
53+
<c:when test="${orderCond == 'ENDDATE_ASC'}">
54+
<a href="list.do?orderCond=ENDDATE_DESC">종료일↑</a>
55+
</c:when>
56+
<c:when test="${orderCond == 'ENDDATE_DESC'}">
57+
<a href="list.do?orderCond=ENDDATE_ASC">종료일↓</a>
58+
</c:when>
59+
<c:otherwise>
60+
<a href="list.do?orderCond=ENDDATE_ASC">종료일</a>
61+
</c:otherwise>
62+
</c:choose></th>
63+
<th><c:choose>
64+
<c:when test="${orderCond == 'STATE_ASC'}">
65+
<a href="list.do?orderCond=STATE_DESC">상태↑</a>
66+
</c:when>
67+
<c:when test="${orderCond == 'STATE_DESC'}">
68+
<a href="list.do?orderCond=STATE_ASC">상태↓</a>
69+
</c:when>
70+
<c:otherwise>
71+
<a href="list.do?orderCond=STATE_ASC">상태</a>
72+
</c:otherwise>
73+
</c:choose></th>
2374
<th></th>
2475
</tr>
2576
<c:forEach var="project" items="${projects}">

Lesson07/build/classes/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/spms
99 Bytes
Binary file not shown.
Binary file not shown.
-3.17 KB
Binary file not shown.
76 Bytes
Binary file not shown.
705 Bytes
Binary file not shown.

Lesson07/src/spms/context/ApplicationContext.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,21 @@
1313

1414
import spms.annotation.Component;
1515

16-
//2. 프로퍼티 파일 및 애노테이션을 이용한 객체 준비
16+
//2. mybatis 적용에 필요한 변경
1717
public class ApplicationContext {
1818
Hashtable<String,Object> objTable = new Hashtable<String,Object>();
1919

2020
public Object getBean(String key) {
2121
return objTable.get(key);
2222
}
2323

24-
public ApplicationContext(String propertiesPath) throws Exception {
25-
Properties props = new Properties();
26-
props.load(new FileReader(propertiesPath));
27-
28-
prepareObjects(props);
29-
prepareAnnotationObjects();
30-
injectDependency();
24+
public void addBean(String name, Object obj) {
25+
objTable.put(name, obj);
3126
}
3227

33-
private void prepareAnnotationObjects()
28+
public void prepareObjectsByAnnotation(String basePackage)
3429
throws Exception{
35-
Reflections reflector = new Reflections("");
30+
Reflections reflector = new Reflections(basePackage);
3631

3732
Set<Class<?>> list = reflector.getTypesAnnotatedWith(Component.class);
3833
String key = null;
@@ -42,7 +37,10 @@ private void prepareAnnotationObjects()
4237
}
4338
}
4439

45-
private void prepareObjects(Properties props) throws Exception {
40+
public void prepareObjectsByProperties(String propertiesPath) throws Exception {
41+
Properties props = new Properties();
42+
props.load(new FileReader(propertiesPath));
43+
4644
Context ctx = new InitialContext();
4745
String key = null;
4846
String value = null;
@@ -58,7 +56,7 @@ private void prepareObjects(Properties props) throws Exception {
5856
}
5957
}
6058

61-
private void injectDependency() throws Exception {
59+
public void injectDependency() throws Exception {
6260
for (String key : objTable.keySet()) {
6361
if (!key.startsWith("jndi.")) {
6462
callSetter(objTable.get(key));
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
package spms.controls.project;
22

3+
import java.util.HashMap;
34
import java.util.Map;
45

56
import spms.annotation.Component;
7+
import spms.bind.DataBinding;
68
import spms.controls.Controller;
79
import spms.dao.ProjectDao;
810

911
@Component("/project/list.do")
10-
public class ProjectListController implements Controller {
12+
public class ProjectListController implements Controller, DataBinding {
1113
ProjectDao projectDao;
1214

1315
public ProjectListController setMemberDao(ProjectDao projectDao) {
1416
this.projectDao = projectDao;
1517
return this;
1618
}
19+
20+
public Object[] getDataBinders() {
21+
return new Object[]{
22+
"orderCond", String.class
23+
};
24+
}
1725

1826
@Override
1927
public String execute(Map<String, Object> model) throws Exception {
20-
model.put("projects", projectDao.selectList());
28+
HashMap<String,Object> paramMap = new HashMap<String,Object>();
29+
paramMap.put("orderCond", model.get("orderCond"));
30+
31+
model.put("projects", projectDao.selectList(paramMap));
2132
return "/project/ProjectList.jsp";
2233
}
2334
}
Lines changed: 30 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,72 @@
11
package spms.dao;
22

3-
import java.sql.Connection;
4-
import java.sql.PreparedStatement;
5-
import java.sql.ResultSet;
6-
import java.sql.Statement;
7-
import java.util.ArrayList;
3+
//3. 동적 SQL 적용
4+
import java.util.HashMap;
85
import java.util.List;
96

10-
import javax.sql.DataSource;
7+
import org.apache.ibatis.session.SqlSession;
8+
import org.apache.ibatis.session.SqlSessionFactory;
119

1210
import spms.annotation.Component;
1311
import spms.vo.Project;
1412

1513
@Component("projectDao")
1614
public class MySqlProjectDao implements ProjectDao {
17-
DataSource ds;
15+
SqlSessionFactory sqlSessionFactory;
1816

19-
public void setDataSource(DataSource ds) {
20-
this.ds = ds;
17+
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
18+
this.sqlSessionFactory = sqlSessionFactory;
2119
}
2220

23-
public List<Project> selectList() throws Exception {
24-
Connection connection = null;
25-
Statement stmt = null;
26-
ResultSet rs = null;
27-
21+
public List<Project> selectList(HashMap<String,Object> paramMap)
22+
throws Exception {
23+
SqlSession sqlSession = sqlSessionFactory.openSession();
2824
try {
29-
connection = ds.getConnection();
30-
stmt = connection.createStatement();
31-
rs = stmt.executeQuery(
32-
"SELECT PNO,PNAME,STA_DATE,END_DATE,STATE" +
33-
" FROM PROJECTS" +
34-
" ORDER BY PNO DESC");
35-
36-
ArrayList<Project> projects = new ArrayList<Project>();
37-
38-
while(rs.next()) {
39-
projects.add(new Project()
40-
.setNo(rs.getInt("PNO"))
41-
.setTitle(rs.getString("PNAME"))
42-
.setStartDate(rs.getDate("STA_DATE"))
43-
.setEndDate(rs.getDate("END_DATE"))
44-
.setState(rs.getInt("STATE")) );
45-
}
46-
47-
return projects;
48-
49-
} catch (Exception e) {
50-
throw e;
51-
25+
return sqlSession.selectList("spms.dao.ProjectDao.selectList", paramMap);
5226
} finally {
53-
try {if (rs != null) rs.close();} catch(Exception e) {}
54-
try {if (stmt != null) stmt.close();} catch(Exception e) {}
55-
try {if (connection != null) connection.close();} catch(Exception e) {}
27+
sqlSession.close();
5628
}
5729
}
5830

5931
public int insert(Project project) throws Exception {
60-
Connection connection = null;
61-
PreparedStatement stmt = null;
62-
32+
SqlSession sqlSession = sqlSessionFactory.openSession();
6333
try {
64-
connection = ds.getConnection();
65-
stmt = connection.prepareStatement(
66-
"INSERT INTO PROJECTS"
67-
+ "(PNAME,CONTENT,STA_DATE,END_DATE,STATE,CRE_DATE,TAGS)"
68-
+ " VALUES (?,?,?,?,0,NOW(),?)");
69-
stmt.setString(1, project.getTitle());
70-
stmt.setString(2, project.getContent());
71-
stmt.setDate(3, new java.sql.Date(project.getStartDate().getTime()));
72-
stmt.setDate(4, new java.sql.Date(project.getEndDate().getTime()));
73-
stmt.setString(5, project.getTags());
74-
75-
return stmt.executeUpdate();
76-
77-
} catch (Exception e) {
78-
throw e;
79-
34+
int count = sqlSession.insert("spms.dao.ProjectDao.insert", project);
35+
sqlSession.commit();
36+
return count;
8037
} finally {
81-
try {if (stmt != null) stmt.close();} catch(Exception e) {}
82-
try {if (connection != null) connection.close();} catch(Exception e) {}
38+
sqlSession.close();
8339
}
8440
}
8541

8642
public Project selectOne(int no) throws Exception {
87-
Connection connection = null;
88-
Statement stmt = null;
89-
ResultSet rs = null;
43+
SqlSession sqlSession = sqlSessionFactory.openSession();
9044
try {
91-
connection = ds.getConnection();
92-
stmt = connection.createStatement();
93-
rs = stmt.executeQuery(
94-
"SELECT PNO,PNAME,CONTENT,STA_DATE,END_DATE,STATE,CRE_DATE,TAGS"
95-
+ " FROM PROJECTS WHERE PNO=" + no);
96-
if (rs.next()) {
97-
return new Project()
98-
.setNo(rs.getInt("PNO"))
99-
.setTitle(rs.getString("PNAME"))
100-
.setContent(rs.getString("CONTENT"))
101-
.setStartDate(rs.getDate("STA_DATE"))
102-
.setEndDate(rs.getDate("END_DATE"))
103-
.setState(rs.getInt("STATE"))
104-
.setCreatedDate(rs.getDate("CRE_DATE"))
105-
.setTags(rs.getString("TAGS"));
106-
107-
} else {
108-
throw new Exception("해당 번호의 프로젝트를 찾을 수 없습니다.");
109-
}
110-
111-
} catch (Exception e) {
112-
throw e;
45+
return sqlSession.selectOne("spms.dao.ProjectDao.selectOne", no);
11346
} finally {
114-
try {if (rs != null) rs.close();} catch(Exception e) {}
115-
try {if (stmt != null) stmt.close();} catch(Exception e) {}
116-
try {if (connection != null) connection.close();} catch(Exception e) {}
47+
sqlSession.close();
11748
}
11849
}
11950

12051
public int update(Project project) throws Exception {
121-
Connection connection = null;
122-
PreparedStatement stmt = null;
52+
SqlSession sqlSession = sqlSessionFactory.openSession();
12353
try {
124-
connection = ds.getConnection();
125-
stmt = connection.prepareStatement(
126-
"UPDATE PROJECTS SET "
127-
+ " PNAME=?,"
128-
+ " CONTENT=?,"
129-
+ " STA_DATE=?,"
130-
+ " END_DATE=?,"
131-
+ " STATE=?,"
132-
+ " TAGS=?"
133-
+ " WHERE PNO=?");
134-
stmt.setString(1, project.getTitle());
135-
stmt.setString(2, project.getContent());
136-
stmt.setDate(3, new java.sql.Date(project.getStartDate().getTime()));
137-
stmt.setDate(4, new java.sql.Date(project.getEndDate().getTime()));
138-
stmt.setInt(5, project.getState());
139-
stmt.setString(6, project.getTags());
140-
stmt.setInt(7, project.getNo());
141-
142-
return stmt.executeUpdate();
143-
144-
} catch (Exception e) {
145-
throw e;
146-
54+
int count = sqlSession.update("spms.dao.ProjectDao.update", project);
55+
sqlSession.commit();
56+
return count;
14757
} finally {
148-
try {if (stmt != null) stmt.close();} catch(Exception e) {}
149-
try {if (connection != null) connection.close();} catch(Exception e) {}
58+
sqlSession.close();
15059
}
15160
}
15261

15362
public int delete(int no) throws Exception {
154-
Connection connection = null;
155-
Statement stmt = null;
156-
63+
SqlSession sqlSession = sqlSessionFactory.openSession();
15764
try {
158-
connection = ds.getConnection();
159-
stmt = connection.createStatement();
160-
return stmt.executeUpdate(
161-
"DELETE FROM PROJECTS WHERE PNO=" + no);
162-
163-
} catch (Exception e) {
164-
throw e;
165-
65+
int count = sqlSession.delete("spms.dao.ProjectDao.delete", no);
66+
sqlSession.commit();
67+
return count;
16668
} finally {
167-
try {if (stmt != null) stmt.close();} catch(Exception e) {}
168-
try {if (connection != null) connection.close();} catch(Exception e) {}
69+
sqlSession.close();
16970
}
17071
}
17172
}

0 commit comments

Comments
 (0)