一.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<StudentCustom> findStudentInfo () throws Exception;
}
4.编写mapper.xml,即StudentCustomMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mybatis.huan.mapper.StudentCustomMapper">
<!--查询学生关联管理级别-->
<select id="findStudentInfo" resultType="mybatis.huan.entity.StudentCustom">
select students.*,admin.a_id,admin.a_username,admin.a_password from students,admin
where students.s_studentid=admin.a_nameid
</select>
</mapper>
5.配置文件
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"></properties>
<typeAliases >
<package name="cn"></package>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<!--添加一对一映射:映射文件-->
<mapper resource="mapper/StudentCustomMapper.xml"></mapper>
</mappers>
</configuration>
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<StudentCustom> 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<StudentBean> findStudentInfoResultMap () throws Exception;
}
3.编写mapper.xml,即StudentCustomMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mybatis.huan.mapper.StudentCustomMapper">
<!--整个查询信息映射到StudentBean中-->
<resultMap id="StudentInfoResultMap" type="mybatis.huan.entity.StudentBean">
<!--配置映射学生信息
column:学生信息唯一表标识
property:学生信息唯一标识列所映射到StudentBean哪个属性中
-->
<id column="s_id" property="s_id"/>
<id column="s_studentid" property="s_studentid"/>
<id column="s_username" property="s_username"/>
<id column="s_password" property="s_password"/>
<id column="s_sex" property="s_sex"/>
<id column="s_class" property="s_class"/>
<id column="s_rank" property="s_rank"/>
<!--配置映射管理员信息-->
<!--association:用于映射关联查询单个对象的信息
property:要将关联查询的用户信息映射到Student中的那个属性
-->
<association property="adminBean" javaType="mybatis.huan.entity.AdminBean">
<id column="a_id" property="a_id"/>
<id column="a_nameid" property="a_nameid"/>
<id column="a_username" property="a_username"/>
<id column="a_password" property="a_password"/>
<id column="a_trank" property="a_trank"/>
</association>
</resultMap>
<!--查询学生关联管理级别resultMap-->
<select id="findStudentInfoResultMap" resultMap="StudentInfoResultMap">
select students.*,admin.a_id,admin.a_username,admin.a_password from students,admin
where students.s_studentid=admin.a_nameid
</select>
</mapper>
4.配置文件
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"></properties>
<typeAliases >
<package name="cn"></package>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<!--添加一对一映射:映射文件-->
<mapper resource="mapper/StudentCustomMapper.xml"></mapper>
</mappers>
</configuration>
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<StudentBean> list=studentCustomMapper.findStudentInfoResultMap();
System.out.println(list);
sqlSession.close();
}
}
总结:
- 1.要实现一对一查询,使用resultType实现比较简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
- 2.若使用resultMap,需要单独定义resultMap,实现有点麻烦,如果对查询结果有特殊要求,使用resultMap可以完成将关联查询出的结果映射到pojo中。
- 3.resultMap可以实现延迟加载,resultType不可以。
.
上一篇:mybatis输入映射(包装类型)+输出映射+动态sql