Mybatis更新数据库示例

@ mybatis-config.xml

1
2
3
<mappers>
<mapper resource="mybatis/xml/GuestInfoMapper.xml" />
</mappers>

@ 添加GuestInfoMapper接口

1
2
public interface GuestInfoMapper {
void updateGuestInfoByGuestNumber(UpdateGuestInfo updateGuestInfo);

@ 编写model类,用于包装参数

1
2
3
4
5
6
7
package mybatis.model;
public class UpdateGuestInfo {
private String guestNumber;
private String firstName;
private String lastName;
private String documentType;
private String documentTypeColumn;

@ 添加Mapper.xml,编写sql
GuestInfoMapper.xml

1
2
3
4
5
6
7
8
9
10
11
<update id="updateGuestInfoByGuestNumber" parameterType="cn.shijinet.kunlun.kiosk.mybatis.model.UpdateGuestInfo">
UPDATE NAME
SET
xlast_name = #{lastName},
xfirst_name = #{firstName},
sxname = #{lastName},
sxfirst_name = #{firstName},
${documentTypeColumn} = #{documentType}
WHERE
name_id = #{guestNumber}
</update>

@ 调用接口方法,注意update时需要commit

1
2
3
4
SqlSession session = sqlSession.get();
GuestInfoMapper mapper = session.getMapper(GuestInfoMapper.class);
mapper.updateGuestInfoByGuestNumber(updateGuestInfo);
session.commit();

本文总阅读量