我一直在寻找各处:stackoverflow,automapper文档,互联网,只是找不到这个的任何信息,即使这似乎是一个非常普遍的问题.
我的映射:
CreateMap<StatusLevelDTO, StatusLevel>()
.ForAllMembers(opt => opt.Condition(src => src != null));
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为src表示源对象(StatusLevelDTO),而不是源属性(我认为).
更具体地说,如果我将ObjectA映射到ObjectB,ObjectA.SomeValue为null而ObjectB.SomeValue为2,我希望目标对象保持其值(2).
我已经看到了这个问题:Automapper使用自定义解析器跳过空值并尝试了前两个答案,但它们似乎都已过时版本6.
有没有办法在Automapper 6中实现这一点?我准确地使用6.0.2.
我一直在想我应该使用哪种投影,所以我做了一点测试,其中包括5种类型的投影(基于文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#project):
1.实体投射
这只是findAll()Spring Data存储库提供的标准.没什么好看的.
服务:
List<SampleEntity> projections = sampleRepository.findAll();
Run Code Online (Sandbox Code Playgroud)
实体:
@Entity
@Table(name = "SAMPLE_ENTITIES")
public class SampleEntity {
@Id
private Long id;
private String name;
private String city;
private Integer age;
}
Run Code Online (Sandbox Code Playgroud)
2.构造函数投影
服务:
List<NameOnlyDTO> projections = sampleRepository.findAllNameOnlyConstructorProjection();
Run Code Online (Sandbox Code Playgroud)
库:
@Query("select new path.to.dto.NameOnlyDTO(e.name) from SampleEntity e")
List<NameOnlyDTO> findAllNameOnlyConstructorProjection();
Run Code Online (Sandbox Code Playgroud)
数据传输对象:
@NoArgsConstructor
@AllArgsConstructor
public class NameOnlyDTO {
private String name;
}
Run Code Online (Sandbox Code Playgroud)
3.界面投影
服务:
List<NameOnly> projections = sampleRepository.findAllNameOnlyBy();
Run Code Online (Sandbox Code Playgroud)
库:
List<NameOnly> findAllNameOnlyBy();
Run Code Online (Sandbox Code Playgroud)
接口:
public interface NameOnly {
String getName(); …Run Code Online (Sandbox Code Playgroud) 正如标题所说,我只能补充一点,如果我在html文件中手动输入±,就可以了.
视图解析器:
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8"/>
<property name="order" value="1"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
pom.xml中:
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Run Code Online (Sandbox Code Playgroud)
html输入的示例:
<h2><p th:text="#{homepage.greeting}">Welcome</p></h2>
Run Code Online (Sandbox Code Playgroud)
在html文件的标记内:
<meta charset="UTF-8"/>
Run Code Online (Sandbox Code Playgroud)
在IntelliJ Idea中,我将项目编码设置为UTF-8,属性文件的默认编码为UTF-8

我老实说不知道问题出在哪里.当我将语言环境更改为pl时,这是输出:

对不起,我还不能发布图片.任何帮助将不胜感激.
在web.xml中试过这个过滤器,仍然没有运气.
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud) 我有以下界面:
public interface UserRepository<T extends User> {
List<T> findAll(UserCriteria userCriteria, PageDetails pageDetails);
T findByEmail(String email);
}
Run Code Online (Sandbox Code Playgroud)
它的实施:
@Repository
public class JpaUserRepository implements UserRepository<JpaUser> {
public List<JpaUser> findAll(UserCriteria userCriteria, PageDetails pageDetails) {
//implementation
}
public JpaUser findByEmail(String email) {
//implementation
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我打电话:
User user = userRepository.findByEmail(email);
Run Code Online (Sandbox Code Playgroud)
在我的服务课上,一切都很好.
但是当我打电话时:
List<User> users = userRepository.findAll(userCriteria, pageDetails);
Run Code Online (Sandbox Code Playgroud)
我得到未经检查的赋值警告,其原因是userRepository具有原始类型,因此findAll的结果将被删除.如果确实如此,不应该findByEmail表现一样吗?它似乎不太一致.
如何在这种情况下消除原始类型?我尝试过几件事:
<T extends User>从界面中删除并将其应用于以下方法:
<U extends User> List<U> findAll(UserCriteria userCriteria, PageDetails pageDetails);
Run Code Online (Sandbox Code Playgroud)
这对于服务工作正常,但是存储库实现现在会发出关于未经检查的覆盖的警告(返回类型需要未经检查的转换).
我也尝试从接口和方法中删除泛型,同时保持返回列表的通用性:
List<? extends User> findAll(UserCriteria userCriteria, PageDetails pageDetails);
Run Code Online (Sandbox Code Playgroud)
这解决了问题,没有警告,但要求我写这样的服务:
List<? …Run Code Online (Sandbox Code Playgroud) 是否可以从 Elastic Search 获取当前日期和时间(最好在 Java API 中)?
我已经到处搜索了相当长一段时间,但只发现范围查询可以对某些日期字段执行数学运算,并且可以使用脚本字段,但我找不到任何用于返回当前日期的脚本。我所要求的只是类似于 SQL 的 now() 的东西。
谢谢你的时间。
我的查询如下所示:
@Query("SELECT p FROM Pilot p LEFT JOIN FETCH p.playerShips WHERE p.nickname = (:nickname)")
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.即使playerShips为空,我也会获得Pilot实例.
现在,我想只获取不活动的船只,所以我修改了我的查询,如下所示:
@Query("SELECT p FROM Pilot p LEFT JOIN FETCH p.playerShips s WHERE p.nickname = (:nickname) AND s.active = false")
Run Code Online (Sandbox Code Playgroud)
我作为一名飞行员得到了空,所以它显然不起作用.
如果有人能解释如何使用适用于子元素的WHERE子句创建JOIN FETCH查询,我会很高兴.提前致谢.
我想这是某种依赖性问题,所以这是我的树:
[INFO] Sikor:BrowserGame:war:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] +- org.springframework:spring-context:jar:4.1.1.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:4.1.1.RELEASE:compile
[INFO] | +- org.springframework:spring-beans:jar:4.1.1.RELEASE:compile
[INFO] | +- org.springframework:spring-core:jar:4.1.1.RELEASE:compile
[INFO] | | \- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] | \- org.springframework:spring-expression:jar:4.1.1.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:4.1.1.RELEASE:compile
[INFO] | \- org.springframework:spring-web:jar:4.1.1.RELEASE:compile
[INFO] +- org.springframework:spring-context-support:jar:4.1.1.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-web:jar:3.2.5.RELEASE:compile
[INFO] | +- aopalliance:aopalliance:jar:1.0:compile
[INFO] | \- org.springframework.security:spring-security-core:jar:3.2.5.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-config:jar:3.2.5.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-taglibs:jar:3.2.5.RELEASE:compile
[INFO] | \- org.springframework.security:spring-security-acl:jar:3.2.5.RELEASE:compile
[INFO] +- org.thymeleaf:thymeleaf-spring4:jar:2.1.3.RELEASE:compile
[INFO] | \- org.thymeleaf:thymeleaf:jar:2.1.3.RELEASE:compile
[INFO] | +- ognl:ognl:jar:3.0.6:compile
[INFO] | \- org.unbescape:unbescape:jar:1.0:compile
[INFO] +- org.thymeleaf.extras:thymeleaf-extras-springsecurity3:jar:2.1.1.RELEASE:compile
[INFO] …Run Code Online (Sandbox Code Playgroud) 基本上,我试图获得一个与另一个实体具有LAZY关系的实体。以下是我尝试过的两件事。第一个起作用,第二个不起作用,我不明白为什么。我想要的只是从数据库获取实体。之所以将其放在其他方法中,是因为我不希望第一个使用@Transactional,因为它可能需要一些时间才能执行。请注意,在第一种方法中,我并没有保存甚至访问数据库,我只需要从db中读取一次即可。
方法1(按预期工作):
@Override
@Transactional
public void sendEmailToUser(Long exhibitorId) {
EmailExhibitorTO exhibitorTO = findExhibitorById(exhibitorId);
}
private EmailExhibitorTO findExhibitorById(Long id){
return converter.convert(exhibitorRepository.findById(id), EmailExhibitorTO.class);
}
Run Code Online (Sandbox Code Playgroud)
这里的一切都很好,我也得到了实体和惰性初始化的实体。
方法2(无效):
@Override
public void sendEmailToUser(Long exhibitorId) {
EmailExhibitorTO exhibitorTO = findExhibitorById(exhibitorId);
}
@Transactional
private EmailExhibitorTO findExhibitorById(Long id){
return converter.convert(exhibitorRepository.findById(id), EmailExhibitorTO.class);
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用。错误:
有一个映射异常,但这是因为无法获取惰性实体。
我可能只是愚蠢,但如果有我不明白的地方,请解释。
提前致谢。
当我有test.js文件并导入脚本时:
<script defer="defer" src="/js/test.js"></script>
Run Code Online (Sandbox Code Playgroud)
一切正常......但如果我将上面的行更改为html:
<script defer="defer">
$(document).ready(function () {
$(".overviewPilotPortraitImage").click(function () {
alert("Hello world!");
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
它不再起作用了.test.js包含与上面完全相同的脚本,只是没有脚本标记.
是的,我确定它完全一样.
带有test.js文件的头标记 - 工作:
<head>
<title>Placeholder - Overview</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<script defer="defer" src="/js/jquery-1.11.1.min.js"></script>
<script defer="defer" src="/js/bootstrap.min.js"></script>
<script defer="defer" src="/js/test.js"></script>
</head>
Run Code Online (Sandbox Code Playgroud)
带有html脚本的头标记 - 不工作:
<head>
<title>Placeholder - Overview</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="/css/style.css" /> …Run Code Online (Sandbox Code Playgroud) java ×6
spring ×5
automapper ×1
c# ×1
date ×1
generics ×1
hibernate ×1
javascript ×1
jquery ×1
mapping ×1
performance ×1
projection ×1
response ×1
servlets ×1
spring-data ×1
sql ×1
thymeleaf ×1
transactions ×1
utf-8 ×1
xhtml ×1