java JDBC之BasicDao--Java笔记

目录

BasicDao

相关的类


BasicDao

①.专门和数据交互(对数据库进行CRUD操作)

②.一张表对应一个 Dao

③.JavaBean == domain == pojo

        BasicDao bd = new BasicDao<cc>();

        String sql = "select * from cc";
        List<cc> i = bd.DaoList(sql, cc.class);
        for (cc cc : i) {
            System.out.println(cc);
        }
        sql = "update cc set num = 22 where id = 1";
        bd.update(sql);

        sql = "select * from cc where id =?";
        System.out.println();
        System.out.println(bd.DaoSingeList(sql, cc.class, 1));

        sql = "select nname from cc where num =?";
        System.out.println();
        System.out.println(bd.DaoSinge(sql, 22));

 输出:

id=1, nname=a, num=20
id=3, nname=c, num=0
id=4, nname=d, num=100
id=5, nname=e, num=666
id=2, nname=b, num=-10
id=6, nname=f, num=1

id=1, nname=a, num=22

a

相关的类

import java.sql.Connection;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

public class BasicDao<T> {
    private QueryRunner qr = new QueryRunner();

    // 增删改
    public int update(String sql, Object... parameters) {
        Connection c = null;
        try {
            c = JdbcutilsDruid.getConnection();
            return qr.update(c, sql, parameters);

        } catch (Exception e) {
            // TODO: handle exception
            throw new RuntimeException(e);
        } finally {
            JdbcutilsDruid.clo(null, null, c);
        }
    }

    // 返回多行数据(一个List)
    public List<T> DaoList(String sql, Class<T> cls, Object... parameters) {
        Connection c = null;
        try {
            c = JdbcutilsDruid.getConnection();
            return qr.query(c, sql, new BeanListHandler<T>(cls), parameters);

        } catch (Exception e) {
            // TODO: handle exception
            throw new RuntimeException(e);
        } finally {
            JdbcutilsDruid.clo(null, null, c);
        }
    }

    // 返回单行数据(一个对应Bean对象)
    public T DaoSingeList(String sql, Class<T> cls, Object... parameters) {
        Connection c = null;
        try {
            c = JdbcutilsDruid.getConnection();
            return qr.query(c, sql, new BeanHandler<T>(cls), parameters);

        } catch (Exception e) {
            // TODO: handle exception
            throw new RuntimeException(e);
        } finally {
            JdbcutilsDruid.clo(null, null, c);
        }
    }

    // 返回单行单个数据(一个 object)
    public Object DaoSinge(String sql, Object... parameters) {
        Connection c = null;
        try {
            c = JdbcutilsDruid.getConnection();
            return qr.query(c, sql, new ScalarHandler<T>(), parameters);

        } catch (Exception e) {
            // TODO: handle exception
            throw new RuntimeException(e);
        } finally {
            JdbcutilsDruid.clo(null, null, c);
        }
    }
}

上一篇:枚举类型转为Map使用


下一篇:Spring Boot WebFlux增删改查样例