使用 MyBatis 将用户信息保存到 MySQL
Saving user info into MySQL using MyBatis
我在 java 项目中使用 mybatis 将用户信息保存到 mysql 时遇到问题。要求如下:
如果数据库中不存在用户信息,则插入该用户。
如果数据库中存在用户信息,则更新该用户。
现在查了下mybatis的文档,动态的sql定义可以节省我的时间,但是没找到例子。
下面是我要更改的示例 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>
谁做过这个,请给我点灯好吗?谢谢
编辑:
现在我可以根据过滤器插入或更新数据库了。但问题是我无法获得 return 结果。有人愿意帮忙吗?下面的xmlsql:
<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>
但是在我 运行 它之后,它会抛出异常:
{
"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":空,
"payload": 空
}
我在代码中定义的方法是:
UserLearningCourse saveOrUpdate(UserLearningCourse entity);
我打算 return 实体,但抛出错误。我知道 xml sql 配置不是 return 类型。但如何解决?
终于,我解决了这个问题。
首先,下面的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>
二、方法如下:
UserLearningCourse saveUpdate(UserLearningCourse entity);
三、调用方法:
public UserLearningCourse saveUpdate(UserLearningCourse userLearningCourse)
{
int affectRows = userLearningCourseDao.saveOrUpdate(userLearningCourse);
return userLearningCourse;
}
所以在这里,您可以看到,return ID 已自动过滤到原始 userLearningCourse 中。所以现在你可以直接return这个实体了。
因为我们引用了 keyproperty 和 keycolumn,所以它会被 mybatis auto return。
saveOrUpdate 仅 return 影响数据库中的行。
如果您有可以使用的物品清单,如下所示:
<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>
我在 java 项目中使用 mybatis 将用户信息保存到 mysql 时遇到问题。要求如下:
如果数据库中不存在用户信息,则插入该用户。
如果数据库中存在用户信息,则更新该用户。
现在查了下mybatis的文档,动态的sql定义可以节省我的时间,但是没找到例子。 下面是我要更改的示例 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>
谁做过这个,请给我点灯好吗?谢谢
编辑: 现在我可以根据过滤器插入或更新数据库了。但问题是我无法获得 return 结果。有人愿意帮忙吗?下面的xmlsql:
<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>
但是在我 运行 它之后,它会抛出异常:
{ "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":空, "payload": 空 }
我在代码中定义的方法是:
UserLearningCourse saveOrUpdate(UserLearningCourse entity);
我打算 return 实体,但抛出错误。我知道 xml sql 配置不是 return 类型。但如何解决?
终于,我解决了这个问题。 首先,下面的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>
二、方法如下:
UserLearningCourse saveUpdate(UserLearningCourse entity);
三、调用方法:
public UserLearningCourse saveUpdate(UserLearningCourse userLearningCourse)
{
int affectRows = userLearningCourseDao.saveOrUpdate(userLearningCourse);
return userLearningCourse;
}
所以在这里,您可以看到,return ID 已自动过滤到原始 userLearningCourse 中。所以现在你可以直接return这个实体了。 因为我们引用了 keyproperty 和 keycolumn,所以它会被 mybatis auto return。 saveOrUpdate 仅 return 影响数据库中的行。
如果您有可以使用的物品清单,如下所示:
<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>