使用 spring 和 mybatis 增删改查

CRUD using spring and mybatis

HI 在浏览器中创建 CRUD 操作时出现问题

`HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: org.apache.jasper.JasperException: org.springframework.beans.NotReadablePropertyException: Invalid property 'standard' of bean class [com.raistudies.domain.User]: Bean property 'standard' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:548)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这里我有springmvc + mybatis操作文件的全部部分 //控制器

package com.raistudies.controllers;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.raistudies.domain.User;
import com.raistudies.persistence.UserService;
import com.raistudies.validator.RegistrationValidator;

@Controller
@RequestMapping(value="/registration")
public class RegistrationController {

private RegistrationValidator validator = null;
private UserService userService = null;

@Autowired
public void setUserService(UserService userService) {
    this.userService = userService;
}

public RegistrationValidator getValidator() {
    return validator;
}

@Autowired
public void setValidator(RegistrationValidator validator) {
    this.validator = validator;
}

@RequestMapping(method=RequestMethod.GET)
public String showForm(ModelMap model){
    List<User> users = userService.getAllUser();
    model.addAttribute("users", users);
    User user = new User();
    user.setId(UUID.randomUUID().toString());
    model.addAttribute("user", user);
    return "registration";
}

@RequestMapping(value="/add", method=RequestMethod.POST)
public ModelAndView add(@ModelAttribute(value="user") User user,BindingResult result){
    validator.validate(user, result);
    ModelAndView mv = new ModelAndView("registration");
    if(!result.hasErrors()){
        userService.saveUser(user);
        user = new User();
        user.setId(UUID.randomUUID().toString());
        mv.addObject("user", user);
    }
    mv.addObject("users", userService.getAllUser());
    return mv;
}

@RequestMapping(value="/update", method=RequestMethod.POST)
public ModelAndView update(@ModelAttribute(value="user") User user,BindingResult result){
    validator.validate(user, result);
    ModelAndView mv = new ModelAndView("registration");
    if(!result.hasErrors()){
        userService.updateUser(user);
        user = new User();
        user.setId(UUID.randomUUID().toString());
        mv.addObject("user", user);
    }
    mv.addObject("users", userService.getAllUser());
    return mv;
}

@RequestMapping(value="/delete", method=RequestMethod.POST)
public ModelAndView delete(@ModelAttribute(value="user") User user,BindingResult result){
    validator.validate(user, result);
    ModelAndView mv = new ModelAndView("registration");
    if(!result.hasErrors()){
        userService.deleteUser(user.getId());
        user = new User();
        user.setId(UUID.randomUUID().toString());
        mv.addObject("user", user);
    }
    mv.addObject("users", userService.getAllUser());
    return mv;
}
 }

坚持就是

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.raistudies.persistence.UserService">

<resultMap id="result" type="user">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="studentClass" column="class"/>
    <result property="roll" column="roll"/>
   <!--  <result property="sex" column="sex"/>-->
</resultMap>

<select id="getAllUser" resultMap="result">  
    SELECT id,name,class,roll
    FROM user; 
</select>

<insert id="saveUser" parameterType="user">
    INSERT INTO user (id,name,standard,age,sex) 
    VALUE (#{id},#{name},#{standard},#{age})
</insert>

<update id="updateUser" parameterType="user"> 
    UPDATE user
    SET
        name = #{name},  
        standard = #{standard},  
        age = #{age},  
        sex = #{sex} 
    where id = #{id} 
</update>

  <delete id="deleteUser" parameterType="int">
    DELETE FROM user
    WHERE id = #{id}
  </delete>
  </mapper>

我必须执行创建删除更新添加操作但是它会像上面一样出错而运行我的项目......请帮助我

根据变量更改 jsp 代码后 java bean class(class 包含 geter 或 setter 方法)has.Then 它会解决问题。

*特别是我的意思是你必须根据 bean 变量

更改 jsp <tr><td>Class : </td><td><form:input path="standard" /></td></tr>* 中的路径

上一个 jsp 文件(错误时)

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page session="true" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Hello World with Spring 3 MVC</title>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
    <script type="text/javascript" src='<c:url value="/resources/common.js"/>'></script>
    <script type="text/javascript" src='<c:url value="/resources/registration.js"/>'></script>
    <script type="text/javascript">
        var projectUrl = '<c:url value="/"/>';
        if(projectUrl.indexOf(";", 0) != -1){
            projectUrl = projectUrl.substring(0, projectUrl.indexOf(";", 0));
        }
    </script>       
</head>
<body>
    <fieldset>
    <legend>Registration Form</legend>
    <center>
    <form:form commandName="user" action="/SpringMVCMyBatisCRUDExample/app/registration/add" name="userForm">

    <form:hidden path="id"/>
    <table>
        <tr><td colspan="2" align="left"><form:errors path="*" cssStyle="color : red;"/></td></tr>
        <tr><td>Name : </td><td><form:input path="name" /></td></tr>
        <tr><td>Class : </td><td><form:input path="standard" /></td></tr>
        <tr><td>RollNo: </td><td><form:input path="roll" /></td></tr>

        <tr><td colspan="2"><input type="submit" value="Save Changes"/>
        &nbsp;<input type="reset" name="newUser" value="New User" onclick="setAddForm();" disabled="disabled"/>
        &nbsp;<input type="submit" name="deleteUser" value="Delete User" onclick="setDeleteForm();" disabled="disabled"/></td></tr>
    </table>
    </form:form>
    </center>
    </fieldset>
    <c:if test="${!empty users}">

        <br />
        <center>
            <table width="90%">
                <tr style="background-color: gray;">
                    <th>Name</th>
                    <th>Class</th>
                    <th>RollNo</th>

                </tr>
                <c:forEach items="${users}" var="user">
                    <tr style="background-color: silver;" id="${user.id}" onclick="setUpdateForm('${user.id}');">
                        <td><c:out value="${user.name}"/></td>
                        <td><c:out value="${user.studentClass}"/></td>
                        <td><c:out value="${user.roll}"/></td>

                    </tr>
                </c:forEach>
            </table>
            </center>
        <br />

    </c:if>
</body>

我的java豆子class

package com.raistudies.domain;

import java.io.Serializable;


public class User implements Serializable{

private static final long serialVersionUID = 3647233284813657927L;

private String id;
private String name = null;
private String studentClass = null;
private String roll;    

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getStudentClass() {
    return studentClass;
}

public void setStudentClass(String studentClass) {
    this.studentClass = studentClass;
}

public String getRoll() {
    return roll;
}

public void setRoll(String roll) {
    this.roll = roll;
}



@Override
public String toString() {
    return "User [name=" + name + ", class="+ studentClass+ ", roll=" + roll + "]";
}
}

之前我在 java bean class(model class) 中有 standard 变量但我必须将其更改为 studentClass 为了避免出错,我们必须更改 jsp 路径即

<tr><td>Class : </td><td><form:input path="standard" /></td></tr>.......

<tr><td>Class : </td><td><form:input path="studentClass" /></td></tr>