我需要从属性文件中加载存储库中的查询。例如,这里:
@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
@Query(value="SELECT * FROM student where year= :le", native=true)
public List<Student> getStudentsByLevel(@Param("le") int level);
}
我需要从属性文件中加载"SELECT * FROM student where year= :le"
字符串。有没有办法做到这一点?
答案 0 :(得分:0)
如上所述,我通过使用xml配置的命名查询克服了这个问题。以下是示例。
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="hibernatePersistenceUnit">
<!-- <mapping-file>META-INF/jpql/oracle-quries.xml</mapping-file> -->
<mapping-file>META-INF/jpql/mysql-quries.xml</mapping-file>
</persistence-unit>
</persistence>
的MySQL-quries.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0"
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 ">
<named-native-query name="Nodecontrol.lockNodeController">
<query>
SELECT * FROM student where year= :le
</query>
</named-native-query>
</entity-mappings>
在oracle-quries.xml和mysql-quries.xml中,我分别定义了所有本机查询。当我需要更改查询而不更改类文件时,我只需要提到我需要在persistence.xml中使用的文件名。