ssj의 프레임워크 통합 실례

14959 단어
Spring MVC + Spring + Spring JDBC 통합 예: 우선 dao 레이어 설정: package com.offcn.dao;
import com.offcn.entity.User; import com.offcn.utils.PageUtils;
import java.util.List;
public interface UserDao {
User selectByName(String userName);

//    -        ,             
List selectAllList(Integer currentPageNo,Integer pageSize);

/**
 *
 * @return
 */
//      
Integer selectUserCount();


/**
 *
 * @param user
 * @return
 */
//     
Integer addUser(User user);

//  

/**
 *
 * @param id
 * @return
 */
Integer deleteUser(int id);
//  

/**
 *
 * @param user
 * @return
 */

Integer updateUser(User user);
//      
User selectOneUser(int id);


/*----    -----*/
 List  selectLikeUser(PageUtils pageUtils);
 //        
Integer selectLikeCount(PageUtils pageUtils);

}
UserDaoImpl 구현 레이어: package com.offcn.dao.impl;
import com.offcn.dao.UserDao; import com.offcn.entity.User; import com.offcn.handle.ArrayResultHandler; import com.offcn.handle.BeanListResultHandler; import com.offcn.handle.BeanResultHandler; import com.offcn.utils.BaseDao; import com.offcn.utils.EmptyUtils; import com.offcn.utils.PageUtils; import org.springframework.stereotype.Repository;
import java.util.ArrayList; import java.util.List;
@Repository(“userDao”) public class UserDaoImpl implements UserDao {
//    
@Override
public User selectByName(String userName) {

    String sql = "select * from user where uname=?";

    return (User) BaseDao.executeQuery(sql,new Object[]{userName}, new BeanResultHandler(User.class));
}



//    
@Override
public List selectAllList(Integer currentPageNo, Integer pageSize) {

    String sql="select * from user limit ?,?";
    Object []objects={(currentPageNo-1)*pageSize,pageSize};

    return (List) BaseDao.executeQuery(sql,objects, new BeanListResultHandler(User.class));
}

@Override
//      
public Integer selectUserCount() {
    String sql="select count(1) from user";


    List list =(List) BaseDao.executeQuery(sql, null, new ArrayResultHandler());
  String count= list.get(0)+"";

    return Integer.parseInt(count);
}

@Override
public Integer addUser(User user) {

    String sql="insert into  user(uname,upwd,usex) values(?,?,?)";
    return  BaseDao.executeUpdate(sql,new Object[]{user.getUname(),user.getUpwd(),user.getUsex()});
}

@Override
public Integer deleteUser(int id) {
    String sql="delete from user where uid=?";
    return BaseDao.executeUpdate(sql,new Object[]{id});
}

@Override
public Integer updateUser(User user) {
    String sql="update  user set uname=?,upwd=?,usex=?   where uid=?";
    return BaseDao.executeUpdate(sql,new Object[]{user.getUname(),user.getUpwd(),user.getUsex(),user.getUid()});
}

@Override
public User selectOneUser(int id) {
    String sql="select * from user where uid=?";

    User user = (User) BaseDao.executeQuery(sql, new Object[]{id}, new BeanResultHandler(User.class));
    return user;
}


/*--      --*/
@Override
public List selectLikeUser( PageUtils pageUtils) {
    //String sql="select * from user where 1=1";

    StringBuffer sb=new StringBuffer("select * from user where 1=1");

    //              
    List list=new ArrayList();

    //          
    if(EmptyUtils.isNotEmpty(pageUtils.getQueryWorld())){
        //  sql  
        sb.append(" and uname like ?");
        list.add("%"+pageUtils.getQueryWorld()+"%");
        //


    }
    //    
    if(EmptyUtils.isNotEmpty(pageUtils.getCurrentPageNo())){

        sb.append( " limit ?,?");
        list.add((pageUtils.getCurrentPageNo()-1)*pageUtils.getPageSize());
        list.add(pageUtils.getPageSize());
    }

    Listlist1 = (List) BaseDao.executeQuery(sb.toString(), list.toArray(), new BeanListResultHandler(User.class));


    return list1;
}

@Override
public Integer selectLikeCount(PageUtils pageUtils) {
    StringBuffer sb=new StringBuffer("select count(1) from user where 1=1");

    //              
    List list=new ArrayList();

    //          
    if(EmptyUtils.isNotEmpty(pageUtils.getQueryWorld())){
        //  sql  
        sb.append(" and uname like ?");
        list.add("%"+pageUtils.getQueryWorld()+"%");
        //

    }

    List list1 = (List) BaseDao.executeQuery(sb.toString(), list.toArray(), new ArrayResultHandler());

    String s= list1.get(0)+"";
    return Integer.parseInt(s);
}

}
UserServiceImpl 구현 사업부: package com.offcn.service.impl;
import com.offcn.dao.UserDao; import com.offcn.entity.User; import com.offcn.service.UserService; import com.offcn.utils.PageUtils; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import java.util.List;
/- 주석이 서비스 레이어를 검색하고 별칭을 지정합니다("userService") public class UserService Impl implements UserService
@Resource/*  userDao*/
private UserDao userDao;
@Override

public User selectByName(String uName, String upwd) {

    User user=userDao.selectByName(uName);


        if (user.getUpwd().equals(upwd)) {
            return user;
        }

    return null;
}

@Override
public List selectAllList(Integer currentPageNo, Integer pageSize) {
    return userDao.selectAllList(currentPageNo,pageSize);
}


@Override
public Integer selectUserCount() {
    return userDao.selectUserCount();
}



@Override
public Integer addUser(User user) {
    return userDao.addUser(user);
}

@Override
public Integer updateUser(User user) {
    return userDao.updateUser(user);
}

@Override
public User selectOneUser(int id) {
    return userDao.selectOneUser(id);
}

@Override
public Integer deleteUser(int id) {
    return userDao.deleteUser(id);
}

@Override
public List selectLikeUser(PageUtils pageUtils) {
    return userDao.selectLikeUser(pageUtils);
}

@Override
public Integer selectLikeCount(PageUtils pageUtils) {
    return userDao.selectLikeCount(pageUtils);
}

}
springmvc-servlet.xml 구성:
mvc:annotation-driven/



	
	

**applicationContext-jdbc.xml:구성**
인터페이스를 수정합니다.jsp Created by IntelliJ IDEA. User: ibm Date: 2018/10/18 Time: 9:49 To change this template use File | Settings | File Templates. –%> Title
인터페이스 수정
사용자 이름:
암호:
성별:
첫 페이지.jsp 파일:Title
증대

    
                   
                   
${user.uid} ${user.uname} ${user.upwd} 수정 삭제
첫 페이지 이전 페이지 [${pageUtils.currentPageNo}/${pageUtils.totalPageSize}] 다음 페이지 끝 페이지
모호한 조회: **User Controller 제어층의 클래스 ** package com.offcn.controller;
import com.offcn.entity.User; import com.offcn.service.UserService; import com.offcn.utils.EmptyUtils; import com.offcn.utils.PageUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List;
@Controller @RequestMapping("/userController") public class UserController {
@Resource
private UserService userService;


/*----    --*/
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String loginIn(@RequestParam(value = "uname",required = false)String uname,
                      @RequestParam(value = "upwd",required = false)String upwd, Model model){

    System.out.println();
    User user = userService.selectByName(uname, upwd);

    if(user!=null){
        model.addAttribute("user",user);
        /*---       ---*/
        //    :forward
        return "redirect:toPage";
    }

    return "error";
}
/*    */
@RequestMapping("toPage")
public String toPage(@RequestParam(value = "currentPageNo",required = false) String currentPageNo
                    ,@RequestParam(value ="pageSize" ,required = false) String pageSize,
                     @RequestParam(value = "muname",required = false)String queryWord,
                     Model model){



        if (currentPageNo == "" || currentPageNo == null) {

            currentPageNo = "1";
        }

        if (pageSize == null || pageSize == "") {
            pageSize = "5";
        }

    PageUtils pageUtils=new PageUtils();

    pageUtils.setCurrentPageNo(Integer.parseInt(currentPageNo));

    pageUtils.setPageSize(Integer.parseInt(pageSize));

    if(EmptyUtils.isNotEmpty(queryWord)){

        pageUtils.setQueryWorld(queryWord);}





    // pageUtils      
    //userService.selectAllList(Integer.parseInt(currentPageNo),Integer.parseInt(pageSize));
    //         

        List list = userService.selectLikeUser(pageUtils);

        //      
        Integer countPage = userService.selectLikeCount(pageUtils);
        //   
        Integer totalPageSize = countPage % Integer.parseInt(pageSize) == 0 ?
                countPage / Integer.parseInt(pageSize) : countPage / Integer.parseInt(pageSize) + 1;


    //  
    pageUtils.setTotalCount(countPage);
    pageUtils.setTotalPageSize(totalPageSize);
    //pageUtils.setCurrentPageNo(Integer.parseInt(currentPageNo));
    pageUtils.setUserList(list);
    //pageUtils.setPageSize(Integer.parseInt(pageSize));

    model.addAttribute("pageUtils", pageUtils);


    return "showInfo";
}

/*     */
@RequestMapping("/toAdd")
public String toAdd(){

return "addUser";
}

/*       */
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String add(@RequestParam(value = "uname",required = false) String uname,
                  @RequestParam(value = "upwd",required = false)String upwd,
                  @RequestParam(value = "usex",required = true)String usex){

    User user=new User();
    user.setUname(uname);
    user.setUpwd(upwd);
    user.setUsex(Integer.parseInt(usex));

    Integer integer = userService.addUser(user);
    if(integer>0){

        return "redirect:toPage";
    }
    return "toAdd";
}

/*     */
@RequestMapping(value = "/toUpdate")
public String toUpdate(@RequestParam(value = "uid",required = true) String uid,Model model){

    User user = userService.selectOneUser(Integer.parseInt(uid));
    model.addAttribute("user",user);

    return "updateUser";
}

/*       */
@RequestMapping(value = "/update",method = RequestMethod.POST)
public String update(@RequestParam(value = "uid",required = true) String uid,
                     @RequestParam(value = "uname",required = true) String uname,
                     @RequestParam(value = "upwd",required = true) String upwd,
                     @RequestParam(value = "usex",required = true) String usex){


    User user=new User();
    user.setUsex(Integer.parseInt(usex));
    user.setUid(Integer.parseInt(uid));
    user.setUpwd(upwd);
    user.setUname(uname);

    Integer integer = userService.updateUser(user);
    if(integer>0){
        return "redirect:toPage";
    }

    return "toUpdate";

}
/*    */
@RequestMapping("deleteUser")
@ResponseBody
public void deleteUser(@RequestParam(value = "uid",required = true) String uid,
                       HttpServletResponse response){

    Integer integer = userService.deleteUser(Integer.parseInt(uid));

}

}
PageUtils 도구 클래스:package com.offcn.utils;
import com.offcn.entity.User;
import java.util.List;
//페이지별 도구 클래스: public class PageUtils {//현재 페이지 번호 private Integer currentPageNo;//페이지당 private Integer pageSize 몇 개 보이는지;//총 레코드 수 private Integer total Count;//총 페이지 수 private Integer total PageSize;//조회된 집합 private List user List;//모호한 조회의 일치 필드 private String String World;
public String getQueryWorld() {
    return queryWorld;
}

public void setQueryWorld(String queryWorld) {
    this.queryWorld = queryWorld;
}

public Integer getCurrentPageNo() {
    return currentPageNo;
}

public void setCurrentPageNo(Integer currentPageNo) {
    this.currentPageNo = currentPageNo;
}

public Integer getPageSize() {
    return pageSize;
}

public void setPageSize(Integer pageSize) {
    this.pageSize = pageSize;
}

public Integer getTotalCount() {
    return totalCount;
}

public void setTotalCount(Integer totalCount) {
    this.totalCount = totalCount;
}

public Integer getTotalPageSize() {
    return totalPageSize;
}

public void setTotalPageSize(Integer totalPageSize) {
    this.totalPageSize = totalPageSize;
}

public List getUserList() {
    return userList;
}

public void setUserList(List userList) {
    this.userList = userList;
}

} 사용자에 대한 삭제 및 수정을 실현합니다. (모호한 검색과 페이지 나누기 포함)

좋은 웹페이지 즐겨찾기