由于项目需求,需要将以往写的jpa的数据增删改查操作换成mybatis,所以稍作了解mybatis
因时间紧迫,并没有对深入实现原理进行学习,只是查询相关资料,学习如何使用。在本项目中,因已经有前辈所在准备,故配置之类并未进行修改。
mybatis与spring cloud 结合使用
1 首先是定义domain。如果相关联查询,需要在domain中包含索要关联查询的domain属性
private static final long serialVersionUID = -1654620673909327705L;
private String userId;
private String loginCode;
private String userName;
private String certType;
private String certNo;
private String userCode;
private String deadline;
private String orgId;
private String dptId;
private String userPassword;
private String userSex;
private String userBirthday;
private String userEmail;
private String userMobilephone;
private String userOfficetel;
private String userEducation;
private String userCertificate;
private String entrantsDate;
private String positionTime;
private String financialJobTime;
private String positionDegree;
private String userAvatar;
private String offenIp;
private String userSts = "1";
private String lastLoginTime;
private String lastEditPassTime;
private String lastChgUsr = SecurityUtils.getCurrentUserLogin();
private String lastChgDt = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
//private AdminSmDpt adminSmDpt;
private Set authorities = new HashSet<>();
// private AdminSmOrg adminSmOrg;
//private AdminSmInstu adminSmInstu;
代码中的
Set authorities
是AdminSmUser 这个domain的一个属性 ,当然也需要建立该domain
public class AdminSmRole implements Serializable{
private static final long serialVersionUID = -3798852960056031116L;
private String roleId;
private String roleCode;
private String roleName;
private String orgId;
private String roleLevel;
private String roleSts;
private String lastChgUsr = SecurityUtils.getCurrentUserLogin();
private String lastChgDt = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
2建立AdminSmUserMapper.jave
public interface AdminSmUserMapper {
public AdminSmUser selectAdminSmUser(@Param("loginCode") String loginCode);
//public AdminSmUser selectAdminSmUserWithDpt(@Param("loginCode") String loginCode);
public AdminSmUser selectAdminSmUserWithRoles(@Param("loginCode") String loginCode);
public AdminSmUser selectAdminSmUserWithAll(@Param("loginCode") String loginCode);
//public List
该接口并没有任何不同(或者是不知道spring cloud实现原理,所以看不出不同)
3 建立AdminSmUserMapper.xml
update ADMIN_SM_USER set USER_PASSWORD=#{newPassword, jdbcType=VARCHAR},LAST_CHG_USR=#{user, jdbcType=VARCHAR},
LAST_CHG_DT=#{date, jdbcType=VARCHAR} where LOGIN_CODE=#{loginCode, jdbcType=VARCHAR}
其中
resultMap 是为了实现联合查询,其中
association 是一对一的关系
collection是一对多的关系。
该步骤就是为了把查到的数据映射到相应的domain属性中。