可以建公司网站,做一个公司官网怎么做,邯郸网络广播电视台,网站建设客户分析调查问卷优质博文#xff1a;IT-BLOG-CN 对于数据访问层#xff0c;无论是SQL还是NoSQL#xff0c;SpringBoot默认采用整合Spring Data的方式进行统一处理#xff0c;添加大量自动配置#xff0c;屏蔽了很多设置。引入各种xxxTemplate#xff0c;xxxRepository来简化我们对数据访… 优质博文IT-BLOG-CN 对于数据访问层无论是SQL还是NoSQLSpringBoot默认采用整合Spring Data的方式进行统一处理添加大量自动配置屏蔽了很多设置。引入各种xxxTemplatexxxRepository来简化我们对数据访问层的操作。对我们来说只需要进行简单的设置即可。
一、整合基本的 JDBC 与数据源
【1】引入jdbc starter [spring-boot-starter-jdbc] 和MySQL驱动。
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jdbc/artifactId
/dependency
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope
/dependency【2】在application.yml中配置数据源相关信息
spring:datasource:username: rootpassword: 123url: jdbc:mysql://127.0.0.1:3306/jdbcdriver-class-name: com.mysql.jdbc.Driver【3】测试默认使用的是org.apache.tomcat.jdbc.pool.DataSource作为数据源。数据源的相关配置都在DataSourceProperties里面。自动配置原理org.springframework.boot.autoconfigure.jdbc包中的DataSourceConfiguration根据配置创建数据源默认使用Tomcat连接池可以通过spring.datasource.type指定自定义数据源类型SpringBoot默认支持一下数据源DataSource、HikariDataSource、BasicDataSource。用户也可以自定义数据源如下可知是通过build创建数据源的。利用反射创建type类型的数据源并绑定相关属性。
ConditionalOnMissingBean({DataSource.class})
ConditionalOnProperty(name {spring.datasource.type}
)
static class Generic {Generic() {}//通过 build 创建数据源的。利用反射创建 type 类型的数据源并绑定相关属性。Beanpublic DataSource dataSource(DataSourceProperties properties) {return properties.initializeDataSourceBuilder().build();}
}【4】第二个比较重要的类DataSourceAutoConfiguration自动配置类中的dataSourceInitializer继承了ApplicationListener。
public class DataSourceAutoConfiguration {private static final Log logger LogFactory.getLog(DataSourceAutoConfiguration.class);public DataSourceAutoConfiguration() {}BeanConditionalOnMissingBeanpublic DataSourceInitializer dataSourceInitializer(DataSourceProperties properties, ApplicationContext applicationContext) {return new DataSourceInitializer(properties, applicationContext);}//......
}class DataSourceInitializer implements ApplicationListenerDataSourceInitializedEvent {
//......
}DataSourceInitializer的两个主要作用①、运行建表语句②、运行操作数据的sql语句
//运行建表语句
private void runSchemaScripts() {ListResource scripts this.getScripts(spring.datasource.schema, this.properties.getSchema(), schema);if(!scripts.isEmpty()) {String username this.properties.getSchemaUsername();String password this.properties.getSchemaPassword();this.runScripts(scripts, username, password);try {this.applicationContext.publishEvent(new DataSourceInitializedEvent(this.dataSource));if(!this.initialized) {this.runDataScripts();this.initialized true;}} catch (IllegalStateException var5) {logger.warn(Could not send event to complete DataSource initialization ( var5.getMessage() ));}}}
//运行操作数据的 sql语句
private void runDataScripts() {ListResource scripts this.getScripts(spring.datasource.data, this.properties.getData(), data);String username this.properties.getDataUsername();String password this.properties.getDataPassword();this.runScripts(scripts, username, password);
}【5】进行数据表创建时默认只需要将文件命名为schema-*.sql进行数据操作时默认只需要将文件命令为data-*.sql如果自定义sql文件时可以在application.yml中通过如下方式指定sql文件位置传入的是一个list列表
spring:datasource:schema:- classpath: student.sql【6】JdbcTemplateAutoConfiguration自动配置类给容器中注入了JdbcTemplate组件帮组我们操作数据库。
AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class JdbcTemplateAutoConfiguration {BeanPrimaryConditionalOnMissingBean({JdbcOperations.class})public JdbcTemplate jdbcTemplate() {return new JdbcTemplate(this.dataSource);}
}【7】高级配置使用druid数据源首先需要引入依赖
!-- https://mvnrepository.com/artifact/com.alibaba/druid --
dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.1.10/version
/dependency【8】在yml配置文件中加入Druid
spring:datasource:username: rootpassword: 123url: jdbc:mysql://127.0.0.1:3306/jdbcdriver-class-name: com.mysql.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource二、整合 Mybatis 数据源注解版
【1】创建项目选中web、mybatis、mysql和jdbc模块的starts在pom.xml中会发现mybatis与springboot的整合依赖这个依赖并不是以spring-boot开始的说明并不是spring提供的依赖而是由第三方mybatis提供的依赖包
dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.1.1/version
/dependency【2】定义个接口类用来操作目标表的增删改查通过Mapper表示该接口类是一个Mybatis的Mapper类通过增删改查注解Select Update Insert Delete对数据表进行操作
//指定这是一个操作数据库的 mapper
Mapper
public interface DepartmentMapper {Select(select * from department where id#{id})public Department getDeptById(Integer id);
}【3】通过Controller层调用Server继而调用Mapper对数据完成操作
Controller
public class DepartmentController {Autowiredprivate DepartmentMapper mapper;GetMapping(/dept/{id})public Department getDeptById(PathVariable(id) Integer id){return mapper.getDeptById(id);}
}