java - 使用 Java、Spring 和 Hibernate 将 MySQL View 转换为 CSV 文件

标签 java mysql hibernate spring orm

有人要求我从 MySQL 的 View 中输出一个 CSV 文件。我目前正在编写的应用程序使用 Spring 和 Hibernate 创建数据库,但 View 只是交给我。

Hibernate 对这个 View 一无所知,但我想做这样的事情:

public List<Object> getCsvView() {
  return (List<Object>) getHibernateTemplate().find("from myView");
}

我的猜测是我可以映射一个 native 查询,以便 hibernate 了解该 View 。当我阅读文档时,这有点棘手:

You can also map a native query[...]To achieve that, you need to describe the SQL resultset structure using @SqlResultSetMapping[...].

现在,我真的对映射结果的结构不感兴趣。我很高兴结构只是一堆对象。

此外,他们可能会随时更改此 View 。即使知道 View ,我也真的对我的应用不感兴趣。

那么,在 Spring/Hibernate 世界中是否有一种简单的方法可以做到这一点,或者我是否正在以困难的方式解决这个问题?

最佳答案

只需对 View 执行 native 查询,您将获得 Object[]List,结果为每列的标量值。来自文档:

16.1.1. Scalar queries

The most basic SQL query is to get a list of scalars (values).

sess.createSQLQuery("SELECT * FROM CATS").list();
sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").list();

These will return a List of Object arrays (Object[]) with scalar values for each column in the CATS table. Hibernate will use ResultSetMetadata to deduce the actual order and types of the returned scalar values.

To avoid the overhead of using ResultSetMetadata, or simply to be more explicit in what is returned, one can use addScalar():

sess.createSQLQuery("SELECT * FROM CATS")
 .addScalar("ID", Hibernate.LONG)
 .addScalar("NAME", Hibernate.STRING)
 .addScalar("BIRTHDATE", Hibernate.DATE)

This query specified:

  • the SQL query string
  • the columns and types to return

This will return Object arrays, but now it will not use ResultSetMetadata but will instead explicitly get the ID, NAME and BIRTHDATE column as respectively a Long, String and a Short from the underlying resultset. This also means that only these three columns will be returned, even though the query is using * and could return more than the three listed columns.

It is possible to leave out the type information for all or some of the scalars.

sess.createSQLQuery("SELECT * FROM CATS")
 .addScalar("ID", Hibernate.LONG)
 .addScalar("NAME")
 .addScalar("BIRTHDATE")

This is essentially the same query as before, but now ResultSetMetaData is used to determine the type of NAME and BIRTHDATE, where as the type of ID is explicitly specified.

How the java.sql.Types returned from ResultSetMetaData is mapped to Hibernate types is controlled by the Dialect. If a specific type is not mapped, or does not result in the expected type, it is possible to customize it via calls to registerHibernateType in the Dialect.

在对 View 一无所知的情况下生成 CVS 文件看起来很完美:)

关于java - 使用 Java、Spring 和 Hibernate 将 MySQL View 转换为 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3427369/

相关文章:

php - 在 phpmyadmin 中使用 json 数据填充 mysql 表

hibernate - 请求结束时 GORM 自动刷新而不调用保存

java - 致命[主要] org.apache.hadoop.mapreduce.v2.app.MRAppMaster:启动MRAppMaster java.lang.NoClassDefFoundError时出错

java - Spock Spring 通过构造函数注入(inject)模拟和 bean

java - http POST 格式问题 - 作为数组、列表?

java - 延迟加载内容

java - JPA创建模式后如何使组件初始化?

Java8 代码停止编译并让我进行不必要的显式转换

mysql - SQL查询速度慢

mysql - 从数字 1 开始列出查询本身的结果