【spring-boot】通过mybatis的配置文件方式查询数据库中的多条数据

需求:当我们相同要通过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如下:

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;
    }
}
View Code

最红运行效果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值