Spring框架为链接数据库提供了许多的帮助,从JDBC链接到使用JdbcTemplate完成元素之间的映射技术。例如Hibernate、Spring Data提供了更高级别的功能,建立Repository的实现,用接口中的方法和xml文件具体SQL的映射,使得用java调用Sql更加简便。java
Java的javax.sql.DataSource
一个接口,能够获取数据库的Connection。是标准化的,取得链接的一种方式。固然在配置DataSource以前须要咱们导入所须要的jar包mysql
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile 'mysql:mysql-connector-java:8.0.11'
复制代码
这个方法很是的简单,只须要在application.yml中配置一些元素便可spring
spring:
datasource:
url: jdbc:mysql://localhost:3306/xmall
username: root
password: *******
driver-class-name: com.mysql.jdbc.Driver
复制代码
在SpringBoot中能够不用详细的指定driver-class-name,由于SpringBoot可以在所写的url中推断出来所用的sql
而后在测试方法类中查看是否已经配置完成,只要打印出来了配置信息,那么既表示已经将配置文件中的元素配置到了DataSource中。数据库
@RunWith(SpringRunner.class)
@SpringBootTest
public class FirstSpringBootApplicationTests {
@Autowired
private DataSourceProperties dataSourceProperties;
@Autowired
ApplicationContext applicationContext;
@Test
public void contextLoads() {
// 获取配置的数据源
DataSource dataSource = applicationContext.getBean(DataSource.class);
// 查看配置数据源信息
System.out.println(dataSource);
System.out.println(dataSource.getClass().getName());
System.out.println(dataSourceProperties.getUsername());
}
}
复制代码
在application.yml中配置以下bash
buxuewushu:
datasource:
url: jdbc:mysql://localhost:3306/xmall
username: root
password: *******
driver-class-name: com.mysql.jdbc.Driver
复制代码
而后在配置文件中写以下信息app
/**
* @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();
}
}
复制代码
而后在测试类中测试以下框架
@Resource(name = "myDataSource")
private DataSource myDataSource;
@Autowired
ApplicationContext applicationContext;
@Test
public void contextLoads() {
//执行SQL,输出查到的数据
JdbcTemplate jdbcTemplate = new JdbcTemplate(myDataSource);
List<?> resultList = jdbcTemplate.queryForList("select * from tb_address");
System.out.println("===>>>>>>>>>>>" + resultList);
}
复制代码
发现可以打印出来Mysql数据库中的信息,即配置的数据源已经生效。spring-boot
若是须要配置多数据源的话,那么能够在使用上一小节的自定义配置来进行配置两个数据源。可是在配置文件中配置两个数据源的时候要配置一个主数据源,即在主数据源上加上@Primary注解。由于SpringBoot的自动配置功能须要有一个指定的数据源进行加载。测试
只须要在资源文件中配置不一样的数据源,用名字进行区分开来,例如以下:
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
复制代码
而后在java的配置文件中,加载配置时以下写便可:
@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();
}
复制代码
Spring的JdbcTemplate
和NamedParameterJdbcTemplate
这两个类是自动配置的。能够直接在类中@Autowired进行使用。例子以下所示:
@Component
public class MyBean {
private final JdbcTemplate jdbcTemplate;
@Autowired
public MyBean(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
// ...
}
复制代码
若是想配置一些关于template的参数的话,能够使用spring.jdbc.template.*
进行配置。
spring.jdbc.template.max-rows=500
复制代码