【Java/JdbcTemplate】JdbcTemplate的三种典型查询应用场景:查数量,查单字段列表,查对象列表

虽说现在访问DB是MyBatis的天下,但JDBC凭借其广泛性及Spring提供的org.springframework.jdbc.core.JdbcTemplat凭借其小巧灵活性依然不可或缺,下文笔者总结了JdbcTemplate的三种典型查询应用场景:查数量queryForObject,查单字段列表queryForList,查对象列表jdbcTplt.query(sql, new RowMapper...),留此以备查阅。

代码:

package com.hy.lab.jdbctplt;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

class Emp{
    long id;
    String name;
    int age;

    public Emp(long id,String name,int age){
        this.id=id;
        this.name=name;
        this.age=age;
    }

    public String toString(){
        return "Emp id:"+id+" name="+name+" age="+age;
    }
}
public class Test {
    // 例一:查数量
    private static void queryForInt(){
        String sql="select count(*) from emp0519";
        int count=DsUtil.getJdbcTplt().queryForObject(sql,Integer.class);
        System.out.println(count);
    }

    // 例二:查单字段列表
    private static void queryForStringList(){
        String sql="select name from emp0519";
        JdbcTemplate jtplt=HikariDsUtil.getJdbcTplt();
        List<String> ls=jtplt.queryForList(sql,String.class);
        for(String name:ls){
            System.out.println(name);
        }
    }

    // 例三:查对象列表
    private static void queryForObjList(){
        String sql="select id,name,age from emp0519";
        JdbcTemplate jdbcTplt=HikariDsUtil.getJdbcTplt();
        List<Emp> emps = jdbcTplt.query(sql, new RowMapper<Emp>() {
            @Override
            public Emp mapRow(ResultSet rs, int rowNum) throws SQLException {

                long id=rs.getInt("id");
                String name=rs.getString("name");
                int age=rs.getInt("age");

                return new Emp(id,name,age);
            }});

        for(Emp emp:emps){
            System.out.println(emp);
        }
    }

    public static void main(String[] args){
        queryForInt();
        queryForStringList();
        queryForObjList();
    }
}

以上关键语句已用红色标出:

下面是辅助性的类:

HikariDsUtil类:

package com.hy.lab.jdbctplt;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class HikariDsUtil {
    private static final String DRIVER="oracle.jdbc.OracleDriver";
    private static final String URL="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    private static final String USER="luna";
    private static final String PSWD="1234";

    // MyBatis框架的DataSource类:org.apache.ibatis.datasource.pooled.PooledDataSource
    private static HikariDataSource ds;

    static {
        ds=new HikariDataSource();
        ds.setDriverClassName(DRIVER);
        ds.setJdbcUrl(URL);
        ds.setUsername(USER);
        ds.setPassword(PSWD);
        ds.setMinimumIdle(10);
        ds.setMaximumPoolSize(10);
    }

    public static JdbcTemplate getJdbcTplt(){
        return new JdbcTemplate(ds);
    }
}

DsUtil类:

package com.hy.lab.jdbctplt;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;

public class DsUtil {
    private static final String DRIVER="oracle.jdbc.OracleDriver";
    private static final String URL="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    private static final String USER="luna";
    private static final String PSWD="1234";

    // MyBatis框架的DataSource类:org.apache.ibatis.datasource.pooled.PooledDataSource
    private static PooledDataSource ds;

    static {
        ds=new PooledDataSource(DRIVER,URL,USER,PSWD);
    }

    public static JdbcTemplate getJdbcTplt(){
        return new JdbcTemplate(ds);
    }
}

整理之后的输出:

4

杨志
林冲
索超
秦明

Emp id:1 name=杨志 age=25
Emp id:2 name=林冲 age=35
Emp id:3 name=索超 age=45
Emp id:4 name=秦明 age=55

表定义:

create table emp0519(
    id number(10),
    name nvarchar2(20),
    age number(3)
)

表数据:

insert into emp0519(id,name,age) values(1,'杨志',25);
insert into emp0519(id,name,age) values(2,'林冲',35);
insert into emp0519(id,name,age) values(3,'索超',45);
insert into emp0519(id,name,age) values(4,'秦明',55);

END

posted @ 2022-05-20 21:10  逆火狂飙  阅读(1890)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东