我有许多简单的对象类型需要持久化到数据库.我正在使用Spring JPA来管理这种持久性.对于每个对象类型,我需要构建以下内容:
import org.springframework.data.jpa.repository.JpaRepository;
public interface FacilityRepository extends JpaRepository<Facility, Long> {
}
public interface FacilityService {
public Facility create(Facility facility);
}
@Service
public class FacilityServiceImpl implements FacilityService {
@Resource
private FacilityRepository countryRepository;
@Transactional
public Facility create(Facility facility) {
Facility created = facility;
return facilityRepository.save(created);
}
}
Run Code Online (Sandbox Code Playgroud)
我想到有可能用三个基于泛型的类替换每个对象类型的多个类,从而节省了大量的样板编码.我不确定如何去做,事实上如果这是一个好主意?
我有一个由一组微服务构建的应用程序.一个服务接收数据,通过Spring JPA和Eclipse链接持久保存,然后向第二个服务发送警报(AMQP).
根据特定条件,第二个服务然后针对持久数据调用RESTfull Web服务以检索保存的信息.
我注意到有时RESTfull服务返回一个空数据集,即使数据先前已保存过.查看持久服务的代码,已使用save而不是saveandflush,因此我假设数据没有快速刷新以供下游服务查询.
我应该说原始的持久性函数包含在内 @Transactional
我有一个Spring应用程序,它使用持久存储在数据库中的各种配置参数.为了最大限度地减少数据库访问周期,我创建了一个Singleton类,它将参数保存在Properties对象中.有时在运行应用程序期间需要刷新Properties对象,因此为此我有一个load()方法和一个reload()方法.为了访问数据库,我有一个@Autowired服务对象.简化一点:
public class AppConfig {
private static AppConfig instance = null;
private Properties appProperties;
@Autowired ConfiguraitonService configService;
protected AppConfig() {
load();
}
public static AppConfig getInstance() {
if (instance == null) {
instance = new AppConfig();
}
return instance;
}
public void reload() {
load();
}
private void load() {
List<Configuration> configList configService.findAll()
for (Configuration myConfiguration : configList) {
if (myConfiguration != null && myConfiguration.getAttribute() != null) {
appProperties.setProperty(myConfiguration.getAttribute(),myConfiguration.getValue());
}
}
}
public String getValue(String key) {
return appProperties.getProperty(key);
} …
Run Code Online (Sandbox Code Playgroud) 我试图了解用于处理一系列消息的最合适的(Java)设计模式.每条消息都包含一个"类型",用于确定如何处理消息中包含的数据.
我一直在考虑Command模式,但正在努力理解特定Command类的角色/相关性.到目前为止,我已经确定接收器将包含实现消息处理方法的代码.具体命令将根据消息类型进行实例化.但是,我不知道应该如何传递实际的消息数据.它是否应该通过具体的命令执行方法调用适当的接收方法传递给接收方构造函数?也许消息数据应该在接收者动作方法调用中传递?
我对所有这些都相当新,所以任何指导都将不胜感激.
这可能有所帮助:
public interface Command {
public void execute(String msg);
}
public class AO1Command implements Command {
Receiver rec = new Receiver();
public void execute(String msg) {
rec.admit(msg);
}
}
public class CommandFactory {
public protected CommandFactory () { }
public static Command getInstance(String type) {
if (type.equals("A01")) return new A01Command();
else if (type.equals("A02")) return new A02Command();
else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud) 我一直在尝试构建一个安全的REST端点来保存(并返回)一个person对象.到目前为止,我有一个方法如下:
@RequestMapping(value = "/save/", method = RequestMethod.POST)
public Person save(@RequestBody Person person) {
return repository.save(person);
}
Run Code Online (Sandbox Code Playgroud)
我一直在尝试使用Postman测试此端点,但似乎无法以正确的方式构建URL.我应该说我已经成功测试了find(Long id)(/ find/{id})和findall.
http://localhost:8080/api/save?person={"id":2,"first":"Brian","last":"Smith"}
Run Code Online (Sandbox Code Playgroud)
首先,这是构建端点以保存对象的正确方法吗?Postman结构是否正确?
我有一个消息结构,需要循环遍历多个 PID.3 段,选择一个 PID.3.5 == 'MR' 的段,然后用标识符替换 PID.3.4。我了解如何循环访问多个段(例如 OBX),但不了解子段。我有一些示例代码(不正确)作为开始。任何指导表示赞赏。
var pid = msg.PID;
for each (pid3 in pid[PID.3]) {
if (pid3[PID.3.5] == 'MR') {
pid3[PID.3.4] = 'IDENTIFIER';
};
};
Run Code Online (Sandbox Code Playgroud) 我们有一个基于 SQLite 的简单应用程序的两个副本。该应用程序有 10 个表,表之间有各种关系。我们希望将数据库合并到具有相同架构的单个 Postgres 数据库。我们可以使用 Talend 来实现这一点,但问题是会出现重复的键(因为两个源数据库都是独立的)。是否有一种系统方法可以让我们使用原始键加上加载第一个数据库产生的偏移量将数据插入到 Postgres 中?
我刚刚开始将旧的 MySQL/Spring/Eclipselink 项目移植到 MariaDB。我在创建表时遇到一个问题,可以如下所示:
MariaDB [spasm]> CREATE TABLE Configuration (ID BIGINT NOT NULL, Attribute VARCHAR(190) NOT NULL UNIQUE, Value VARCHAR(255) NOT NULL, PRIMARY KEY (ID));
Query OK, 0 rows affected (0.07 sec)
MariaDB [spasm]> drop table Configuration;
Query OK, 0 rows affected (0.06 sec)
MariaDB [spasm]> CREATE TABLE Configuration (ID BIGINT NOT NULL, Attribute VARCHAR(255) NOT NULL UNIQUE, Value VARCHAR(255) NOT NULL, PRIMARY KEY (ID));
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
MariaDB …
Run Code Online (Sandbox Code Playgroud) 这是来自另一个StackOverflow问题的代码段:
@Override
public String convertToDatabaseColumn(final UUID entityValue) {
return ofNullable(entityValue).map(entityUuid -> entityUuid.toString()).orElse(null);
}
Run Code Online (Sandbox Code Playgroud)
我真的很难理解Optional类的使用.返回代码是"返回映射的值(String),还是NULL,如果失败?
如何返回作用于方法而不是类 - 即Optional.ofNullable()?
我的 Spring AMQP 应用程序在启动时记录了以下异常:
org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Failed to invoke target method 'receiveMessage' with argument type = [class [B], value = [{[B@660cff44}]
Run Code Online (Sandbox Code Playgroud)
从我的搜索中我了解到这是因为类与消息类型不兼容?但是,我看不到这是哪里。
以下是相关的代码段:
@Bean
public MessageConverter jsonMessageConverter(){
return new Jackson2JsonMessageConverter();
}
@Bean
Queue queue() {
return new Queue(config.getAMQPResultsQueue(), false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange(config.getAMQPResultsExchange());
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("#");
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(config.getAMQPResultsQueue());
container.setMessageListener(listenerAdapter);
container.setMessageConverter(jsonMessageConverter());
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) …
Run Code Online (Sandbox Code Playgroud) 我有以下结构:
public interface MyService{
public void someMethod();
}
@Service
public class MyServiceImplA implements MyService {
public void someMethod() {
return;
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我这样做:
@Autowired
MyServiceImplA myService;
myService.somemethod();
Run Code Online (Sandbox Code Playgroud)
我不确定我应该如何在这里使用界面?如果我有第二个实现,比如MyServiceImplB,会发生什么?我可以通过某种配置来设置它(我正在使用Java Config)
spring ×6
java ×5
eclipselink ×2
hl7 ×1
javascript ×1
jpa ×1
mariadb ×1
mirth ×1
mysql ×1
postgresql ×1
postman ×1
rabbitmq ×1
rest ×1
spring-amqp ×1
spring-data ×1
spring-mvc ×1
sqlite ×1
talend ×1