gjdyjs
From entry to abandoment
2021-01-19 12:51

java查询的结果集放到list里

  • java
  • oracle
  • maven
  • intellij-idea

controller

@RestController
public class CsvController {
    @Autowired
    CsvDownloadService csvDownloadService;

    @RequestMapping(value = "/downCsv",method= RequestMethod.GET)
    public void DownCsv(HttpServletResponse response) throws IOException {
        List<couse> result = csvDownloadService.CsvDown();
        System.out.println(result);
    }
}
    
 

service

@Service
public class CsvDownloadService {
    @Autowired
    CsvDownloadMapper csvDownloadMapper;
    //获取数据
    public List<couse> CsvDown(){
        return csvDownloadMapper.CsvDown();
    }
}

entity

public class couse {
    private int couseid;
    private String cousername;
    private String cousetype;

    public int getCouseid() {
        return couseid;
    }

    public void setCouseid(int couseid) {
        this.couseid = couseid;
    }

    public String getCousername() {
        return cousername;
    }

    public void setCousername(String cousername) {
        this.cousername = cousername;
    }

    public String getCousetype() {
        return cousetype;
    }

    public void setCousetype(String cousetype) {
        this.cousetype = cousetype;
    }
}

mapper 

@Repository
@Mapper
public interface CsvDownloadMapper {
    List<couse> CsvDown();
}

xml

<mapper namespace="com.example.demo.mapper.CsvDownloadMapper">
    <resultMap id="BaseResultMap" type="com.example.demo.entity.couse">
        <result column="COUSEID" jdbcType="INTEGER" property="couseid" />
        <result column="COUSENAME" jdbcType="VARCHAR" property="cousename" />
        <result column="COUSETYPE" jdbcType="VARCHAR" property="cousetype" />
    </resultMap>

    <select id="CsvDown" resultType="com.example.demo.entity.couse">
        select * from T_COUSE
    </select>
</mapper>

执行结果

[com.example.demo.entity.couse@60cc9727, com.example.demo.entity.couse@1f303423, com.example.demo.entity.couse@1001e123, com.example.demo.entity.couse@7bef6cc0]

期望结果

[

{couseid:"10",cousename:"asdasd",cousetype:"asdasda"}

{couseid:"11",cousename:"asdasd",cousetype:"asdasda"}

{couseid:"12",cousename:"asdasd",cousetype:"asdasda"}

]

我知道在entity里加个toString方法可以变成期望值,但是不是list类型是string类型,想实现直接就是返回一个list,里面数据能正常显示。没接触过这方面,有人可以帮帮忙回答一下吗

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答