dao:
public List<Favorite> findRidByUid(int uid) {
String sql="select * from tab_favorite where uid=?";
return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Favorite>(Favorite.class),uid);
}
service:
public List<Favorite> findFavorite(int uid) {
List<Favorite> favorites = dao.findRidByUid(uid);
for (Favorite favorite : favorites) {
favorite.setRoute(routeDao.findOne(favorite.getRid()));
}
return favorites;
}
servlet:
public void findFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取uid
User user = (User) request.getSession().getAttribute("user");
//调用service方法
List<Favorite> favorite = favoriteService.findFavorite(user.getUid());
//序列化json写回客户端
writeValue(favorite,request,response);
}
html:
<script>
$(function () {
$.get("routeServlet/findFavorite",{},function (data) {
var divs="";
for (var i = 0; i < data.length; i++) {
var route=data[i];
var div='<div class="col-md-3">\n' +
' <a href="route_detail.html?rid='+route.rid+'">\n' +
' <img src="'+route.route.rimage+'" alt="">\n' +
' <div class="has_border">\n' +
' <h3>'+route.route.rname+'</h3>\n' +
' <div class="price">网付价<em>¥</em><strong>'+route.route.price+'</strong><em>起</em></div>\n' +
' </div>\n' +
' </a>\n' +
' </div>';
divs+=div;
}
$("#favorite").html(divs);
})
})
</script>