com.example.domain>AddressVO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.example.domain;
public class AddressVO {
private int id;
private String name;
private String address;
private String tel;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String toString() {
return "AddressVO [id=" + id + ", name=" + name + ", address=" + address + ", tel=" + tel + "]";
}
}
|
com.example.domain>Criteria.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
//시작 페이지,DISPLAY,페이지 정보
package com.example.domain;
public class Criteria{
private int page;
private int perPageNum;
//한 페이지에 출력할 수 있는 개수
public Criteria(){
this.page = 1;
this.perPageNum = 3;
//한 페이지당 3개가 나온다.
}
//페이지를 입력 안했을때 설정
public void setPage(int page){
if (page <= 0){
this.page = 1;
return;
}
this.page = page;
}
//시작 페이지 지정 메서드
public void setPerPageNum(int perPageNum){
if(perPageNum <= 0 || perPageNum > 100){
this.perPageNum = 10;
return;
}
this.perPageNum = perPageNum;
}
public int getPage(){
return page;
}
//실저 데이터를 몇개 부터 가져올지 가져오는 메서드
public int getPageStart(){
return (this.page - 1) * perPageNum;
}
public int getPerPageNum(){
return this.perPageNum;
}
@Override
public String toString(){
return "Criteria [page=" + page + ", " + "perPageNum=" + perPageNum + "]";
}
}
|
com.example.domain>PageMaker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
//페이지 번호 클래스
package com.example.domain;
public class PageMaker{
private int totalCount;
private int startPage;
private int endPage;
//true,false 값으로 리턴
private boolean prev;
//이전 버튼이 보여지는지
private boolean next;
//다음 버튼이 보여지는지
private int displayPageNum = 10;
//한페이지당 보여지는 데이터 개수
private Criteria cri;
public void setCri(Criteria cri){
this.cri = cri;
}
public void setTotalCount(int totalCount){
this.totalCount = totalCount;
calcData();
}
private void calcData(){
startPage = (endPage - displayPageNum) + 1;
if(endPage > tempEndPage){
endPage = tempEndPage;
}
prev = startPage == 1 ? false : true;
next = endPage * cri.getPerPageNum() >= totalCount ? false : true;
}
public int getStartPage() {
return startPage;
}
public void setStartPage(int startPage) {
this.startPage = startPage;
}
public int getEndPage() {
return endPage;
}
public void setEndPage(int endPage) {
this.endPage = endPage;
}
public boolean isPrev() {
return prev;
}
public void setPrev(boolean prev) {
this.prev = prev;
}
public boolean isNext() {
return next;
}
public void setNext(boolean next) {
this.next = next;
}
public int getDisplayPageNum() {
return displayPageNum;
}
public void setDisplayPageNum(int displayPageNum) {
this.displayPageNum = displayPageNum;
}
public int getTotalCount() {
return totalCount;
}
public Criteria getCri() {
return cri;
}
}
|
h |
com.example.domain>SearchCriteria.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//Criteria 상속
package com.example.domain;
public class SearchCriteria extends Criteria{
private String searchType;
private String keyword;
public String getSearchType() {
return searchType;
}
public void setSearchType(String searchType) {
this.searchType = searchType;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
@Override
public String toString() {
return "SearchCriteria [searchType=" + searchType + ", keyword=" + keyword + "]";
}
}
|
com.example.persistence>AddressDAO.Interface
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.example.persistence;
import java.util.List;
public interface AddressDAO {
public List<AddressVO> list(SearchCriteria cri) throws Exception;
public int total() throws Exception;
public List<AddressVO> slist(SearchCriteria cri) throws Exception;
public int stotal(SearchCriteria cri) throws Exception;
public void delete(int id) throws Exception;
public void insert(AddressVO vo)throws Exception;
public AddressVO read(int id) throws Exception;
public void update(AddressVO vo) throws Exception;
}
|
src/test/java-com.example.web-DBTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.example.web;
import javax.inject.Inject;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.example.persistence.AddressDAO;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/**/*.xml"})
public class DBTest {
@Inject
AddressDAO dao;
@Test
public void list() throws Exception{
SearchCriteria cri=new SearchCriteria();
cri.setPage(2);
cri.setPerPageNum(10);
}
}
|
com.example.web>AddressController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package com.example.persistence;
import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Repository;
@Repository
public class AddressDAOImpl implements AddressDAO {
@Inject
SqlSession session;
private static final String namespace="AddressMapper";
@Override
public List<AddressVO> list(SearchCriteria cri) throws Exception {
// TODO Auto-generated method stub
return session.selectList(namespace + ".list" ,cri);
}
@Override
public int total() throws Exception {
// TODO Auto-generated method stub
return session.selectOne(namespace +".total");
}
@Override
public List<AddressVO> slist(SearchCriteria cri) throws Exception {
// TODO Auto-generated method stub
return session.selectList(namespace +".slist",cri);
}
@Override
public int stotal(SearchCriteria cri) throws Exception {
// TODO Auto-generated method stub
return session.selectOne(namespace +".stotal", cri);
}
@Override
public void delete(int id) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void insert(AddressVO vo) throws Exception {
// TODO Auto-generated method stub
}
@Override
public AddressVO read(int id) throws Exception {
// TODO Auto-generated method stub
return session.selectOne(namespace +".read", id);
}
@Override
public void update(AddressVO vo) throws Exception {
// TODO Auto-generated method stub
}
}
|
src/main/resource>mappers>AddressMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
<mapper namespace="AddressMapper">
select * from tbl_address
order by id desc
limit #{pageStart},#{perPageNum}
</select>
<select id="total" resultType="int">
select count(*) from tbl_address
</select>
select * from tbl_address
<if test="searchType=='name'">
where name like concat('%',#{keyword} ,'%')
</if>
<if test="searchType=='address'">
where address like concat('%',#{keyword},'%')
</if>
order by id desc
limit #{pageStart},#{perPageNum}
</select>
<select id="stotal" resultType="int">
select count(*) from tbl_address
<if test="searchType=='name'">
where name like concat('%',#{keyword} ,'%')
</if>
<if test="searchType=='address'">
where address like concat('%',#{keyword},'%')
</if>
</select>
<delete id="delete">
delete from tbl_address
where id=#{id}
</delete>
<insert id="insert">
insert into tbl_address(name,tel,address)
values(#{name},#{tel},#{address})
</insert>
select * from tbl_address where id=#{id}
</select>
<update id="update">
update tbl_address set name=#{name},address=#{address},tel=#{tel}
where id=#{id}
</update>
</mapper>
|
[src]-[main]-[webapp]-[WEB-INF]-[views]-list.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>주소목록</title>
<script
<!-- 합쳐지고 최소화된 최신 CSS -->
<link rel="stylesheet"
<!-- 부가적인 테마 -->
<link rel="stylesheet"
<!-- 합쳐지고 최소화된 최신 자바스크립트 -->
<script
<style>
#D {
position: absolute;
top: 0px;
left: 0px;
right: 0px;
height: 100%;
width: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 1000;
overflow-y: scroll;
display: none;
}
#L {
width: 700px;
margin: 20px auto;
padding: 15px;
border: 1px solid #333333;
border-radius: 5px;
background: white;
box-shadow: 0px 5px 5px rgba(34, 25, 25, 0.4);
text-align: center;
}
</style>
</head>
<body>
<h1>[주소입력]</h1>
<div>
<table border=1 width=700 class="table table-bordered">
<tr>
<td>이름</td>
<td><input type="text" id="txtName"></td>
</tr>
<tr>
<td>☏전화번호☏</td>
<td><input type="text" id="txtTel"></td>
</tr>
<tr>
<td>주소</td>
<td><input type="text" id="txtAddress"></td>
</tr>
</table>
<input type="button" value="입력" id="btnInsert">
</div>
<h1>[주소목록]</h1>
<div>
<select id="searchType">
<option value="name">이름</option>
<option value="address">주소</option>
</select> <input type="text" id="keyword"> <input type="button"
value="검색" id="btnSearch">
</div>
데이터:
<span id="total"></span>
<table border=1 width=700 class="table table-bordered">
<tr>
<th>NO.</th>
<th>NAME.</th>
<th>TEL.</th>
<th>ADDRESS.</th>
<th>DELETE.</th>
<th>UPDATE.</th>
</tr>
</table>
<table id="tbl" border=1 width=700 class="table table-bordered"></table>
<script id="temp" type="text/x-handlebars-template">
{{#each list}}
<tr class="row">
<td class="id">{{id}}</td>
<td>{{name}}</td>
<td>{{tel}}</td>
<td>{{address}}</td>
<td><button class="btnDelete" bid="{{id}}">삭제</button></td>
<td><button class="btnUpdate" bid="{{id}}">수정</button></td>
</tr>
{{/each}}
</script>
<!-- 라이트박스 -->
<div id="D">
<div id="L">
<input type="text" id="editId">
<table border=1 width=700 class="table table-bordered">
<tr>
<td>이름</td>
<td><input type="text" id="editName"></td>
</tr>
<tr>
<td>☏전화번호☏</td>
<td><input type="text" id="editTel"></td>
</tr>
<tr>
<td>주소</td>
<td><input type="text" id="editAddress"></td>
</tr>
</table>
<input type="button" value="저장" id="btnSave">
<button id="btnClose">닫기</button>
</div>
</div>
<ul id="pagination" class="pagination"></ul>
</body>
<script>
$("#tbl").on("click",".row .btnUpdate", function(){
$("#D").show();
var id=$(this).attr("bid");
$("#editId").val(id);
$.ajax({
type:"get",
url:"read.json",
data:{"id":id},
success:function(data){
$("#editName").val(data.name);
$("#editTel").val(data.tel);
$("#editAddress").val(data.address);
}
});
});
$("#btnClose").on("click", function(){
$("#D").hide();
});
var page = 1;
var searchType = $("#searchType").val();
var keyword = $("#keyword").val();
$("#tbl").on("click", ".row .btnDelete", function() {
if (!confirm("삭제하시겠습니까?"))
return;
var id = $(this).parent().parent().find(".id").html();
//alert(id);
$.ajax({
type : "post",
url : "delete",
data : {
"id" : id
},
success : function() {
getList();
}
});
});
$("#btnSave").on("click", function(){
var id=$("#editId").val();
var name=$("#editName").val();
var tel=$("#editTel").val();
var address=$("#editAddress").val();
$.ajax({
type:"post",
url:"update",
data:{"id":id,"name":name,"tel":tel,"address":address},
success:function(){
alert("저장되었습니다.");
$("#D").hide();
getList();
}
});
});
$("#btnInsert").on("click", function() {
//alert("확인");
var name = $("#txtName").val();
var tel = $("#txtTel").val();
var address = $("#txtAddress").val();
//alert(name +"-"+ tel +"-"+ address);
/*
if(name.length==0 || tel.length==0 || address==0){
alert("데이타를 잘 입력하세요 ");
return;
}
*/
$.ajax({
type : "post",
url : "insert",
data : {
"name" : name,
"tel" : tel,
"address" : address
},
success : function() {
alert("저장되었습니다.");
getList();
}
});
});
getList();
function getList() {
$.ajax({
type : "get",
url : "list.json",
data : {
"page" : page,
"searchType" : searchType,
"keyword" : keyword
},
dataType : "json",
//받는 data가 json이라는 뜻
success : function(data) {
var temp = Handlebars.compile($("#temp").html());
$("#tbl").html(temp(data));
$("#total").html(data.total);
var str = "";
if (data.pm.prev) {
+ "'>◀</a></li>";
}
if (page == i) {
str = str + "<li class='active'>"
+ "<a href='" + i + "'>" + i + "</a></li>";
} else {
str = str + "<li>" + "<a href='" + i + "'>" + i
+ "</a></li>";
}
}
if (data.pm.next) {
+ "'>▶ </a></li>";
}
$("#pagination").html(str);
}
});
}
$("#pagination").on("click", "li a", function(event) {
event.preventDefault();
page = $(this).attr("href");
//alert(page);
getList();
});
$("#btnSearch").on("click", function() {
searchType = $("#searchType").val();
keyword = $("#keyword").val();
page = 1;
getList();
});
$("#keyword").keydown(function(key) {
if (key.keyCode == 13) {
searchType = $("#searchType").val();
keyword = $("#keyword").val();
page = 1;
getList();
}
});
</script>
</html>
|
'spring > 소스코드' 카테고리의 다른 글
kakaoAPI를 이용한 책검색-검색개수,마지막페이지 여부 (0) | 2019.10.25 |
---|---|
○네이버 xml,json 이용한 검색프로그램 파일 만들기 (0) | 2019.10.24 |
학사 프로그램 만들기 (0) | 2019.10.23 |
c라이브러리를 이용한 학사 페이징 작업 (0) | 2019.10.22 |
페이지 번호로 이동하기 (0) | 2019.10.18 |