====================== 查询指定用户的账户列表 ====================== 添加用于非空判断的包依赖: org.apache.commons commons-lang3 3.4 实现数据访问接口方法: // 针对t_account表的数据访问接口实现 @Repository public class AccountDao implements IAccountDao { @Resource // 注入JDBC模板 private JdbcTemplate jdbcTemplate; ... // 查询指定用户的账户列表 @Override public List queryAccounts( int userId, String accountName, String accountType, String createTime) { // 定义SQL语句 String sql = "select * from t_account where user_id = ?"; // 设置参数 List args = new ArrayList<>(); args.add(userId); if (StringUtils.isNotBlank(accountName)) { // 若账户名称非空 // 拼接SQL语句 sql += " and account_name like concat('%', ?, '%')"; // 模糊查询 // 追加参数 args.add(accountName); } if (StringUtils.isNotBlank(accountType)) { // 若账户类型非空 // 拼接SQL语句 sql += " and account_type = ?"; // 追加参数 args.add(accountType); } if (StringUtils.isNotBlank(createTime)) { // 若创建时间非空 // 拼接SQL语句 sql += " and create_time < ?"; // 范围查询 // 追加参数 args.add(createTime); } // 执行SQL语句 List accounts = jdbcTemplate.query( sql, args.toArray(), (ResultSet rs, int i) -> { Account acc = new Account( rs.getInt("account_id"), rs.getString("account_name"), rs.getString("account_type"), rs.getDouble("money"), rs.getString("remark"), rs.getTimestamp("create_time"), rs.getTimestamp("update_time"), rs.getInt("user_id")); return acc; }); // 返回指定用户的账户列表 return accounts; } ... } 编写测试用例: // 查询账户记录测试类 public class SpringJDBCTestQuery extends SpringJDBCTest { @Resource // 注入数据访问对象 private IAccountDao accountDao; ... // 测试:查询指定用户的账户列表 @Test public void testQueryAccounts() { // 查询指定用户的账户列表 List accounts = accountDao.queryAccounts( 1, null, null, null); System.out.println(getClass().getName() + "." + Thread .currentThread().getStackTrace()[1].getMethodName() + ": " + accounts); // 查询指定用户的账户列表 accounts = accountDao.queryAccounts( 1, "95588", null, null); System.out.println(getClass().getName() + "." + Thread .currentThread().getStackTrace()[1].getMethodName() + ": " + accounts); // 查询指定用户的账户列表 accounts = accountDao.queryAccounts( 1, "95588", "工商银行北京分行", null); System.out.println(getClass().getName() + "." + Thread .currentThread().getStackTrace()[1].getMethodName() + ": " + accounts); // 查询指定用户的账户列表 accounts = accountDao.queryAccounts( 1, "95588", null, "2022-01-13 12:40:00"); System.out.println(getClass().getName() + "." + Thread .currentThread().getStackTrace()[1].getMethodName() + ": " + accounts); } ... } 运行测试用例: cn.tedu.springjdbc.test.SpringJDBCTestQuery.testQueryAccounts: [Account{accountId=1, accountName='9558872419', accountType='工商银行北京分行', money=20000.0, remark='工资', createTime=2022-01-13 12:39:58.0, updateTime=2022-01-14 15:04:21.0, userId='1'}, Account{accountId=2, accountName='9558803625', accountType='工商银行上海分行', money=10000.0, remark='兼职', createTime=2022-01-13 12:41:55.0, updateTime=2022-01-14 15:04:44.0, userId='1'}, Account{accountId=6, accountName='9555527653', accountType='招商银行', money=5000.0, remark='奖金', createTime=2022-01-14 09:01:54.0, updateTime=2022-01-14 09:01:54.0, userId='1'}] cn.tedu.springjdbc.test.SpringJDBCTestQuery.testQueryAccounts: [Account{accountId=1, accountName='9558872419', accountType='工商银行北京分行', money=20000.0, remark='工资', createTime=2022-01-13 12:39:58.0, updateTime=2022-01-14 15:04:21.0, userId='1'}, Account{accountId=2, accountName='9558803625', accountType='工商银行上海分行', money=10000.0, remark='兼职', createTime=2022-01-13 12:41:55.0, updateTime=2022-01-14 15:04:44.0, userId='1'}] cn.tedu.springjdbc.test.SpringJDBCTestQuery.testQueryAccounts: [Account{accountId=1, accountName='9558872419', accountType='工商银行北京分行', money=20000.0, remark='工资', createTime=2022-01-13 12:39:58.0, updateTime=2022-01-14 15:04:21.0, userId='1'}] cn.tedu.springjdbc.test.SpringJDBCTestQuery.testQueryAccounts: [Account{accountId=1, accountName='9558872419', accountType='工商银行北京分行', money=20000.0, remark='工资', createTime=2022-01-13 12:39:58.0, updateTime=2022-01-14 15:04:21.0, userId='1'}] 看库。