mybatis 中使用包裝類和擴充套件類物件的好處
阿新 • • 發佈:2018-11-29
在實際開發專案中,我們查詢條件不可能很單一,查詢欄位可能包括很多欄位,比如:查詢條件可以有使用者資訊,商品資訊,訂單資訊等。
這裡我們的基礎實體類是使用者類,我在這個基礎上擴充套件他,之後包裝他作為我們的查詢條件。
/** * 使用者po類 */ import java.util.Date; public class User { @Override public String toString() { return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; } private int id; private String username; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } private Date birthday; private String address; }
/**
* User擴充套件物件
* 用於繼承user類,直接獲取user物件的欄位
* 並新增一些擴充套件欄位
*
*/
public class UserCustom extends User{
//可以新增一些user類中沒有的欄位
//...
}
包裝類:用於parameterType
public class UserQueryVo { private UserCustom userCustom; public UserCustom getUserCustom() { return userCustom; } public void setUserCustom(UserCustom userCustom) { this.userCustom = userCustom; } }
mapper配置檔案編寫sql:
<select id="findUserList" parameterType="userQueryVo" resultType="userCustom">
select * from user where username like '%${userCustom.username}%'
</select>
之後就是測試使用者的編寫。
自定義查詢條件查詢,藉助定義pojo包裝型別實現
parameterType:指定包裝型別,將不同的引數(可以是pojo,也可以是簡單型別)傳入進去
包裝型別的好處