porm.xml 에 추가 (아래에있음)
http://mvnrepository.com/
MongoDB Java Driver
Spring Data MongoDB Core
porm.xml
Spring 버전 변경
<!-- Spring -->
<spring-framework.version>4.0.9.RELEASE</spring-framework.version>
#몽고DB Core
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.6.1.RELEASE</version>
</dependency>
#몽고DBJAVA드라이버
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.4</version>
</dependency>
web.xml 인코딩 문제
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
제일아래쪽 servlet
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
src/main/java
test.mongodb 패키지 생성
mvc-config
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="test.*"></context:component-scan>
application-config
**namespace 에서 mongo 추가
<!-- mongo db server setting -->
<mongo:mongo id="mongo" host="127.0.0.1" port="27017">
<mongo:options
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000"
max-wait-time="1500"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="1500"
write-number="1"
write-timeout="0"
write-fsync="true"
/>
</mongo:mongo>
test.mongodb/ListController.java
@Controller
public class ListController {
@RequestMapping("/mongo/list.do")
public ModelAndView list()
{
ModelAndView model=new ModelAndView();
model.setViewName("boardlist");
return model;
}
}
src/main/java/test.mongodb/Userdao.java
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
@Repository
public class Userdao {
@Autowired
private MongoTemplate mongoTemp;
public void dropCollection()
{
mongoTemp.dropCollection(UserDao.class);
}
public List<UserDto> findList()
{
//방법 1 : 전체 데이타를 목록으로 얻고자 할 경우
List<UserDto> list=mongoTemp.findAll(UserDto.class,"dto");
//방법 2 - 조건에 의한 목록을 얻고자 할경우
/* Query query=new Query(Criteria.where("_id").in("a1","a2","a3"));
List<UserDto> list=mongoTemp.find(query, UserDto.class);*/
return list;
}
}
ListController
@Autowired
private Userdao dao;
List<UserDto> list=dao.findList();
model.addObject("list",list);
model.addObject("count",list.size());
추가
boardlist
부트스트랩 추가 head 에
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
디자인부분 추가
<input type="button" class="btn btn-sm btn-success" value="멤버등록" onclick="location.href='writeform.do'" style="magin-left:50px;">
<table style="margin:0 auto;" class="table table-striped">
<caption><b>MongoDB Test</b></caption>
<tr bgcolor="#ffffcc">
<th style="text-align: center;">순번</th>
<th style="text-align: center;">이름</th>
<th style="text-align: center;">핸드폰</th>
<th style="text-align: center;">주소</th>
<th style="text-align: center;">나이</th>
<th style="text-align: center;">편집</th>
</tr>
<c:forEach var="dto" items="${list}" varStatus="i">
<tr>
<td align="center">${i.count}</td>
<td align="center">${dto.name}</td>
<td align="center">${dto.hp}</td>
<td align="center">${dto.address}</td>
<td align="center">${dto.age}</td>
<td align="center">
<!-- info 파란색 버튼 danger 빨간색 버튼 -->
<input type="button" value="수정" class="btn btn-cs btn-info"
onclick="location.href='updateform.do?name=${dto.name}'">
<input type="button" value="삭제" class="btn btn-cs btn-danger"
onclick="location.href='delete.do?name=${dto.name}'">
</td>
</tr>
</c:forEach>
</table>
</body>
webapp/index.jsp
body 내용을 수정 list.do 로 표출되도록
<c:redirect url="mongo/list.do"></c:redirect>
여기까지 진행하였을경우 프로젝트 실행시 정상적으로 입력된 게시글들이 출력되어야함.
WriteController.java
@Controller
public class WriteController {
@Autowired
private UserDao dao;
@RequestMapping("/mongo/writeform.do")
public String form()
{
return "writeform";
}
}
UserDao.java 추가
//dto 한개 저장시
public void insert(UserDto dto)
{
mongoTemp.insert(dto);
}
//dto 여러개를 List 에 담아 저장시
public void insertList(List<UserDto> list)
{
mongoTemp.insert(list,UserDto.class);
}
WriteController
@RequestMapping("/mongo/write.do")
public String readData(@ModelAttribute UserDto dto)
{
dao.insert(dto);
return "redirect:list.do";
}
여기까지 진행하면 맴버등록이 됨.
boardlist.jsp
네임클릭시 반응하게 네임부분 수정
<td align="center">
<a href="content.do?name=${dto.name}">${dto.name}</a>
</td>
/WEB-INF/view/content.jsp 생성
UserDao.java 추가
public UserDto getSearchName(String name)
{
Query query=new Query(Criteria.where("name").is(name));
return mongoTemp.findOne(query, UserDto.class);
}
ListController.java 추가
@RequestMapping("/mongo/content.do")
public ModelAndView content(@RequestParam String name)
{
ModelAndView model=new ModelAndView();
UserDto dto=dao.getSearchName(name);
model.addObject("dto",dto);
model.setViewName("content");
return model;
}
Content.jsp -> writeform 에 있는거 그대로 복사해서 수정
form 삭제후
<body>
<table class="table table-bordered" style="width:300px; margin: 0 auto;">
<caption><b>맴버 확인</b></caption>
<tr>
<th style="width:100px;">이름</th>
<td>
${dto.name}
</td>
</tr>
<tr>
<th style="width:100px;">핸드폰</th>
<td>
${dto.hp}
</td>
</tr>
<tr>
<th style="width:100px;">주소</th>
<td>
${dto.address}
</td>
</tr>
<tr>
<th style="width:100px;">나이</th>
<td>
${dto.age}세
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="멤버목록" class="btn btn-sm btn-success" onclick="location.href='list.do'">
</td>
</tr>
</table>
</body>
여기까지 이상없이 진행시
---이름 수정 누르면 데이터 출력--
삭제기능추가
UserDao.java
public void deletedata(String name)
{
Query query=new Query(Criteria.where("name").is(name));
mongoTemp.remove(query,UserDto.class);
}
deleteController.java
@Controller
public class DeleteController {
@Autowired
private UserDao dao;
@RequestMapping("mongo/delete.do")
public String delete(@RequestParam String name)
{
dao.deletedata(name);
return "redirect:list.do";
}
}
여기까지 진행시 삭제버튼누르면 해당 이름 삭제됨.
수정기능
writeform.jsp 내용을 updateform.jsp 로 복사
updateform.jsp
<body>
<form action="update.do" method="post">
<table class="table table-bordered" style="width:300px; margin: 0 auto;">
<caption><b>맴버 정보수정하기</b></caption>
<tr>
<th style="width:100px;">이름</th>
<td>
${dto.name }
<input type="hidden" name="name" value="${dto.name}">
</td>
</tr>
<tr>
<th style="width:100px;">핸드폰</th>
<td>
<input type="text" name="hp" required="required" value="${dto.hp}">
</td>
</tr>
<tr>
<th style="width:100px;">주소</th>
<td>
<input type="text" name="address" required="required" value="${dto.address}">
</td>
</tr>
<tr>
<th style="width:100px;">나이</th>
<td>
<input type="text" name="age" required="required" value="${dto.age}">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="mongodb수정" class="btn btn-sm btn-warning">
<input type="button" value="멤버목록" class="btn btn-sm btn-success" onclick="location.href='list.do'">
</td>
</tr>
</table>
</form>
</body>
updateController.java
@Controller
public class UpdateController {
@Autowired
private UserDao dao;
@RequestMapping("/mongo/updateform.do")
public ModelAndView updateform(@RequestParam String name)
{
ModelAndView model=new ModelAndView();
UserDto dto=dao.getSearchName(name);
model.addObject("dto",dto);
model.setViewName("updateform");
return model;
}
}
-----------------------여기까지 했을때 수정폼 나오면 정상
Userdao.java
public void updateData(UserDto dto)
{
Query query=new Query(Criteria.where("name").is(dto.getName()));
mongoTemp.updateFirst(query, Update.update("hp", dto.getHp()), UserDto.class);
mongoTemp.updateFirst(query, Update.update("address", dto.getAddress()), UserDto.class);
mongoTemp.updateFirst(query, Update.update("age", dto.getAge()), UserDto.class);
}
updateController.java
@RequestMapping("/mongo/update.do")
public String update(@ModelAttribute UserDto dto)
{
dao.updateData(dto);
return "redirect:list.do";
}
'프로그래밍언어 > JAVA' 카테고리의 다른 글
부트스트랩3,4 head 소스 (0) | 2018.09.19 |
---|---|
깃허브 이클립스 사용법 (Git HUB) 가장쉬운방법! (0) | 2018.09.15 |
JAVA 크롤링 멜론 뮤직차트 크롤링 예제 (0) | 2018.08.09 |
JAVA 크롤링 Daum 뉴스 크롤링 (0) | 2018.08.09 |
Tomcat 이클립스 서버 설치 & 구동하기 (1) | 2018.05.31 |