0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Mapper2

Posted at

好的,以下是一个使用动态类和@SelectProvider的完整代码示例:

  1. 定义Mapper接口:
@Mapper
@Repository
public interface MyMapper {
    @SelectProvider(type = SqlProvider.class, method = "selectSql")
    List<Result> selectData(Map<String, Object> params);
}
  1. 定义SqlProvider类:
public class SqlProvider {
    public String selectSql(Map<String, Object> params) {
        String tableName = (String) params.get("tableName");
        String columnNames = (String) params.get("columnNames");

        String sql = String.format("SELECT %s FROM %s", columnNames, tableName);
        return sql;
    }
}
  1. 定义动态类ResultSetMapper:
public class ResultSetMapper<T> implements ResultSetHandler<List<T>> {
    private Class<T> clazz;

    public ResultSetMapper(Class<T> clazz) {
        this.clazz = clazz;
    }

    @Override
    public List<T> handle(ResultSet rs) throws SQLException {
        List<T> list = new ArrayList<>();
        ResultSetMetaData metaData = rs.getMetaData();
        int columnCount = metaData.getColumnCount();

        while (rs.next()) {
            T obj = null;
            try {
                obj = clazz.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }

            for (int i = 1; i <= columnCount; i++) {
                String columnName = metaData.getColumnLabel(i);
                Object columnValue = rs.getObject(i);
                setProperty(obj, columnName, columnValue);
            }

            list.add(obj);
        }

        return list;
    }

    private void setProperty(T obj, String propertyName, Object value) {
        try {
            Field field = obj.getClass().getDeclaredField(propertyName);
            field.setAccessible(true);
            field.set(obj, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  1. 调用Mapper接口方法:
@Autowired
private MyMapper myMapper;

public List<Result> selectData() {
    Map<String, Object> params = new HashMap<>();
    params.put("tableName", "my_table");
    params.put("columnNames", "column1, column2, column3");

    ResultSetMapper<Result> mapper = new ResultSetMapper<>(Result.class);
    return myMapper.selectData(params).stream().flatMap(Collection::stream).map(mapper::handle).collect(Collectors.toList());
}

在这个例子中,使用了@SelectProvider注解来指定SQL查询语句。SqlProvider类的selectSql方法返回SQL查询语句字符串。在Mapper接口方法中,调用了动态类ResultSetMapper来将查询结果映射成List。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?