在SpringBoot中创建本机SQL查询而不创建实体类 [英] Create native SQL query without creating entity class in SpringBoot

查看:682
本文介绍了在SpringBoot中创建本机SQL查询而不创建实体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ORM的基本原理是与对象进行映射.但是,由于某种原因,我不想创建用于运行查询的对象.

The fundamental of ORM is mapping with the objects. But, for some reason, I don't want to create objects for running a query.

有什么方法可以在不创建实体(托管类)的情况下运行本机SQL查询?

Is there any way, in which without creating entities (managed classes), I can run a native SQL query?

推荐答案

是.你可以.

在存储库类中使用特定查询(本机查询)创建方法:

Create a method in the repository class with specific query (native query):

@Query(value="select * from emp", nativeQuery=true)
Object getAllFromEmp();

将此方法保留在存储库界面中,然后从服务类中调用它

Keep this method in the repository interface and call it from the service class

或者您可以如下使用 EntityManager 对象

Or you can use EntityManager object as below

Query q = entityManager.createNativeQuery("SELECT * FROM emp e");
List<Object[]> empObject= q.getResultList();

这篇关于在SpringBoot中创建本机SQL查询而不创建实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆