조회하기
<select id="searchByName" parameterType="String" resultType="userVo">
select * from users where usernm like '%' || #{value} || '%'
</select>
<select id="searchByAlias" parameterType="String" resultType="userVo">
select * from users where alias like '%' || #{value} || '%'
</select>
<select id="searchById" parameterType="String" resultType="userVo">
select * from users where userid like '%' || #{value} || '%'
</select>
<select id="countByName" parameterType="String" resultType="int">
select count(*) from users where usernm like '%' || #{value} || '%'
</select>
<select id="countByAlias" parameterType="String" resultType="int">
select count(*) from users where alias like '%' || #{value} || '%'
</select>
<select id="countById" parameterType="String" resultType="int">
select count(*) from users where userid like '%' || #{value} || '%'
</select>
service 객체
인터페이스
//이름으로 조회
List<UserVo> searchByName(String keyword);
//별명으로 조회
List<UserVo> searchByAlias(String keyword);
//아이디로 조회
List<UserVo> searchById(String keyword);
// 이름으로 갯수 조회
int countByName(String keyword);
// 별명으로 갯수 조회
int countByAlias (String keyword);
// 아이디으로 갯수 조회
int countById (String keyword);
클래스
//이름으로 조회
public List<UserVo> searchByName(String keyword){
return dao.searchByName(keyword)
}
//별명으로 조회
public List<UserVo> searchByAlias(String keyword){
return dao.searchByAlias(keyword)
}
//아이디로 조회
public List<UserVo> searchById(String keyword){
return dao.searchById(keyword)
}
// 이름으로 갯수 조회
int countByName(String keyword){
return dao.countByName(keyword)
}
// 별명으로 갯수 조회
int countByAlias (String keyword){
return dao.countByAlias(keyword)
}
// 아이디으로 갯수 조회
int countById (String keyword){
return dao.countById(keyword)
}
dao 객체
인터페이스
//이름으로 조회
List<UserVo> searchByName(String keyword);
//별명으로 조회
List<UserVo> searchByAlias(String keyword);
//아이디로 조회
List<UserVo> searchById(String keyword);
// 이름으로 갯수 조회
int countByName(String keyword);
// 별명으로 갯수 조회
int countByAlias (String keyword);
// 아이디으로 갯수 조회
int countById (String keyword);
//이름으로 조회
public List<UserVo> searchByName(String keyword){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
List<UserVo> userList = sqlSession.selectList("users2.searchByName" , keyword);
sqlSession.close();
return userList;
}
//별명으로 조회
public List<UserVo> searchByAlias(String keyword){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
List<UserVo> userList = sqlSession.selectList("users2.searchByAlias" , keyword);
sqlSession.close();
return userList;
}
//아이디로 조회
public List<UserVo> searchById(String keyword){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
List<UserVo> userList = sqlSession.selectList("users2.searchById" , keyword);
sqlSession.close();
return userList;
}
// 이름으로 갯수 조회
int countByName(String keyword){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
int cnt = sqlSession.selectOne("users2.countByName" , keyword)
sqlSession.close();
return cnt;
}
// 별명으로 갯수 조회
int countByAlias (String keyword){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
int cnt = sqlSession.selectOne("users2.countByAlias" , keyword)
sqlSession.close();
return cnt;
}
// 아이디으로 갯수 조회
int countById (String keyword){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
int cnt = sqlSession.selectOne("users2.countById" , keyword)
sqlSession.close();
return cnt;
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
UserServiceI userService = new UserService();
String keyword = "";
String title = "";
List<UserVo> listVo = new arrayList();
if(req.getParameter("title") != null && req.getParameter("keyword") != null) {
//아이디면 1 , 이름이면 2 , 별명3
keyword = req.getParameter("keyword");
title = req.getParameter("title");
아이디로 검색
if(title == 1 ){
int cnt = userService.countById(keyword);
if(cnt < 1 ) {
doGet(req , resp)
}
listVo = userService.searchById(keyword);
}else if (title == 2) {
int cnt = userService.countByName(keyword);
if(cnt < 1 ){
doGet(req , resp)
}
listVo = userService.searchByName(keyword);
}else {
int cnt = userService.countByAlias(keyword);
if(cnt < 1 ){
doGet(req , resp)
}
listVo = userService.searchByAlias(keyword)
}
}
req.setAttribute( "listVo", listVo );
//컨트롤러 1개 더 만들기( paging 처리도 해서 화면에 뿌려주기 )
req.getRequestDispatcher("${cp2}/ 컨트롤러이름ㅁ넣기").forward(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 다시 main.jsp 페이지로 돌아간다.
resp.sendRedirect(req.getContextPath() + "/pagepaging");
}
댓글
댓글 쓰기