[英]Saving user info into MySQL using MyBatis
I have problem when saving user info into mysql by using mybatis in my java project.在我的 java 项目中使用 mybatis 将用户信息保存到 mysql 时遇到问题。 The requirement is below:要求如下:
If user's info is not exist in database, then insert this user.如果数据库中不存在用户的信息,则插入该用户。
If user's info exists in database, then update this user.如果数据库中存在用户的信息,则更新此用户。
Now I have checked the document of mybatis, the dynamic sql defination may save my time, but I didn't find example on it.现在查了mybatis的文档,动态sql定义可以节省我的时间,但是我没有找到例子。 So below is the sample sql I want to change:所以下面是我要更改的示例 sql:
<insert id="create" parameterType="UserLearningCourse" >
<!--If record not exists, perform insert command-->
INSERT INTO `user_learning_course` (
user_identifier,
course_identifier,
best_grade,
latest_grade,
total_number_of_sequences,
last_modified_at
)
VALUES (
#{user_identifier},
#{course_identifier},
#{best_grade},
#{latest_grade},
#{total_number_of_sequences},
#{last_modified_at}
)
<!--If record exists, perform update command-->
UPDATE user_learning_course
<set>
<if test="best_grade!=null">
best_grade = #{best_grade},
</if>
<if test="latest_grade!=null">
latest_grade = #{latest_grade},
</if>
<if test="total_number_of_sequences!=null">
total_number_of_sequences = #{total_number_of_sequences},
</if>
<if test="last_modified_at!=null">
last_modified_at = #{last_modified_at},
</if>
</set>
WHERE user_identifier = #{user_identifier}
</insert>
Anyone who did this, would you pls give me some lights?谁做过这个,你能给我一些灯吗? thx.谢谢。
EDIT: Now I can insert or update database based on the filter now.编辑:现在我可以根据过滤器插入或更新数据库了。 But the problem is that I can't get the return result.但问题是我无法得到返回结果。 Would anyone help?有人会帮忙吗? the xml sql below:下面的xml sql:
<insert id="saveOrUpdate" parameterType="UserLearningCourse" useGeneratedKeys="true"
keyProperty="id" keyColumn="id">
<selectKey keyProperty="count" resultType="int" order="BEFORE">
SELECT COUNT(*) count FROM user_learning_course
where user_identifier = #{userIdentifier}
AND course_identifier = #{courseIdentifier}
</selectKey>
<if test="count > 0">
UPDATE user_learning_course
<set>
<if test="courseIdentifier!=null">
course_identifier = #{courseIdentifier},
</if>
<if test="bestCourseGrade!=null">
best_grade = #{bestCourseGrade},
</if>
<if test="latestCourseGrade!=null">
latest_grade = #{latestCourseGrade},
</if>
<if test="totalNumberOfSequences!=null">
total_number_of_sequences = #{totalNumberOfSequences},
</if>
<if test="lastModifiedAt!=null">
last_modified_at = #{lastModifiedAt},
</if>
</set>
<where>
user_identifier = #{userIdentifier}
AND course_identifier = #{courseIdentifier}
</where>
</if>
<if test="count==0">
INSERT INTO `user_learning_course` (
user_identifier,
course_identifier,
best_grade,
latest_grade,
total_number_of_sequences,
last_modified_at
)
VALUES (
#{userIdentifier},
#{courseIdentifier},
#{bestCourseGrade},
#{latestCourseGrade},
#{totalNumberOfSequences},
#{lastModifiedAt}
)
</if>
</insert>
But after I ran it, it will throw me exception as :但是在我运行它之后,它会抛出异常:
{ "type": "genericResponseWrapper", "httpStatusCode": 500, "applicationErrorCode": 0, "errorMessage": "Unexpected Throwable : Mapper method 'com.rosettastone.ws.ptsws.dao.mybatis.UserLearningCourseDaoImpl.saveOrUpdate' has an unsupported return type: class com.rosettastone.ws.ptsws.domain.UserLearningCourse", "errorDetails": null, "payload": null } { "type": "genericResponseWrapper", "httpStatusCode": 500, "applicationErrorCode": 0, "errorMessage": "Unexpected Throwable : Mapper method 'com.rosettastone.ws.ptsws.dao.mybatis.UserLearningCourseDaoImpl.saveOrUpdate' 有一个不支持的返回类型:class com.rosettastone.ws.ptsws.domain.UserLearningCourse”,“errorDetails”:null,“payload”:null }
The method I defined in my code is :我在代码中定义的方法是:
UserLearningCourse saveOrUpdate(UserLearningCourse entity);
I intend to return entity, but error throws.我打算返回实体,但错误抛出。 I know the xml sql config is not with return type.我知道 xml sql 配置没有返回类型。 but how to resolve?但如何解决?
Finally, I got this solved.最后,我解决了这个问题。 First, the sql below:首先,下面的sql:
<insert id="saveOrUpdate" parameterType="UserLearningCourse" useGeneratedKeys="true"
keyProperty="id" keyColumn="id">
<selectKey keyProperty="count" resultType="int" order="BEFORE">
SELECT COUNT(*) count FROM user_learning_course
where user_identifier = #{userIdentifier}
AND course_identifier = #{courseIdentifier}
</selectKey>
<if test="count > 0">
UPDATE user_learning_course
<set>
<if test="courseIdentifier!=null">
course_identifier = #{courseIdentifier},
</if>
<if test="bestCourseGrade!=null">
best_grade = #{bestCourseGrade},
</if>
<if test="latestCourseGrade!=null">
latest_grade = #{latestCourseGrade},
</if>
<if test="totalNumberOfSequences!=null">
total_number_of_sequences = #{totalNumberOfSequences},
</if>
<if test="lastModifiedAt!=null">
last_modified_at = #{lastModifiedAt},
</if>
</set>
<where>
user_identifier = #{userIdentifier}
AND course_identifier = #{courseIdentifier}
</where>
</if>
<if test="count==0">
INSERT INTO `user_learning_course` (
user_identifier,
course_identifier,
best_grade,
latest_grade,
total_number_of_sequences,
last_modified_at
)
VALUES (
#{userIdentifier},
#{courseIdentifier},
#{bestCourseGrade},
#{latestCourseGrade},
#{totalNumberOfSequences},
#{lastModifiedAt}
)
</if>
Second, the method below:二、方法如下:
UserLearningCourse saveUpdate(UserLearningCourse entity);
Third, Invoke the method:三、调用方法:
public UserLearningCourse saveUpdate(UserLearningCourse userLearningCourse)
{
int affectRows = userLearningCourseDao.saveOrUpdate(userLearningCourse);
return userLearningCourse;
}
So here, you can see that, the return ID has been automatically filt into the origin userLearningCourse.所以在这里,你可以看到,返回ID已经自动过滤到源userLearningCourse中。 So here you can directly return the entity now.所以这里你现在可以直接返回实体了。 Because we refered the keyproperty and keycolumn, so it will auto return by mybatis.因为我们引用了keyproperty和keycolumn,所以mybatis会自动返回。 saveOrUpdate only return affect rows in database. saveOrUpdate 只返回影响数据库中的行。
if you have list of item you may use like below :如果你有你可以使用的项目列表,如下所示:
<insert id="insertTapAndGoInfo" parameterType="java.util.List">
<foreach collection="list" item="TapAndGo" index="index">
MERGE tap_and_go_info as tap
USING
(select 1 as c_temp ) as temp ON
(
tap.user_id=#{TapAndGo.id.userId} and
tap.station_code=#{TapAndGo.id.stationCode} and
tap.article_id=#{TapAndGo.id.articleId}
)
WHEN matched then
UPDATE
SET
tap.count = #{TapAndGo.count}
WHEN not matched then
INSERT
([user_id]
,[station_code]
,[article_id]
,[product_name]
,[count]
,[label_code]
,[status_update_time]
,[accesspoint_ip_address]
,[accesspoint_mac]
,[modified])
VALUES
(
#{TapAndGo.id.userId},
#{TapAndGo.id.stationCode},
#{TapAndGo.id.articleId},
#{TapAndGo.productName},
#{TapAndGo.count},
#{TapAndGo.labelCode},
#{TapAndGo.statusUpdateTime},
#{TapAndGo.accesspointIpAddress},
#{TapAndGo.accesspointMac},
#{TapAndGo.modified}
);
</foreach>
</insert>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.