Рубрики
Без рубрики

Пружинный ботинок : JdbcTemplate

Пример настройки Spring Boot с помощью Jdbctemplate pom.xml <зависимость> <идентификатор группы... Помеченный java, springboot.

Пример настройки загрузки Spring с помощью шаблона JDBC

  • Пример настройки загрузки Spring с помощью шаблона

    org.springframework.boot
    spring-boot-starter-web

    
    org.springframework.boot
    spring-boot-starter-jdbc


    org.postgresql
    postgresql
    42.2.12


    org.projectlombok
    lombok
    1.18.12
    provided

пружинный загрузчик-стартер-jdbc и драйвер JDBC, в данном случае postgresql является обязательным.

spring.datasource.url=jdbc:postgresql://localhost/{DBNAME}
spring.datasource.username={USERNAME}
spring.datasource.password={PASSWORD}
spring.datasource.driver-class-name=org.postgresql.Driver

Нам часто не нужен spring.datasource.имя_класса драйвера , потому что Spring может определить это по url.

  • Нам часто не нужен
@Slf4j
@SpringBootApplication
public class App implements CommandLineRunner {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public static void main( String[] args ) {
        SpringApplication springApplication = new SpringApplication(App.class);
        springApplication.setWebApplicationType(WebApplicationType.NONE);
        springApplication.run(args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("Count total row = {}", 
            jdbcTemplate.queryForObject("SELECT COUNT(id) FROM customer", Integer.class));
    }
}

JdbcTemplate автоматически настраивается весной. Мы можем автоматически подключить его непосредственно к любому классу Spring bean.

Оригинал: “https://dev.to/mackittipat/spring-boot-jdbctemplate-2fl8”