在 Spring 和 MyBatis 中找不到语句参数 group_id 的 TypeHandler

There was no TypeHandler found for parameter group_id of statement in Spring and MyBatis

我正在使用 MyBatis 和 Spring,我有 2 个 table 是:User 和 GroupMaster table。我想通过 insert() 方法将数据插入数据库,如下所示:

用户class:

public class User {     
    private Long user_id;   
    private GroupMaster group_id;
    private String login_name;
    private String password;
    private String full_name;
    private String full_name_kana;
    private String email;
    private String tel;
    private Date birthday;
    // getter and setter
}

组长class:

public class GroupMaster {  
   private Long group_id;
   private String group_name;
   // getter and setter
 }

UserMapper.java

public interface UserMapper {
@Insert("INSERT INTO users(group_id, login_name, password, full_name,"
        + "full_name_kana, birthday, email, tel, rule, salt) VALUES"
        + "(#{group_id}, #{login_name},#{password}, #{full_name}, #{full_name_kana},"
        + "#{birthday}, #{email}, #{tel}, #{rule}, #{salt})")
@Options(useGeneratedKeys=true, keyProperty="id", flushCache=true, keyColumn="id")
public void insertUser(User user);
}
...

UserMapper.xml

<?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="net.project.springmybatis.mappers.UserMapper">
   <resultMap id="userMap" type="User">
    <id property="user_id" column="user_id"/>
    <result property="login_name" column="login_name"/>
    <result property="full_name" column="full_name"/>
    <result property="full_name_kana" column="full_name_kana"/>     
    <result property="birthday" column="birthday"/>
    <result property="email" column="email"/>
    <result property="tel" column="tel"/>

    <association property="group_id" javaType="GroupMaster">
        <id property="group_id" column="group_id"/>
        <result property="group_name" column="group_name"/>
    </association>

    <association property="detail_user_japan_id" javaType="DetailUserJapan">
        <id property="detail_user_japan_id" column="detail_user_japan_id"/>
        <result property="user_id" column="user_id"/>
        <result property="code_level" column="code_level"/>
        <result property="start_date" column="start_date"/>
        <result property="end_date" column="end_date"/>
        <result property="total" column="total"/>
    </association>


    <association property="code_level" javaType="JapanMaster">
        <id property="code_level" column="code_level"/>
        <result property="name_level" column="name_level"/>
    </association>


</resultMap>

add_user.jsp:

<tr>
    <td>group name</td>
    <td>                    
        <select name="groupSelected">
        <c:forEach items="${groupMasterList}" var="groupMaster">
            <option value="${groupMaster.group_id}">${groupMaster.group_name}</option>
        </c:forEach>
        </select>
     </td>
 </tr>

当我部署时,出现以下问题和异常:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: There was no TypeHandler found for parameter group_id of statement net.project.springmybatis.mappers.UserMapper.insertUser
org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
com.sun.proxy.$Proxy14.insert(Unknown Source)
org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:237)
org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:79)
org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
com.sun.proxy.$Proxy20.insertUser(Unknown Source)
net.luvina.springmybatis.service.UserServiceImpl.insertUser(UserServiceImpl.java:31)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)

在 JSP 文件中,我有一个要插入带有选择框的表单,当我提交它时出现问题。 如何解决问题

更新:

控制器class:

@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String submitAddNewUser(HttpServletRequest request, HttpServletResponse response,
        @RequestParam("groupSelected") Integer groupSelected,
        // @RequestParam("detailUserJapanSelected") Integer detailUserJapanSelected, 
        @RequestParam("japanMasterSelected") Integer japanMasterSelected, 

        @ModelAttribute("createUser") User createUser,  ModelMap model) throws SQLException, IOException {

    GroupMaster myGroupMaster = userService.getGroupById(groupSelected);
    // DetailUserJapan myDetailUserJapan = userService.getDetailUserJapanById(detailUserJapanSelected);
    JapanMaster myJapanMaster = userService.getJapanMasterById(japanMasterSelected);
    // tao lan luot cho cac ham con lai

    createUser.setGroup_id(myGroupMaster);
    // createUser.setDetail_user_japan_id(myDetailUserJapan);
    createUser.setCode_level(myJapanMaster);

    // model.addAttribute("user", user);
    userService.insertUser(createUser);

    return "redirect:/";
}

add_user.jsp

<tr>
    <td>資格: (name level | mst_japan)</td>
    <td>                     
        <select name="japanMasterSelected">
            <c:forEach items="${japanMasterList}" var="japanMaster">
                <option value="${japanMaster.code_level}">${japanMaster.name_level}</option>
                        </c:forEach>
                    </select>                           
                </td>
            </tr>

当我在 mst_japan 中使用 code_level 添加更多内容时,会出现错误 404,如下所示: 错误 404:

HTTP Status 400 – Bad Request
Type Status Report

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

group_id的类型是GroupMasteruser.group_id列的数据类型是INT
报错说MyBatis不知道如何将GroupMaster转换成INT.

要解决错误,只需将 #{group_id} 更改为 #{group_id.group_id}

@Insert("INSERT INTO users(group_id, login_name, password, full_name,"
        + "full_name_kana, birthday, email, tel, rule, salt) VALUES"
        + "(#{group_id.group_id}, #{login_name},#{password}, #{full_name}, #{full_name_kana},"
        + "#{birthday}, #{email}, #{tel}, #{rule}, #{salt})")

你好像对 MyBatis 很陌生,建议你花点时间看看文档。
https://mybatis.org/mybatis-3/index.html

我还推荐使用更新版本的 MyBatis。
3.4.6 支持 Java 7 如果你不能升级到 Java 8+.