Mybatis学习十 (SqlSessionDaoSupport 实现)

示例步骤:

   第一步:创建UserDAO   

import java.util.List;

import com.yiibai.pojo.Order;

public interface UserDAO {
    public List<Order> getUserOrders(int userId);
}

 

   第二步:创建UserDaoImpl

     

import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.yiibai.pojo.Order;

@Repository
public class UserDAOImpl extends SqlSessionDaoSupport implements UserDAO {
    public List<Order> getUserOrders(int userId) {
        // TODO Auto-generated method stub
        return this.getSqlSession().selectList("com.yiibai.inter.IUser.getUserOrders",userId);
    }
}

 

   第三步:创建UserController

     

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yiibai.dao.UserDAO;
import com.yiibai.pojo.Order;
import com.yiibai.util.Page;

// http://localhost:8080/mybatis08-paging/user/orders
@Controller
@RequestMapping("/user")
public class UserController {
    //UserMaper userMaper;
    
    @Autowired
    UserDAO userDao;
    
    /**
     * 某一个用户下的所有订单(Dao方式)
     * URL => http://localhost:8080/mybatis11/user/lists
     * 
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/lists")
    public ModelAndView listalldao(HttpServletRequest request,
            HttpServletResponse response) {
        List<Order> orders = this.userDao.getUserOrders(1);
        // 制定视图 =>list.jsp
        ModelAndView mav = new ModelAndView("lists");
        mav.addObject("orders", orders);
        return mav;
    }

}

第四步:测试

 

 

学习来源:https://www.yiibai.com/mybatis/mybatis_sqlsessiondaosupport.html

posted @ 2020-09-16 12:06  小窝蜗  阅读(701)  评论(0编辑  收藏  举报