1.当一对多查询时可能会出现如下情况
select students.*,admin.a_id,admin.a_username,admin.a_password,evaluate.e_sudent_t from
students,admin,evaluate where students.s_studentid=admin.a_nameid
and evaluate.e_studentid = students.s_studentid;
2.编码实现
要不能出现重复学生信息
在StudentBean.java中添加属性,List<EvaluateBean> evaluateBeans 。
package mybatis.ma.entity;
import java.util.List;
/**
* 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;
private List<EvaluateBean> evaluateBeans;
public List<EvaluateBean> getEvaluateBeans() {
return evaluateBeans;
}
public void setEvaluateBeans(List<EvaluateBean> evaluateBeans) {
this.evaluateBeans = evaluateBeans;
}
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 + '\'' +
", evaluateBeans=" + evaluateBeans +
'}';
}
}
mapper.xml (StudentEvaluateMapper.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.ma.mapper.StudentEvaluateMapper">
<resultMap id="StudentEvaluateInfo" type="mybatis.ma.entity.StudentBean">
<id column="s_id" property="s_id"/>
<result column="s_studentid" property="s_studentid"/>
<result column="s_username" property="s_username"/>
<result column="s_password" property="s_password"/>
<result column="s_sex" property="s_sex"/>
<result column="s_class" property="s_class"/>
<result column="s_rank" property="s_rank"/>
<!--配置订单关联的订单明细信息
一条订单关联查询出了多条明细,要使用collection进行映射
collection :对关联查询到多条记录映射到集合中
property:将关联查询到的多条记录映射到com.djp.pojo.StudentBean中的哪个属性
ofType:指定映射到list集合属性中的pojo的类型
-->
<collection property="evaluateBeans" ofType="mybatis.ma.entity.EvaluateBean">
<id column="e_id" property="e_id"></id>
<result column="e_sudent_t" property="e_sudent_t"></result>
<result column="e_time" property="e_time"></result>
</collection>
</resultMap>
<select id="findStudentEvaluateInfo" resultMap="StudentEvaluateInfo">
select students.*,evaluate.e_sudent_t ,evaluate.e_time from students,admin,evaluate
where students.s_studentid=admin.a_nameid and
evaluate.e_studentid = students.s_studentid;
</select>
</mapper>
接口:StudentEvaluateMapper.java
package mybatis.ma.mapper;
import mybatis.ma.entity.StudentBean;
import java.util.List;
/**
* Created by 马欢欢 on 17-7-1.
*/
public interface StudentEvaluateMapper {
List<StudentBean> findStudentEvaluateInfo() throws Exception;
}
配置文件: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/StudentEvaluateMapper.xml"></mapper>
</mappers>
</configuration>
测试:StudentEvaluateMapperTest.java
package mybatis.ma.mybatisTest;
import mybatis.ma.entity.StudentBean;
import mybatis.ma.mapper.StudentEvaluateMapper;
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 StudentEvaluateMapperTest {
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 findStudentEvaluateInfo() throws Exception{
SqlSession sqlSession = sqlSessionFactory.openSession();
//StudentCustomMapper,mybatis自动生成mapper代理对象
StudentEvaluateMapper studentEvaluateMapper = sqlSession.getMapper(StudentEvaluateMapper.class);
List<StudentBean> studentBeans = studentEvaluateMapper.findStudentEvaluateInfo();
System.out.println(studentBeans);
sqlSession.close();
}
}
总结:mybatis使用resultMap实现一对多查询用collection对关联查询出的多条记录映射到一个list集合中
上一篇:mybatis高级映射——(一对一查询)