JDBCTemplate 的Query方法的重要说明

乜钱明
2023-12-01

package Test03;

import JDBCUtils.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Date;
import java.util.List;
import java.util.Map;

/*
JDBCTemplate实现查询:queryXXX()

        一、query返回数据
            1.public <T> T queryForObject(String sql, Class<T> requiredType, Object... args)    返回一个数据,用指定类型接收
            2.public Map<String, Object> queryForMap(String sql, Object... args)                返回一行数据,用Map集合接收
            3.public List<Map<String, Object>> queryForList(String sql, Object... args)         返回多行数据,用list接受,list中保存的是多个map

*重要*  二、使用RowMapper做映射返回对象(此时需要创建实体类emp)
            4.public <T> T queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object... args)     返回一个映射对象
            5.public <T> List<T> query(String sql, RowMapper<T> rowMapper, @Nullable Object... args)                        了                                                    。          返回多个映射对象存入到list集合中

*/
public class Test02 {
//1.
@Test
public void test_queryForObject() {
JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
String ename = jt.queryForObject(“SELECT ename FROM emp WHERE id=?”, String.class, 3);
System.out.println(“ename:” + ename);
}

//2.
@Test
public void test_queryForMap() {
    JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
    Map<String, Object> map = jt.queryForMap("SELECT * FROM emp WHERE id=?", 3);
    System.out.println(map);
}

//3.
@Test
public void test_queryForList() {
    JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
    List<Map<String, Object>> list = jt.queryForList("SELECT * FROM emp");
    for (Map<String, Object> map : list) {
        System.out.println(map);
    }
}

//4.
@Test
public void test_queryForObject_RowMapper() {
    JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
    emp emp = jt.queryForObject("SELECT * FROM emp WHERE id=?", new BeanPropertyRowMapper<>(emp.class), 3);
    /**
     * 这里rowmapper是一个接口,快捷键Ctrl+H查看选中接口的结构图,使用BeanPropertyRowMapper<>(类名.class)
     */
    System.out.println(emp);
}

//5.
@Test
public void test_queryFor() {
    JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
    List<emp> empList = jt.query("SELECT * FROM emp", new BeanPropertyRowMapper<>(emp.class));
    for (emp emp : empList) {
        System.out.println(emp);
    }
}

}

/*
实体类要求:
1.两条建议:类名=表名,属性名=字段名
2.两条必须:成员属性类型最好使用包装类类型,setXXX方法,XXX必须和数据库列名一致
*/
class emp {
private Integer id;
private String ename;
private Double sal;
private Date birth;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getEname() {
    return ename;
}

public void setEname(String ename) {
    this.ename = ename;
}

public Double getSal() {
    return sal;
}

public void setSal(Double sal) {
    this.sal = sal;
}

public Date getBirth() {
    return birth;
}

public void setBirth(Date birth) {
    this.birth = birth;
}

@Override
public String toString() {
    return "emp{" +
            "id=" + id +
            ", ename='" + ename + '\'' +
            ", sal=" + sal +
            ", birth=" + birth +
            '}';
}

}

——————————————

 类似资料: