一.resultType实现
resultType的方式查询,查询出的字段需要和pojo类中字段名一致才能完成输出映射,学生pojo类字段并不满足sql语句字段查询出来的全部映射,所以需要新增新的pojo类StudentCustom,它继承于学生类
代码实现步骤:
1.编写我们需要的pojo
StudentBean.java
package mybatis.huan.entity;
/**
* Created by 马欢欢 on 2017/6/6.
*/
public class StudentBean {
private int s_id;
private String s_studentid;
private String s_username;
private String s_password;
@Override
public String toString() {
return "StudentBean{" +
"s_id=" + s_id +
", s_studentid='" + s_studentid + '\'' +
", s_username='" + s_username + '\'' +
", s_password='" + s_password + '\'' +
", s_sex='" + s_sex + '\'' +
", s_class='" + s_class + '\'' +
", s_rank='" + s_rank + '\'' +
'}';
}
private String s_sex;
private String s_class;
private String s_rank;
public int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_id;
}
public String getS_studentid() {
return s_studentid;
}
public void setS_studentid(String s_studentid) {
this.s_studentid = s_studentid;
}
public String getS_username() {
return s_username;
}
public void setS_username(String s_username) {
this.s_username = s_username;
}
public String getS_password() {
return s_password;
}
public void setS_password(String s_password) {
this.s_password = s_password;
}
public String getS_sex() {
return s_sex;
}
public void setS_sex(String s_sex) {
this.s_sex = s_sex;
}
public String getS_class() {
return s_class;
}
public void setS_class(String s_class) {
this.s_class = s_class;
}
public String getS_rank() {
return s_rank;
}
public void setS_rank(String s_rank) {
this.s_rank = s_rank;
}
}
2.学生类的扩展类
扩展类StudentCustom.java
package mybatis.huan.entity;
/**
* Created by 马欢欢 on 17-7-1.
*/
public class StudentCustom extends StudentBean {
private int a_id;
private String a_nameid;
private String a_username;
@Override
public String toString() {
return super.toString()+"StudentCustom{" +
"a_id=" + a_id +
", a_nameid='" + a_nameid + '\'' +
", a_username='" + a_username + '\'' +
", a_password='" + a_password + '\'' +
", a_trank='" + a_trank + '\'' +
'}'+'\n';
}
private String a_password;
private String a_trank;
public int getA_id() {
return a_id;
}
public void setA_id(int a_id) {
this.a_id = a_id;
}
public String getA_nameid() {
return a_nameid;
}
public void setA_nameid(String a_nameid) {
this.a_nameid = a_nameid;
}
public String getA_username() {
return a_username;
}
public void setA_username(String a_username) {
this.a_username = a_username;
}
public String getA_password() {
return a_password;
}
public void setA_password(String a_password) {
this.a_password = a_password;
}
public String getA_trank() {
return a_trank;
}
public void setA_trank(String a_trank) {
this.a_trank = a_trank;
}
}
3.编写mapper.java接口
StudentCustomMapper.java
package mybatis.huan.mapper;
import mybatis.huan.entity.StudentCustom;
import java.util.List;
/**
* Created by 马欢欢 on 17-7-1.
*/
public interface StudentCustomMapper {
/**
*综合查询学生信息包括管理级别
*/
List findStudentInfo () throws Exception;
}
4.编写mapper.xml,即StudentCustomMapper.xml
5.配置文件
mybatis-config.xml
6.编写测试类,代码如下:
StudentCustomMapperText.java
package mybatis.huan.mybatisTest;
import mybatis.huan.entity.StudentCustom;
import mybatis.huan.mapper.StudentCustomMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Created by 马欢欢 on 17-7-1.
*/
public class StudentCustomMapperText {
private SqlSessionFactory sqlSessionFactory;
@Before
//测试前执行
public void setUp() throws IOException {
//配置文件
String resource = "mybatis-config.xml";
//的到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void findStudentInfo() throws Exception{
SqlSession sqlSession = sqlSessionFactory.openSession();
//StudentCustomMapper,mybatis自动生成mapper代理对象
StudentCustomMapper studentCustomMapper = sqlSession.getMapper(StudentCustomMapper.class);
List list=studentCustomMapper.findStudentInfo();
System.out.println(list);
sqlSession.close();
}
}
二.resultMap实现
使用resultMap将查询结果映射到学生StudentBean信息中,在学生类中添加AdminBean属性,给get和set方法将关联查询的用户信息映射到StudentBean对象中的AdminBean属性
1.编写我们需要的pojo
StudentBean.java
package mybatis.huan.entity;
/**
* Created by 马欢欢 on 2017/6/6.
*/
public class StudentBean {
private int s_id;
private String s_studentid;
private String s_username;
private String s_password;
private String s_sex;
private String s_class;
private String s_rank;
//添加Admin属性
private AdminBean adminBean;
public AdminBean getAdminBean() {
return adminBean;
}
public void setAdminBean(AdminBean adminBean) {
this.adminBean = adminBean;
}
public int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_id;
}
public String getS_studentid() {
return s_studentid;
}
public void setS_studentid(String s_studentid) {
this.s_studentid = s_studentid;
}
public String getS_username() {
return s_username;
}
public void setS_username(String s_username) {
this.s_username = s_username;
}
public String getS_password() {
return s_password;
}
public void setS_password(String s_password) {
this.s_password = s_password;
}
public String getS_sex() {
return s_sex;
}
public void setS_sex(String s_sex) {
this.s_sex = s_sex;
}
public String getS_class() {
return s_class;
}
public void setS_class(String s_class) {
this.s_class = s_class;
}
public String getS_rank() {
return s_rank;
}
public void setS_rank(String s_rank) {
this.s_rank = s_rank;
}
@Override
public String toString() {
return "StudentBean{" +
"s_id=" + s_id +
", s_studentid='" + s_studentid + '\'' +
", s_username='" + s_username + '\'' +
", s_password='" + s_password + '\'' +
", s_sex='" + s_sex + '\'' +
", s_class='" + s_class + '\'' +
", s_rank='" + s_rank + '\'' +
'}'+'\n';
}
}
AdminBean,java
package mybatis.huan.entity;
/**
* Created by 马欢欢 on 2017/6/6.
*/
public class AdminBean {
private int a_id;
private String a_nameid;
private String a_username;
private String a_password;
private String a_trank;
@Override
public String toString() {
return "AdminBean{" +
"a_id=" + a_id +
", a_nameid='" + a_nameid + '\'' +
", a_username='" + a_username + '\'' +
", a_password='" + a_password + '\'' +
", a_trank='" + a_trank + '\'' +
'}';
}
public int getA_id() {
return a_id;
}
public void setA_id(int a_id) {
this.a_id = a_id;
}
public String getA_nameid() {
return a_nameid;
}
public void setA_nameid(String a_nameid) {
this.a_nameid = a_nameid;
}
public String getA_username() {
return a_username;
}
public void setA_username(String a_username) {
this.a_username = a_username;
}
public String getA_password() {
return a_password;
}
public void setA_password(String a_password) {
this.a_password = a_password;
}
public String getA_trank() {
return a_trank;
}
public void setA_trank(String a_trank) {
this.a_trank = a_trank;
}
}
2.编写mapper.java接口
StudentCustomMapper.java
package mybatis.huan.mapper;
import mybatis.huan.entity.StudentBean;
import mybatis.huan.entity.StudentCustom;
import java.util.List;
/**
* Created by 马欢欢 on 17-7-1.
*/
public interface StudentCustomMapper {
/**
*综合查询学生信息包括管理级别
*/
List findStudentInfoResultMap () throws Exception;
}
3.编写mapper.xml,即StudentCustomMapper.xml
4.配置文件
mybatis-config.xml
5.编写测试类,代码如下:
StudentCustomMapperText.java
package mybatis.huan.mybatisTest;
import mybatis.huan.entity.StudentBean;
import mybatis.huan.entity.StudentCustom;
import mybatis.huan.mapper.StudentCustomMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Created by 马欢欢 on 17-7-1.
*/
public class StudentCustomMapperText {
private SqlSessionFactory sqlSessionFactory;
@Before
//测试前执行
public void setUp() throws IOException {
//配置文件
String resource = "mybatis-config.xml";
//的到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void findStudentInfoResultMap() throws Exception{
SqlSession sqlSession = sqlSessionFactory.openSession();
//StudentCustomMapper,mybatis自动生成mapper代理对象
StudentCustomMapper studentCustomMapper = sqlSession.getMapper(StudentCustomMapper.class);
List list=studentCustomMapper.findStudentInfoResultMap();
System.out.println(list);
sqlSession.close();
}
}
总结:
- 1.要实现一对一查询,使用resultType实现比较简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
- 2.若使用resultMap,需要单独定义resultMap,实现有点麻烦,如果对查询结果有特殊要求,使用resultMap可以完成将关联查询出的结果映射到pojo中。
- 3.resultMap可以实现延迟加载,resultType不可以。
.
上一篇:mybatis输入映射(包装类型)+输出映射+动态sql