Don't Learn Countless - Getting Started with SpringBoot VI

Keywords: Programming JDBC MySQL Spring SpringBoot

SpringBoot

1 Connect to the database

The Spring framework helps you connect to databases by connecting from JDBC to mapping elements using JdbcTemplate.For example, Hibernate and Spring Data provide higher-level functionality, create an implementation of Repository, and use methods in the interface and mapping of specific SQL from xml files to make calling Sql in java easier.

1.1 Configure DataSource

Java's javax.sql.DataSource is an interface to get a database Connection.Is standardized, a way to get connected.Of course we need to import the required jar packages before configuring DataSource

	compile('org.springframework.boot:spring-boot-starter-jdbc')
	compile 'mysql:mysql-connector-java:8.0.11'

1.1.1 Default Configuration

This method is very simple and requires only a few elements to be configured in application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xmall
    username: root
    password: *******
    driver-class-name: com.mysql.jdbc.Driver

There is no need to specify driver-class-name in SpringBoot because SpringBoot can infer what is used in the url it writes

Then check in the test method class to see if the configuration is complete, and as long as the configuration information is printed out, it means that the elements in the configuration file have been configured in DataSource.

@RunWith(SpringRunner.class)
@SpringBootTest
public class FirstSpringBootApplicationTests {

	@Autowired
	private DataSourceProperties dataSourceProperties;
	
	@Autowired
	ApplicationContext applicationContext;

	@Test
	public void contextLoads() {
		// Get the configured data source
		DataSource dataSource = applicationContext.getBean(DataSource.class);
		// View Configuration Data Source Information
		System.out.println(dataSource);
		System.out.println(dataSource.getClass().getName());
		System.out.println(dataSourceProperties.getUsername());
	}

}

1.1.2 Custom Configuration Data Source

Configure the following in application.yml


buxuewushu:
  datasource:
    url: jdbc:mysql://localhost:3306/xmall
    username: root
    password: *******
    driver-class-name: com.mysql.jdbc.Driver


Then write the following information in the configuration file


/**
 * @program: FirstSpringBoot
 * @description:
 * @author: hu_pf@suixingpay.com
 * @create: 2018-07-30 16:37
 **/
@Configuration
public class DataConfigure {

    @Bean(name = "myDataSource")
    @Qualifier("myDataSource")
    @ConfigurationProperties("buxuewushu.datasource")
    public DataSource dateSource(){
        return DataSourceBuilder.create().build();
    }

}

Then test the following in the test class

	@Resource(name = "myDataSource")
	private DataSource myDataSource;

	@Autowired
	ApplicationContext applicationContext;

	@Test
	public void contextLoads() {
		//Execute SQL, output the data found
		JdbcTemplate jdbcTemplate = new JdbcTemplate(myDataSource);
		List<?> resultList = jdbcTemplate.queryForList("select * from tb_address");
		System.out.println("===>>>>>>>>>>>" + resultList);
	}

Information found in the Mysql database can be printed out that the configured data source is in effect.

1.1.3 Configuring multiple data sources

If you need to configure multiple data sources, you can configure two data sources using the custom configuration in the previous section.However, when you configure two data sources in the configuration file, you configure a primary data source by adding the @Primary annotation to the primary data source.Because SpringBoot's auto-configuration feature requires a specified data source to load.

You only need to configure different data sources in the resource file to distinguish them by name, such as the following:

buxuewushu:
  datasource:
    first:
      url: jdbc:mysql://localhost:3306/first
      username: root
      password: hpy911213
      driver-class-name: com.mysql.jdbc.Driver

buxuewushu:
  datasource:
    secend:
      url: jdbc:mysql://localhost:3306/secend
      username: root
      password: hpy911213
      driver-class-name: com.mysql.jdbc.Driver


Then in the java configuration file, load the configuration as follows:

	 @Bean
    @ConfigurationProperties("buxuewushu.datasource.first")
    @Primary
    public DataSource dateSourceFirst(){
        return DataSourceBuilder.create().build();
    }
    
    @Bean
    @ConfigurationProperties("buxuewushu.datasource.secend")
    public DataSource dateSourceSecend(){
        return DataSourceBuilder.create().build();
    }

1.1.4 Using JdbcTemplate

Spring's JdbcTemplate and NamedParameter JdbcTemplate classes are automatically configured.It can be used directly in the @Autowired class.Examples are as follows:

@Component
public class MyBean {

	private final JdbcTemplate jdbcTemplate;

	@Autowired
	public MyBean(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	// ...

}

If you want to configure some parameters about templates, you can configure them using spring.jdbc.template. *.

spring.jdbc.template.max-rows=500

Posted by martincrumlish on Tue, 07 May 2019 02:00:39 -0700