Article From:https://www.cnblogs.com/zxq-Study-Java/p/9970594.html

1  First, the pom. XML file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.itcast.springboot111605</groupId>
<artifactId>springboot111605</artifactId>
<version>1.0-SNAPSHOT</version>

<!-- All springboot projects are based on the spring parent project - & gt;
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>

<!--webRelevant dependencies - gt;
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Druid Connection Pool & gt;
<dependency>
<groupId>com.github.drtrang</groupId>
<artifactId>druid-spring-boot2-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>

</project>

2  Then there's the only configuration file in spring boot, application. properties, in the resource directory, which contains the configuration information for database connections, and all the other configuration queries are in it.
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/travel
jdbc.username=root
jdbc.password=123456

3  Create HelloController classes in the controller folder
@RestController
public class HelloController01 {


@GetMapping("hello")
public String test(){
return "hello world";
}

}

4  Add a boot class TestApplication to the upper directory of the controller folder
@SpringBootApplication
public class TestApplication {

public static void main(String[] args) {

SpringApplication.run(TestApplication.class,args);
}

}

5  Start the main method to access localhost: 8080/hello

6  The method of establishing database connection and establishing a JdbcConfiguration class
@Configuration
public class JdbcConfiguration {
@Bean
// To declare the prefix of the attribute to be injected, SpringBook automatically injects the relevant attribute into DataSource through the set method.
@ConfigurationProperties(prefix ="jdbc")
public DataSource dataSource(){

DruidDataSource wwdataSource=new DruidDataSource();
return wwdataSource;
}
}
In this class, the method name must be dataSource, and the return value or return type can be changed, but the method name must not be changed.
  

Leave a Reply

Your email address will not be published. Required fields are marked *