发表于: 2018-03-12 21:12:28

1 525


今天完成

1.解决昨天的问题

   (1)排查了依赖树中的报错;

   (2)解决前天遇到的问题

      a.spring的xml文件集成mybatis文件时遇到的问题:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
   <context:annotation-config />
   <!--<mvc:annotation-driven/>-->
   <context:component-scan base-package="com"/>
   <context:property-placeholder location="classpath:db.properties"/>
   <!--配置druid连接池-->
   <bean id="dataSource" class="${dataSource}" destroy-method="close" >
       <property name="username" value="${jdbc.username}"/>
       <property name="driverClassName" value="${jdbc.driver}"/>
       <property name="url" value="${jdbc.url}"/>
       <property name="password" value="${jdbc.password}"/>
   </bean>
   <!--以下是给mybatis的配置,数据库对话工厂,mapper映射-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 指定数据源 -->
       <property name="dataSource" ref="dataSource" />
       <!-- 指定需要别名的类所在的包名 ,或者在mybatis中配置-->
       <!--<property name="typeAliasesPackage" value="com.DAO"/>-->
       <!-- 扫描所有xml文件,或者在mybatis中配置 -->
       <property name="mapperLocations" value="classpath:com/DAO/*.xml"/>
   </bean>
   <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.DAO" />
   </bean>
</beans>


在上面代码中,对比前天的代码,在实质上只是更改了红色标记的一行的value,前天我直接指定value="xml的相对地址",然后一直报错找不到mapper.xml文件,改为上方的格式后,运行成功.原因:估计为格式或spring扫描原理的原因造成了该问题.需要学习源码;


 (2)初步实现了springMVC的增删改查,还需要加入RESTful,分页查询,重新jsp页面;

@Controller
@RequestMapping("/user")
public class SpringController {
   Student student;
   @Autowired
   StudentMapper studentMapper;
   String message;
   String messaged;
   @RequestMapping(value = "/listsall",method = RequestMethod.GET)
public String selectAll( Model model) throws IOException {
       List<Student> students= studentMapper.findAll();
       model.addAttribute("listsall", students);
       return "listsall";
   }
/*
    * 根据ID查询信息
    * */
   @RequestMapping(value = "/selectbyid",method = RequestMethod.GET)
public String select( int id,Model model) throws IOException {
student = studentMapper.findUserById(id);
       model.addAttribute("student", student);
       return "selectbyid";
   }
/*
    * 根据姓名模糊查询*/
   @RequestMapping(value = "/selectbyname")
public String selectByName(String name, HttpServletRequest model) throws IOException {
       List<Student> students = studentMapper.findUserByName(name);
       model.setAttribute("lists", students);
       return "selectbyname";
   }
@RequestMapping("/deletbyid")
public String delect(int id, Model model,HttpServletRequest httpServletRequest) throws Exception {
       System.out.println(httpServletRequest);
//       String vv =  httpServletRequest.getParameter("id");
       int a = studentMapper.deleteUser(id);
       if (a == 1) {
message = "数据删除成功";
       } else {
message = "数据删除失败";
       }
       model.addAttribute("message", message);
       return "deletbyid";
   }
//有问题
   @RequestMapping("/update")
public String update(Student student3,Model model) throws Exception {
           int s=studentMapper.updateUser(student3);
       System.out.println(student3);
           if (s == 1) {
               message = "数据修改成功";
           }
           else {
               message = "数据修改失败";
           }
           model.addAttribute("message",message);
           return "update";
   }

@RequestMapping("/add")
public String insert(Student student2) throws Exception {
studentMapper.insertUser(student2);
       return "update";
   }
}

上面蓝色代码中,用的是对象接收从URL中提取的信息,所以问题出在对应的JSP页面中,下方蓝色处.

<%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <title>Insert title here</title>
</head>
<body>
查询所有:<br>
<a href="/user/listsall">List All</a><br><br>

新增:<br><form action="user/add">
   NAME:<input type="text" name="name"><br>
   QQ:<input type="text" name="QQ"><br>
   <input type="submit" value="提交">
</form><br>

删除:<br><form action="/user/deletbyid">
   输入要删除的ID:<input type="text" name="id">
   <input type="submit" value="提交"><br>
</form><br>

查询:<br><form action="/user/selectbyid/">
   输入要查询的ID:<input type="text" name="id">
   <input type="submit" value="提交"><br>
</form><br>

查询:<br><form action="/user/selectbyname">
   输入要查询的姓名:<input type="text" name="name"><input type="submit" value="提交"><br>
</form><br>

修改:<br><form action="/user/update">

   输入需要修改信息的学员的ID:<input type="text" name="ID"><br>

   修改后的NAME:<input type="text" name="name"><br>
   修改后的QQ:<input type="text" name="QQ"><br>
   <input type="submit" value="提交">

</form><br>
</body>
</html>


以前如下:

 输入需要修改信息的学员的ID:<input type="text" name="id"><br>

造成了与对象中的属性名不一致的问题,所以接受不到id的值,所以无法修改.


明天计划

1.尝试加入RESTful风格.

2.学习jsp;

遇到问题

--------

收获

自己填坑未果,在师兄的帮助下填了2个坑.




返回列表 返回列表
评论

    分享到