我的spring-boot项目中有一个属性类.
@Component
@ConfigurationProperties(prefix = "myprefix")
public class MyProperties {
private String property1;
private String property2;
// getter/setter
}
Run Code Online (Sandbox Code Playgroud)
现在,我想将默认值设置为application.properties文件中的其他属性property1.类似于以下示例使用@Value
@Value("${myprefix.property1:${somepropety}}")
private String property1;
Run Code Online (Sandbox Code Playgroud)
我知道我们可以像下面的示例一样分配静态值,其中"默认值"被指定为默认值property,
@Component
@ConfigurationProperties(prefix = "myprefix")
public class MyProperties {
private String property1 = "default value"; // if it's static value
private String property2;
// getter/setter
}
Run Code Online (Sandbox Code Playgroud)
如何在spring boot中使用@ConfigurationProperties类(而不是类型安全的配置属性)来执行此操作,其中我的默认值是另一个属性?
我是Redis的新手,想要用我现有的spring应用程序实现它.
我的问题是使用不同的redisTemplate和相同的键来存储不同类型的值.
例如
我在spring中定义了redisTemplate1和redisTemplate2 bean,就像.
<bean id="redisTemplate1" class ="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref ="connectionFactory" />
<bean id="redisTemplate2" class ="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref ="connectionFactory" />
Run Code Online (Sandbox Code Playgroud)
在我的服务的java文件中,我使用这两个redis模板创建了两个不同的数据结构.
@Autowired
@Qualifier(value = "redisTemplate1")
private RedisTemplate<String, Student> redisTemplate1;
@Autowired
@Qualifier(value = "redisTemplate2")
private RedisTemplate<String, Address> redisTemplate2;
Run Code Online (Sandbox Code Playgroud)
并且,使用以下模式来存储数据.
redisTemplate1.opsForHash().put("KEY1", student.getId(), student);
redisTemplate2.opsForHash().put("KEY1", address.getId(), address);
Run Code Online (Sandbox Code Playgroud)
情况是,我有每个表以1开头的主键.所以1是学生和地址的主键.
我正在使用下面的行让学生从数据存储中回来.
(Student) redisTemplate1.opsForHash().get("KEY1", 1);
Run Code Online (Sandbox Code Playgroud)
但是,不幸的是它产生了一个例外.
java.lang.ClassCastException: com.redis.model.Address cannot be cast to com.redis.model.Student
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是,
提前致谢.
我将MySQL用作应用程序的后端。
我想使用JPA条件查询执行以下查询。
SELECT *
FROM table1 t1
WHERE datediff(now(), t1.created) >= 20;
Run Code Online (Sandbox Code Playgroud)
注意:的类型created为TIMESTAMP
我想要条件查询,例如:
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
// select
CriteriaQuery<Table1> query = cb.createQuery(Table1.class);
// from
Root<Table1> root = query.from(Table1.class);
// where
List<Predicate> predicates = new ArrayList<Predicate>();
// TODO: add expression for datediff(now(), t1.created) >= 20
query.where(cb.and(predicates.toArray(new Predicate[0])));
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
提前致谢。
java ×2
criteria-api ×1
datediff ×1
jpa ×1
properties ×1
redis ×1
spring ×1
spring-boot ×1
spring-mvc ×1