通过parameterType指定输入参数的类型,类型可以是简单类型、hashmap、pojo的包装类型 。
在此我们定义一个包装类型的pojo,来完成比较复杂的查询
(User.java)
package com.mybatis.po;
/**
* Created by Administrator on 2018/3/28.
* author:邹培贤
*/
public class User {
//属性名和数据库的字段对应
private int id ;
private String name ;
private String loginName;
private String password;
private String gender ;
private String phoneNumber;
private String email ;
private String description ;
private String departmentId ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDepartmentId() {
return departmentId;
}
public void setDepartmentId(String departmentId) {
this.departmentId = departmentId;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", loginName='" + loginName + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
", email='" + email + '\'' +
", description='" + description + '\'' +
", departmentId='" + departmentId + '\'' +
'}';
}
}
(ForumPostUser.java)
package com.mybatis.po;
/**
* Created by Administrator on 2018/3/29.
* 发帖用户(扩展自用户类User)
*/
public class ForumPostUser extends User{
}
(TopicQueryVo.java)
package com.mybatis.po;
/**
* Created by Administrator on 2018/3/29.
* 论坛发帖的包装类型
*/
public class TopicQueryVo {
//在这里包装所需要的查询条件
//发帖作者查询条件
private ForumPostUser forumPostUser;
//可以包装其他的查询条件,比如当前帖子所属模块等等
public ForumPostUser getForumPostUser() {
return forumPostUser;
}
public void setForumPostUser(ForumPostUser forumPostUser) {
this.forumPostUser = forumPostUser;
}
}
(向UserMapper.xml中添加如下:)
<!--发帖用户信息综合查询-->
<select id="findUserList" parameterType="com.mybatis.po.TopicQueryVo" resultType="com.mybatis.po.ForumPostUser">
SELECT * FROM oa_user WHERE oa_user.loginName = #{forumPostUser.loginName}
AND oa_user.name LIKE '%${forumPostUser.name}%'
</select>
(UserMapper.java新增如下:)
//用户的综合查询
public List<ForumPostUser> findUserList(TopicQueryVo topicQueryVo) throws Exception;
(UserMapperTest.java测试新增如下:)
@Test
public void testFindUserList() throws Exception {
SqlSession sqlSession=sqlSessionFactory.openSession();
//创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
//创建包装条件
TopicQueryVo topicQueryVo=new TopicQueryVo();
ForumPostUser forumPostUser=new ForumPostUser();
forumPostUser.setName("贤");
forumPostUser.setLoginName("w1");
topicQueryVo.setForumPostUser(forumPostUser);
//调用userMapper的方法
List<ForumPostUser> forumPostUserList= userMapper.findUserList(topicQueryVo);
System.out.println("获取结果长度"+forumPostUserList.size());
sqlSession.close();
}