YuFeng

BeanUtils工具SQL得相关使用
1~通过指定得列来生成一个集合package it.dao; import java.sql.SQLExcept...
扫描右侧二维码阅读全文
26
2019/06

BeanUtils工具SQL得相关使用

1~通过指定得列来生成一个集合

package it.dao;

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

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

import yufeng.it.utils.MyCp30Utils;

public class FindProductDao {
    public List<Object> findProduct(String pname) throws SQLException {
        QueryRunner qr = new QueryRunner(MyCp30Utils.getDataSource());
        //sql模糊查询语句,注意%是一个变量
        String sql = "SELECT * FROM `product` WHERE `pname` LIKE ?";
        //new ColumnListHandler("pname") 是通过指定得列来创建一个集合
        List<Object> query = qr.query(sql, new ColumnListHandler("pname"),"%"+pname +"%");
        return query;
    }
    
}

2~查询数据库中得条数

QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select count(*) from product";
        //new ScalarHandler() 获得的是一个Object类型强转成Long型
        Long query = (Long) runner.query(sql, new ScalarHandler());
        return query.intValue();
Last modification:June 26th, 2019 at 10:39 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment