JDBC动态搜索查询

时间:2015-02-05 18:40:35

标签: java jdbc

我有一个 dao图层

  public class UserDao {

    private static String GET_USERS_DOCTORS_FROM_BERLIN = "SELECT * FROM users where occupation = 'doctor' and city = 'berlin'";
    private static String GET_USERS_ENGINEERS_AGE_30 = "SELECT * FROM users where occupation = 'engineer' AND age = 30";

    JdbcTemplate jdbcTemplate;

    public List getDoctors() {
        return jdbcTemplate.query(GET_USERS_DOCTORS_FROM_BERLIN, new RowMapper<Object>() {
            @Override
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                .....;
            }
        });
    }

    public List getEngineers() {
        return jdbcTemplate.query(GET_USERS_ENGINEERS_AGE_30, new RowMapper<Object>() {
            @Override
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                .....;
            }
        });
    }

    }

服务层

 public class UserService {

    public UserDao userDao;

    public List getDoctors() {
        return userDao.getDoctors();
    }

    public List getEngineers() {
        return userDao.getEngineers();
    }

    }

我想要的是这样的:

  public class UserDao {

    JdbcTemplate jdbcTemplate;

    public List getUsers(/* criteria object */Criteria criteria) {
        ....
    }

    }

这样我就可以从服务层使用它了。我正在寻找有关如何实现这一点的最佳实践。

0 个答案:

没有答案