简体   繁体   English

Spring和MyBatis中statement的参数group_id没有找到TypeHandler

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

I'm using MyBatis and Spring, I have 2 tables are: User and GroupMaster table.我正在使用 MyBatis 和 Spring,我有 2 个表是:User 和 GroupMaster 表。 I want to insert data into database by method insert() following as:我想通过方法 insert() 将数据插入数据库,如下所示:

User class:用户 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
}

GroupMaster class: GroupMaster class:

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

UserMapper.java 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 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: 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>

When I deploy, it happen the problem and exception following as:当我部署时,会发生以下问题和异常:

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)

in JSP file, I have a form to insert with a selectbox, when I submit it happen the problem.在 JSP 文件中,我有一个用选择框插入的表单,当我提交它时会出现问题。 How to fix the problem如何解决问题

UPDATE:更新:

Controller class: Controller 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 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>

When I add more with: code_level in mst_japan, it happen an error 404 following as: Error 404:当我在 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).

The type of group_id is GroupMaster and the data type of user.group_id column is INT . GroupMaster group_id user.group_id列的数据类型是INT
The error says that MyBatis does not know how to convert GroupMaster into INT .该错误表明 MyBatis 不知道如何将GroupMaster转换为INT

To resolve the error, you just need to change #{group_id} to #{group_id.group_id} .要解决此错误,您只需将#{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})")

You seem to be very new to MyBatis, so I suggest you to take some time to read the documentation.您似乎对 MyBatis 很陌生,所以我建议您花一些时间阅读文档。
https://mybatis.org/mybatis-3/index.html https://mybatis.org/mybatis-3/index.html

I also recommend using more recent version of MyBatis.我还建议使用更新版本的 MyBatis。
3.4.6 supports Java 7 if you cannot upgrade to Java 8+.如果不能升级到 Java 8+,3.4.6 支持 Java 7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM