需求:当我们相同要通过mybatis-{table}-mapper.xml的方式执行类似:select * from users;的语句时,会查询到很多条数据,那么我们该如何处理呢。
mybatis-score-mapper.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="com.springboot.mapper.ScoreMapper">
<resultMap id="scoreResults" type="com.springboot.bean.Score">
<!--column对应写数据库表中的该表具有的字段,我的数据表就只有三个字段,property对应的是Score.java实体类中对应的字段,不能弄错-->
<result column="id" property="id"></result>
<result column="math" property="math"></result>
<result column="chinese" property="chinese"></result>
</resultMap>
<select id="selectAll" resultMap="scoreResults">
select * from score
</select>
</mapper>
ScoreMapper.java文件如下:
//这里没有加@Mapper 那是因为我在主配置类上加了一个注解:@MapperScan("mapper所在地包全名")
public interface ScoreMapper {
public List<Score> selectAll();
}
ScoreControler.java文件如下:
@RequestMapping("/score/selectall")
public List selectAll(){
List<Score> scores = scoreMapper.selectAll();
System.out.println(scores.toString());
return scores;
}
Score.java如下:
![](https://img-blog.csdnimg.cn/img_convert/8f900a89c6347c561fdf2122f13be562.png)
![](https://img-blog.csdnimg.cn/img_convert/961ddebeb323a10fe0623af514929fc1.png)
public class Score {
private Integer id;
private Float math;
private Float chinese;
@Override
public String toString() {
return "Score{" +
"id=" + id +
", math=" + math +
", chinese=" + chinese +
'}';
}
public Score() {
}
public Score(Float math, Float chinese) {
this.math = math;
this.chinese = chinese;
}
public Score(Integer id, Float math, Float chinese) {
this.id = id;
this.math = math;
this.chinese = chinese;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Float getMath() {
return math;
}
public void setMath(Float math) {
this.math = math;
}
public Float getChinese() {
return chinese;
}
public void setChinese(Float chinese) {
this.chinese = chinese;
}
}
最红运行效果: