我正在寻找自动postgres数据库连接时,作业被触发。我无法连接到postgres数据库并获取书目。
我试图从memberclassservice调用一个名为book\u tbl的postgres数据库表,其中包含两列id和name,如下所示
@Slf4j
@Service
public class MemberClassService {
private ClassRepository classRepository;
private MemberRepository memberRepository;
@Autowired
private BookRepository bookRepository;
Logger log = LoggerFactory.getLogger("SampleLogger");
public MemberClassService(ClassRepository classRepository, MemberRepository memberRepository,
BookRepository bookRepository) {
this.classRepository = classRepository;
this.memberRepository = memberRepository;
this.bookRepository = bookRepository;
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public void classStats() {
List<Book> booksList = bookRepository.findAll();
log.info("Books List sieze:::"+booksList.size());
Map<String, Integer> bookMap = booksList.stream()
.collect(Collectors.toMap(Book::getName, c -> 0));
}
}
我在application.properites文件中添加了以下属性
# Postgres db2
spring.book.datasource.jdbcUrl=jdbc:postgresql://localhost:5432/periphery_old
spring.book.datasource.username=postgres
spring.book.datasource.password=postgres
spring.book.datasource.driver-class-name=org.postgresql.Driver
我为它创建了以下类。
package com.gitlab.johnjvester.jpaspec.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import com.gitlab.johnjvester.jpaspec.web.model.Book;
@Repository
public interface BookRepository extends JpaRepository<Book, Integer>, JpaSpecificationExecutor {
}
package com.gitlab.johnjvester.jpaspec.repository;
import com.gitlab.johnjvester.jpaspec.domain.MemberClass;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.List;
public interface ClassRepository extends JpaRepository<MemberClass, Long>, JpaSpecificationExecutor {
List<MemberClass> findAllByNameContainsIgnoreCase(String searchString);
}
暂无答案!
目前还没有任何答案,快来回答吧!