Java Eclipse:如何防止格式化多行连接字符串?
我在Eclipse中遇到了这个问题,我正在编写一个JPQL查询,Eclipse一直将连接格式化到同一行。例如,这就是我想要的:Java Eclipse:如何防止格式化多行连接字符串?,java,eclipse,Java,Eclipse,我在Eclipse中遇到了这个问题,我正在编写一个JPQL查询,Eclipse一直将连接格式化到同一行。例如,这就是我想要的: @Query( "SELECT p " + "FROM Person p " + "LEFT JOIN FETCH p.Address a " + "WHERE p.id = :personId "
@Query(
"SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
public List<Person> findPerson(@Param("personId") Long personId);
有人知道如何预防吗?我使用的是Java 11,因此文本块不是这里的选项您可以尝试以下方法之一
public class QueryFormat {
// Add padding in line end
// Good: No need to edit formatting profile
// Bad: Decrease readability and need to do manual work.
@Query("SELECT p " /* */
+ "FROM Person p " /* */
+ "LEFT JOIN FETCH p.Address a " /* */
+ "WHERE p.id = :personId " /* */
+ "ORDER BY p.id DESC")
public List<Person> findPerson1(@Param("personId") Long personId) {
return Collections.emptyList();
}
// Need to enable "Off/On Tags" -> check "Enable Off/On Tags"
// Good: Will not affect existing formatting
// Bad: Need to format space, indentation yourself
// @formatter:off
@Query("SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
// @formatter:on
public List<Person> findPerson2(@Param("personId") Long personId) {
return Collections.emptyList();
}
// Code Style > Formatter > Edit > Line Wrapping tab > Check "Never Join already
// wrapped line (suggested by Christian Baumann)"
// Good: Seems to be best out of three, preserve formatting while line will not be
// joined
// Bad: May change existing formatting
@Query("SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
public List<Person> findPerson3(@Param("personId") Long personId) {
return Collections.emptyList();
}
}
这方面存在一个公开的bug: 对此的一个评论是 您可以禁用连接已格式化的行代码样式>格式化程序>编辑>换行选项卡 只是 编辑非内置配置文件或添加新配置文件。 启用关闭/打开标记 注意:冒号@formatter:on/off后面没有空格
有一个关于此的开放错误:。关于这一点的一条评论是,您可以禁用连接已格式化的行代码样式>格式化程序>编辑>换行选项卡。如果没有其他内容,请尝试在行尾添加//注释。@ChristianBaumann谢谢!这似乎对我起了作用
public class QueryFormat {
// Add padding in line end
// Good: No need to edit formatting profile
// Bad: Decrease readability and need to do manual work.
@Query("SELECT p " /* */
+ "FROM Person p " /* */
+ "LEFT JOIN FETCH p.Address a " /* */
+ "WHERE p.id = :personId " /* */
+ "ORDER BY p.id DESC")
public List<Person> findPerson1(@Param("personId") Long personId) {
return Collections.emptyList();
}
// Need to enable "Off/On Tags" -> check "Enable Off/On Tags"
// Good: Will not affect existing formatting
// Bad: Need to format space, indentation yourself
// @formatter:off
@Query("SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
// @formatter:on
public List<Person> findPerson2(@Param("personId") Long personId) {
return Collections.emptyList();
}
// Code Style > Formatter > Edit > Line Wrapping tab > Check "Never Join already
// wrapped line (suggested by Christian Baumann)"
// Good: Seems to be best out of three, preserve formatting while line will not be
// joined
// Bad: May change existing formatting
@Query("SELECT p "
+ "FROM Person p "
+ "LEFT JOIN FETCH p.Address a "
+ "WHERE p.id = :personId "
+ "ORDER BY p.id DESC")
public List<Person> findPerson3(@Param("personId") Long personId) {
return Collections.emptyList();
}
}