mybatis 中使用包装类和扩展类对象的好处

在实际开发项目中,我们查询条件不可能很单一,查询字段可能包括很多字段,比如:查询条件可以有用户信息,商品信息,订单信息等。

这里我们的基础实体类是用户类,我在这个基础上扩展他,之后包装他作为我们的查询条件。


/**
 * 用户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,也可以是简单类型)传入进去
         包装类型的好处

猜你喜欢

转载自blog.csdn.net/qq_27908393/article/details/84583170