当前位置: 首页 > news >正文

企业品牌网站建设报价营销型网站建设中坚站

企业品牌网站建设报价,营销型网站建设中坚站,网站建设 合同,广州网络营销选择优质博文:IT-BLOG-CN 对于数据访问层,无论是SQL还是NoSQL,SpringBoot默认采用整合Spring Data的方式进行统一处理,添加大量自动配置,屏蔽了很多设置。引入各种xxxTemplate,xxxRepository来简化我们对数据访…

优质博文:IT-BLOG-CN

对于数据访问层,无论是SQL还是NoSQLSpringBoot默认采用整合Spring Data的方式进行统一处理,添加大量自动配置,屏蔽了很多设置。引入各种xxxTemplatexxxRepository来简化我们对数据访问层的操作。对我们来说只需要进行简单的设置即可。

一、整合基本的 JDBC 与数据源

【1】引入jdbc starter [spring-boot-starter-jdbc] 和MySQL驱动。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</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默认支持一下数据源:DataSourceHikariDataSourceBasicDataSource。用户也可以自定义数据源:如下可知是通过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() {}@Bean@ConditionalOnMissingBeanpublic DataSourceInitializer dataSourceInitializer(DataSourceProperties properties, ApplicationContext applicationContext) {return new DataSourceInitializer(properties, applicationContext);}//......
}class DataSourceInitializer implements ApplicationListener<DataSourceInitializedEvent> {
//......
}

DataSourceInitializer的两个主要作用:①、运行建表语句;②、运行操作数据的sql语句;

//运行建表语句
private void runSchemaScripts() {List<Resource> 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() {List<Resource> 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 {@Bean@Primary@ConditionalOnMissingBean({JdbcOperations.class})public JdbcTemplate jdbcTemplate() {return new JdbcTemplate(this.dataSource);}
}

【7】高级配置:使用druid数据源,首先需要引入依赖:

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.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】创建项目:选中webmybatismysqljdbc模块的starts;在pom.xml中会发现mybatisspringboot的整合依赖:这个依赖并不是以spring-boot开始的,说明并不是spring提供的依赖,而是由第三方mybatis提供的依赖包;

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.1</version>
</dependency>

【2】定义个接口类,用来操作目标表的增删改查:通过@Mapper表示该接口类是一个MybatisMapper类,通过增删改查注解@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);}
}
http://www.yayakq.cn/news/139924/

相关文章:

  • 关于企业官方网站建设的ppt网至普的营销型网站布局
  • 南通住房和城乡建设部网站首页wordpress插件中文版
  • 网上给别人做网站东莞网络推广哪家好
  • 网站备案的公司注销了网页设计难学吗有技术含量吗
  • 工程做网站如东城乡建设局网站
  • 西安网站seo哪家公司好品牌设计案例
  • 一个主机可以建设多少个网站怎么找app开发公司
  • 建网站如何赚钱福安 网站设计
  • 手机建站专家邢台做网站的价格究竟多少钱?
  • 刚做的网站搜索不到工信部网站 备案
  • 万网网站后台管理网页设计与网站建设期末考试试卷
  • 企业网站网页设计有哪些wordpress page style
  • 网站建设 方案 评价表wordpress主题权限
  • 洛阳网站建设找洛阳铭信网络中国十大电商公司
  • 安阳做网站公司广州培训+网站开发
  • 工地找工作哪个软件好深圳seo排名优化
  • 网站建设 客户定位怎么自己创建网站
  • 自学网站制作教程怎么开网站平台
  • 公司网站的作用企业网站制作排名
  • 高校网站建设意义网站模板能上传图片
  • 视频投票网站怎么做的文化公司网站建设
  • 大学网站建设公司wordpress管理历史版本
  • 如何找网站开发人员秦皇岛建网站公司
  • 用动态和静态设计一个网站wordpress发布文章 自定义栏目
  • 三亚网站建设哪家好做网站编程时容易遇到的问题
  • 网站建设和设计wordpress 福利主题
  • 各大网站网络推广的收费广州网站建设推广服务
  • 淘宝网站建设基本流程国内html5视频网站建设
  • 网站建设的架构wordpress 自定义结构
  • 手机上怎么建设网站那个网站可以做软件出售的