Spring JDBC与事务管理3:Spring JDBC三:Jdbc Template查询方法;(queryForObject();query();queryForList();)

说明:

(1)在【Spring JDBC与事务管理2:Spring JDBC二:Spring JDBC配置过程;】中介绍了【Spring JDBC的配置过程】;本篇博客主要内容是Spring JDBC的核心类→JdbcTemplate类的的查询方法;

(2)本篇博客的代码沿用【Spring JDBC与事务管理2:Spring JDBC二:Spring JDBC配置过程;】;

(3)本篇博客内容说明:

          ● 如果查询结果只有一条记录:使用queryForObject()方法;

          ● 如果查询结果有多条记录:使用query()方法;

          ● 如果查询结果无法用一个实体类对象去承载:使用queryForList()方法;(这个在实际开发中还是比较常用的)

目录

0.准备:在pom.xml中引入【junit单元测试】,【spring test测试模块】;

1.JdbcTemplate的【queryForObject()】:查询单条数据;

2.JdbcTemplate的【query()】:查询多条(复合)数据;

3.JdbcTemplate的【queryForList()】:当没有的实体类对象能够去承载查询结果时,使用List去承载查询结果;(string,object)>


0.准备:在pom.xml中引入【junit单元测试】,【spring test测试模块】;

说明:

(1)这儿引入【junit单元测试】,【spring test测试模块】的目的是,可以更灵活的来及时测试JdbcTemplate各个方法的效果;

(2)【junit单元测试】可以快速参考【单元测试与Junit4】;【spring test测试模块】可以快速参考【Spring IoC容器与Bean管理27:Spring Test测试模块;Spring与JUnit4整合;(@RunWith,@ContextConfiguration)】;

1.JdbcTemplate的【queryForObject()】:查询单条数据;

JdbcTemplateTestor:

import com.imooc.spring.jdbc.dao.EmployeeDao;
import com.imooc.spring.jdbc.entity.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class JdbcTemplateTestor {
    @Resource
    private EmployeeDao employeeDao;

    @Test
    public void testFindById() {
        Employee employee = employeeDao.findById(3308);
        System.out.println(employee);
    }

}

说明:

(1)@RunWith()和@ContextConfiguration()注解,作用是整合【spring test】和【junit】;可以参考【Spring IoC容器与Bean管理27:Spring Test测试模块;Spring与JUnit4整合;(@RunWith,@ContextConfiguration)】;

(2)把IoC容器中的EmployeeDao对象,注入到当前测试类对象中;

(3)当查询结果只有一条记录时,使用JdbcTemplate的queryForObject()方法;

(4)运行结果;

2.JdbcTemplate的【query()】:查询多条(复合)数据;

首先,在EmployeeDao类中,增加一个findByDname()方法,该方法会调用JdbcTemplate的query()方法; 

package com.imooc.spring.jdbc.dao;

import com.imooc.spring.jdbc.entity.Employee;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class EmployeeDao {
    private JdbcTemplate jdbcTemplate;

    public List<Employee> findByDname(String dname) {
        String sql = "select * from employee where dname = ?";
        List<Employee> list = jdbcTemplate.query(sql,
                new Object[]{dname},
                new BeanPropertyRowMapper<Employee>(Employee.class));
        return list;
    }
    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}

说明:

(1)方法说明:查询结果有多条记录,使用JdbcTemplate的query()方法;

…………………………………………………… 

然后,在JdbcTemplateTestor类中创建testFindByDname()方法去测试;

import com.imooc.spring.jdbc.dao.EmployeeDao;
import com.imooc.spring.jdbc.entity.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class JdbcTemplateTestor {
    @Resource
    private EmployeeDao employeeDao;

    @Test
    public void testFindByDname() {
        System.out.println(employeeDao.findByDname("研发部"));
    }

}

说明:

(1)运行结果;

3.JdbcTemplate的【queryForList()】:当没有的实体类对象能够去承载查询结果时,使用List<Map(String,Object)>去承载查询结果

首先,在EmployeeDao类中,增加一个findMapByDname()方法,该方法会调用JdbcTemplate的queryForList()方法;  

package com.imooc.spring.jdbc.dao;

import com.imooc.spring.jdbc.entity.Employee;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class EmployeeDao {
    private JdbcTemplate jdbcTemplate;


    public List<Map<String, Object>>  findMapByDname(String dname) {
        String sql = "select eno as empno,salary as s from employee where dname = ?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, new Object[]{dname});
        return maps;
    }
    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}

说明:

(1)情况说明:没有【实体类对象】能够去承载【查询结果】;(即,无法完成实体类和数据表的映射)

(2)没有【实体类对象】能够去承载【查询结果】时,使用queryForList()方法,使用List<Map()>去承载查询结果;

…………………………………………………… 

然后,在JdbcTemplateTestor类中创建testFindMapByDname()方法去测试;

import com.imooc.spring.jdbc.dao.EmployeeDao;
import com.imooc.spring.jdbc.entity.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class JdbcTemplateTestor {
    @Resource
    private EmployeeDao employeeDao;

    @Test
    public void testFindMapByDname() {
        System.out.println(employeeDao.findMapByDname("研发部"));
    }

}

说明:

(1)运行结果;

(2)分析:通过queryForList()方法,即使没有合适的实体类对象可以去承载查询结果,也可以通过一个List<Map(String,Object)>去承载查询结果;这在实际开发中,是比较常见的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值