我将创建一个用于访问数据库(MySQL(的Java控制台应用程序。我将使用Spring Boot/Spring Data JPA。使用Spring Boot创建控制台应用程序的正确方法是什么?
我找到了几种方法:
spring.main.web-application-type=NONE
(在application.properties中(spring.main.web-environment = false
(在application.properties中(- 使用Spring Shell项目
- 实现
CommandLineRunner
接口
我想其中一些可能已经过时了,有优点也有缺点。你能解释一下如何使用SpringBoot/SpringData创建一个普通控制台应用程序吗?
最近,我完成了一个控制台应用程序,您现在需要它。我通过实现CommandLineRunner
接口做到了这一点。当spring-boot启动应用程序时,它将调用CommandLineRunner
接口的run(String... args)
方法。因此,您可以在这个实现类中自动连接(或使用构造函数注入(spring数据存储库(例如AppRunner(&调用数据库操作。
我已经使用了MongoDB和一些缓存操作,而不是MySql数据库。
示例:
AppRunner.java
package com.cache.caching;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(AppRunner.class);
BookRepository bookRepository;
public AppRunner(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@Override
public void run(String... args) throws Exception {
logger.info("articles fetching..."+bookRepository.getArticles());
logger.info("articles fetching..."+bookRepository.getArticles());
logger.info("articles fetching..."+bookRepository.getArticles());
logger.info("articles fetching..."+bookRepository.getArticles());
}
}
BookRepository.java
package com.cache.caching;
import java.net.UnknownHostException;
import java.util.List;
public interface BookRepository {
List<Article> getArticles() throws UnknownHostException;
}
BookRepositoryImpl.java
package com.cache.caching;
import com.mongodb.*;
import org.bson.codecs.pojo.annotations.BsonId;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.MongoCollectionUtils;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
@Component
public class BookRepositoryImpl implements BookRepository{
@Override
@Cacheable("articles")
public List<Article> getArticles() throws UnknownHostException {
MongoClient mongoClient
= new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
DB db = mongoClient.getDB("Mart");
DBCollection collection = db.getCollection("articles");
DBCursor cursor = collection.find();
List<Article> list= new ArrayList<>();
while (cursor.hasNext()) {
Article article = new Article();
DBObject dbObject = cursor.next();
article.setId((Double) dbObject.get("_id"));
article.setSubject((String) dbObject.get("subject"));
list.add(article);
}
return list;
}
}
在您的情况下,您可以在application.yml/application.properties文件中提供MySQL数据库连接的详细信息。
CachingApplication.java-应用程序从这里开始,这个SpringApplication.run(CachingApplication.class, args);
调用CommandLineRunner
接口的run(String... args)
方法。
package com.cache.caching;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class CachingApplication {
public static void main(String[] args) {
SpringApplication.run(CachingApplication.class, args);
}
}
示例:此处为示例完整示例