Spring boot with mysql database example
pom.xml file
Modify auto created POM file. We need to add parent in pom to make it a spring boot application. After that add spring-boot-starter-thymeleaf dependency and java version.
4.0.0 w3spoint SpringBoot05 0.0.1-SNAPSHOT jar SpringBoot05 http://maven.apache.org 1.8 org.springframework.boot spring-boot-starter-parent 1.4.2.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat provided mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc junit junit 3.8.1 test org.springframework.boot spring-boot-maven-plugin
Now, right click on project -> click on Maven -> click on update project -> Popup window will open -> click Ok to update the project.
It is a initializer class which runs a SpringApplication.
App.java
package com.w3schools360; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class App extends SpringBootServletInitializer { public static void main( String[] args ) { SpringApplication.run(App.class, args); } }
JDBCController.java
package com.w3schools360; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class JDBCController { @Autowired JdbcTemplate jdbc; @RequestMapping("/insert") public String insertData(){ jdbc.execute("insert into users(seq,user_name)values(12,'hkumar')"); return"Data inserted Successfully"; } }
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test_schema?useSSL=false spring.datasource.username=root spring.datasource.password=**** spring.datasource.driver-class-name=com.mysql.jdbc.Driver