我有一个名为UserDTO的域对象
这是我的mapper.xml文件配置
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id" parameterType="com.myapp.dto.UserDTO">
insert into
tblusers(username,password,email,phone,role,is_active,security_question,securtiy_answer,first_name,last_name)
values(#{username},#{password},#{email},#{phone},#{role,typeHandler=com.greenflight.typehandlers.StringArrayListTypeHandler},#{is_active},#{security_question},#{securtiy_answer},#{first_name},#{last_name})
</insert>
但这就像这样抛出异常
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column #1 from result set. Cause: org.postgresql.util.PSQLException: Bad value for type long : asdfds
### The error may involve org.mybatis.myapp.Mapper.insertUser-Inline
### The error occurred while setting parameters
### SQL: insert into tblusers(username,password,email,phone,role,is_active,security_question,securtiy_answer,first_name,last_name) values(?,?,?,?,?,?,?,?,?,?)
### Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column #1 from result set. Cause: org.postgresql.util.PSQLException: Bad value for type long : asdfds
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:200)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)
at com.greenflight.dao.UserDAO.CreateNewUser(UserDAO.java:98)
我查看了mybatis的文档,但未能找到关于如何将Domain对象作为参数发送的好例子。
任何人都可以向我展示解决方案
答案 0 :(得分:0)
你如何在UserDAO中的CreateNewUserDAO方法中实现。我想对于电话号码你给出错误的值(可能是设置错误的值)。
答案 1 :(得分:0)
我可以像这样更改映射器文件后插入记录
我改变了这个
require 'test_helper'
class ConcernedController < ApplicationController
include Concern
def action
render plain: "response", status: :ok
end
end
class ConcernTest < ActionDispatch::IntegrationTest
setup do
@controller = ConcernedController.new
Rails.application.routes.draw do
get "/action" => "concerned#action"
post "/action" => "concerned#action"
end
end
teardown do
Rails.application.reload_routes!
end
test "concern method" do
post "/action"
# Test what the concern does
assert_response :ok
assert_equal "Response", response.body
end
end
进入
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id" parameterType="com.greenflight.dto.UserDTO">
insert into
tblusers(username,password,email,phone,role,is_active,security_question,securtiy_answer,first_name,last_name)
values(#{username},#{password},#{email},#{phone},#{role,typeHandler=com.greenflight.typehandlers.StringArrayListTypeHandler},#{is_active},#{security_question},#{securtiy_answer},#{first_name},#{last_name})
</insert>
参数
<insert id="insertUser" parameterType="com.greenflight.dto.UserDTO">
insert into
tblusers(username,password,email,phone,role,is_active,security_question,securtiy_answer,first_name,last_name)
values(#{username},#{password},#{email},#{phone},#{role,typeHandler=com.greenflight.typehandlers.StringArrayListTypeHandler},#{is_active},#{security_question},#{securtiy_answer},#{first_name},#{last_name})
</insert>
适用于 HashMap 但不适用于POJO不知道原因