如何使用spring boot在xml中定义命名本机查询

时间:2017-10-25 02:54:00

标签: java hibernate jpa spring-boot

我正在使用spring boot,我想在xml文件中定义本机查询。 这是实体:

@NamedNativeQuery(name = "findAllUsers",query = "select * from user")
@Entity
@Table(name="user")
public class User implements Serializable {
    @Id
    @GeneratedValue
    @Column(name = "UserId")
    private Integer userId;
}

和存储库:

@Repository
public interface UserDao extends CrudRepository<User, Integer>, JpaSpecificationExecutor<User> {
    @Query(name="findAllUsers")
    List<User> findAllUsers();
}

我想在native-sql-oracle.xml和native-sql-mysql.xml之间快速切换本机sql。

1 个答案:

答案 0 :(得分:0)

只需在资源目录中添加 .hbm.xml 文件,它将在春季自动扫描。

示例xml:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0">
    <package>com.xxx.auth</package>
    <entity class="User">
        <named-native-query name="searchUser" result-class="User">
            <query>
                <![CDATA[
                SELECT * FROM User
                ]]>
            </query>
        </named-native-query>
    </entity>
</entity-mappings>