发表于: 2017-10-05 20:15:39

1 776


最近登陆tomcat总是报

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
### The error may exist in com/how2java/mapper/CategoryMapper.java (best guess)
### The error may involve com.how2java.mapper.CategoryMapper.total
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)

实际原因是因为我这边网络太差,总是连接不到服务器数据库的缘故


今天完成的任务

完成了增删改查操作

// 告诉spring mvc这是一个控制器类
@Controller
@RequestMapping("")
public class CategoryController {
@Autowired
   CategoryService categoryService;

   @RequestMapping("listCategory")//这个分页查询就是调用的sql关于某段的某几个数列的查询
   public ModelAndView listCategory(Page page){

ModelAndView mav = new ModelAndView();//构造方法
       List<Category> cs= categoryService.list(page);//怕不是自动装配的
       //int total = categoryService.total();//得到数据库数据总量
      // page.caculateLast(total);//调用caculateLast方法,但是没卵用啊,这句话 根本就没被返回
       // 放入转发参数
       mav.addObject("cs", cs);
       // 放入jsp路径
       mav.setViewName("listCategory");
       return mav;
   }

@RequestMapping("addCategory")//增加
   public ModelAndView addCategory(Category category){
categoryService.add(category);
       ModelAndView mav = new ModelAndView("redirect:/listCategory");
       return mav;
   }

@RequestMapping("deleteCategory")//删除
   public ModelAndView deleteCategory(Category category){
categoryService.delete(category);
       ModelAndView mav = new ModelAndView("redirect:/listCategory");
       return mav;
   }

@RequestMapping("editCategory")//查询id,有点特殊
   public ModelAndView editCategory(Category category){
Category c= categoryService.get(category.getId());
       ModelAndView mav = new ModelAndView("editCategory");
       mav.addObject("c", c);
       return mav;
   }

@RequestMapping("updateCategory")//改
   public ModelAndView updateCategory(Category category){
categoryService.update(category);
       ModelAndView mav = new ModelAndView("redirect:/listCategory");
       return mav;
   }

}
 ModelAndView mav = new ModelAndView("redirect:/listCategory");

保证都能映射在地址为listCategory上

其实很简单

就是

jsp-程序-数据库的交互


用户在网页上输入内容-通过java调用相关的sql语句-sql返回信息-打印在网页上


关于分页查询所使用的sql语句

<select id="list" resultType="Category">
   select * from category_
<if test="start!=null and count!=null">
       limit #{start},#{count}
</if>

</select>


实际上为select * from   category_ limit #{start},#{count}




即查询category_表中自start行开始共计count行的数据



分页查询就是通过用户的操作改变start的数值返回数据并打印出来

修改和删除都是通过获得的id,通过sql语句进行操作,本质上讲和任务一的对数据库进行增删改查几乎一样


因为修改和增加涉及到了jsp对用户输入的一个接受,对这部分我理解还不深刻,仅在下面附录

<%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8" import="java.util.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<div style="width:500px;margin:0px auto;text-align:center">
   <table align='center' border='1' cellspacing='0'>
       <tr>
           <td>id</td>
           <td>name</td>
           <td>编辑</td>
           <td>删除</td>
       </tr>
       <c:forEach items="${cs}" var="c" varStatus="st">
<tr>
               <td>${c.id}</td>
               <td>${c.name}</td>
               <td><a href="editCategory?id=${c.id}">编辑</a></td>
               <td><a href="deleteCategory?id=${c.id}">删除</a></td>
           </tr>
       </c:forEach>
</table>
   <div style="text-align:center">
       <a href="?start=0">首 页</a>
       <a href="?start=${page.start-page.count}">上一页</a>
       <a href="?start=${page.start+page.count}">下一页</a>
       <a href="?start=${page.last}">末 页</a>
   </div>


   <div style="text-align:center;margin-top:40px">

       <form method="post" action="addCategory">
           分类名称: <input name="name" value="" type="text"> <br><br>
           <input type="submit" value="增加分类">
       </form>

   </div>
</div>


<%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8" import="java.util.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<div style="width:500px;margin:0px auto;text-align:center">


   <div style="text-align:center;margin-top:40px">

       <form method="post" action="updateCategory">
           分类名称: <input name="name" value="${c.name}" type="text"> <br><br>
           <input type="hidden" value="${c.id}" name="id">
           <input type="submit" value="增加分类">
       </form>

   </div>
</div>


明天要做的事

学习jsp做出一个符合修真院特色的网页格式

观看师兄的日报,找出自己对REST风格的接口理解是否有问题




遇到的问题



收获


返回列表 返回列表
评论

    分享到