Java 使用MyBatis将用户信息保存到MySQL
我在java项目中使用mybatis将用户信息保存到mysql时遇到问题。要求如下: 若数据库中不存在用户信息,则插入此用户 若数据库中存在用户信息,则更新此用户 现在我已经检查了mybatis的文档,动态sql定义可能会节省我的时间,但我没有在上面找到示例。 下面是我想要更改的示例sql:Java 使用MyBatis将用户信息保存到MySQL,java,mysql,mybatis,Java,Mysql,Mybatis,我在java项目中使用mybatis将用户信息保存到mysql时遇到问题。要求如下: 若数据库中不存在用户信息,则插入此用户 若数据库中存在用户信息,则更新此用户 现在我已经检查了mybatis的文档,动态sql定义可能会节省我的时间,但我没有在上面找到示例。 下面是我想要更改的示例sql: <insert id="create" parameterType="UserLearningCourse" > <!--If record not exists, perf
<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>
我打算返回实体,但错误抛出。我知道xml sql配置不是返回类型。但是如何解决呢?最后,我解决了这个问题。 首先,下面是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>
<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>
第三,调用方法:
public UserLearningCourse saveUpdate(UserLearningCourse userLearningCourse)
{
int affectRows = userLearningCourseDao.saveOrUpdate(userLearningCourse);
return userLearningCourse;
}
在这里,您可以看到,返回ID已自动过滤到origin userLearningCourse中。因此,现在您可以直接返回实体。
因为我们引用了keyproperty和keycolumn,所以它将由mybatis自动返回。
saveOrUpdate仅返回影响数据库中的行。如果您有可以使用的项目列表,如下图所示:
<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>
将点击和点击信息合并为点击
使用
(选择1作为c_温度)作为温度开启
(
tap.user_id={TapAndGo.id.userId}和
tap.station_code={TapAndGo.id.stationCode}和
tap.article_id={TapAndGo.id.articleId}
)
当匹配时
更新
设置
tap.count=#{TapAndGo.count}
当不匹配时
插入
([用户识别码]
,[车站代码]
,[文章编号]
,[产品名称]
,[计数]
,[标签编号]
,[状态\更新\时间]
,[accesspoint\u ip\u地址]
,[accesspoint\u mac]
,[修改])
价值观
(
#{TapAndGo.id.userId},
#{TapAndGo.id.stationCode},
#{TapAndGo.id.articleId},
#{TapAndGo.productName},
#{TapAndGo.count},
#{TapAndGo.labelCode},
#{TapAndGo.statusUpdateTime},
#{TapAndGo.accesspointIpAddress},
#{TapAndGo.accesspointMac},
#{TapAndGo.modified}
);
Dear@CharlieShi自动执行查询不会返回插入或更新的记录id,它返回的是selectKey keyProperty=“count”的值
public UserLearningCourse saveUpdate(UserLearningCourse userLearningCourse)
{
int affectRows = userLearningCourseDao.saveOrUpdate(userLearningCourse);
return userLearningCourse;
}
<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>