1、JDBC

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring‐boot‐starter‐jdbc</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>mysql</groupId>
  7. <artifactId>mysql‐connector‐java</artifactId>
  8. <scope>runtime</scope>
  9. </dependency>
  1. spring:
  2. datasource:
  3. username: root
  4. password: 123456
  5. url: jdbc:mysql://192.168.15.22:3306/jdbc
  6. driverclassname: com.mysql.jdbc.Driver

效果:
默认是用org.apache.tomcat.jdbc.pool.DataSource作为数据源;
数据源的相关配置都在DataSourceProperties里面;
自动配置原理:
org.springframework.boot.autoconfigure.jdbc:
1、参考DataSourceConfiguration,根据配置创建数据源,默认使用Tomcat连接池;可以使用
spring.datasource.type指定自定义的数据源类型;
2、SpringBoot默认可以支持;

org.apache.tomcat.jdbc.pool.DataSource、HikariDataSource、BasicDataSource、

3、自定义数据源类型

  1. /**
  2. * Generic DataSource configuration.
  3. */
  4. @ConditionalOnMissingBean(DataSource.class)
  5. @ConditionalOnProperty(name = "spring.datasource.type")
  6. static class Generic {
  7. @Bean
  8. public DataSource dataSource(DataSourceProperties properties) {
  9. //使用DataSourceBuilder创建数据源,利用反射创建响应type的数据源,并且绑定相关属性
  10. return properties.initializeDataSourceBuilder().build();
  11. }
  12. }

4、DataSourceInitializer:ApplicationListener;
作用:
1)、runSchemaScripts();运行建表语句;
2)、runDataScripts();运行插入数据的sql语句;
默认只需要将文件命名为:

  1. schema‐*.sqldata‐*.sql
  2. 默认规则:schema.sqlschemaall.sql
  3. 可以使用
  4. schema:
  5. classpath:department.sql
  6. 指定位置

5、操作数据库:自动配置了JdbcTemplate操作数据库
2、整合Druid数据源

  1. 导入druid数据源
  2. @Configuration
  3. public class DruidConfig {
  4. @ConfigurationProperties(prefix = "spring.datasource")
  5. @Bean
  6. public DataSource druid(){
  7. return new DruidDataSource();
  8. }
  9. //配置Druid的监控
  10. //1、配置一个管理后台的Servlet
  11. @Bean
  12. public ServletRegistrationBean statViewServlet(){
  13. ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),
  14. "/druid/*");
  15. Map<String,String> initParams = new HashMap<>();
  16. initParams.put("loginUsername","admin");
  17. initParams.put("loginPassword","123456");
  18. initParams.put("allow","");//默认就是允许所有访问
  19. initParams.put("deny","192.168.15.21");
  20. bean.setInitParameters(initParams);
  21. return bean;
  22. }
  23. //2、配置一个web监控的filter
  24. @Bean
  25. public FilterRegistrationBean webStatFilter(){
  26. FilterRegistrationBean bean = new FilterRegistrationBean();
  27. bean.setFilter(new WebStatFilter());
  28. Map<String,String> initParams = new HashMap<>();
  29. initParams.put("exclusions","*.js,*.css,/druid/*");
  30. bean.setInitParameters(initParams);
  31. bean.setUrlPatterns(Arrays.asList("/*"));
  32. return bean;
  33. }
  34. }

3、整合MyBatis

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis‐spring‐boot‐starter</artifactId>
  4. <version>1.3.1</version>
  5. </dependency>

步骤:
1)、配置数据源相关属性(见上一节Druid)
2)、给数据库建表
3)、创建JavaBean
4)、注解版

  1. //指定这是一个操作数据库的mapper
  2. @Mapper
  3. public interface DepartmentMapper {
  4. @Select("select * from department where id=#{id}")
  5. public Department getDeptById(Integer id);
  6. @Delete("delete from department where id=#{id}")
  7. public int deleteDeptById(Integer id);
  8. @Options(useGeneratedKeys = true,keyProperty = "id")
  9. @Insert("insert into department(departmentName) values(#{departmentName})")
  10. public int insertDept(Department department);
  11. @Update("update department set departmentName=#{departmentName} where id=#{id}")
  12. public int updateDept(Department department);
  13. }

问题:
自定义MyBatis的配置规则;给容器中添加一个ConfigurationCustomizer;

  1. @org.springframework.context.annotation.Configuration
  2. public class MyBatisConfig {
  3. @Bean
  4. public ConfigurationCustomizer configurationCustomizer(){
  5. return new ConfigurationCustomizer(){
  6. @Override
  7. public void customize(Configuration configuration) {
  8. configuration.setMapUnderscoreToCamelCase(true);
  9. }
  10. };
  11. }
  12. }
  1. 使用MapperScan批量扫描所有的Mapper接口;
  2. @MapperScan(value = "com.atguigu.springboot.mapper")
  3. @SpringBootApplication
  4. public class SpringBoot06DataMybatisApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(SpringBoot06DataMybatisApplication.class, args);
  7. }
  8. }

5)、配置文件版

  1. mybatis:
  2. configlocation: classpath:mybatis/mybatisconfig.xml 指定全局配置文件的位置
  3. mapperlocations: classpath:mybatis/mapper/*.xml 指定sql映射文件的位置

更多使用参照
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

4、整合SpringData JPA
1)、SpringData简介

2)、整合SpringData JPA
JPA:ORM(Object Relational Mapping);
1)、编写一个实体类(bean)和数据表进行映射,并且配置好映射关系;

  1. //使用JPA注解配置映射关系
  2. @Entity //告诉JPA这是一个实体类(和数据表映射的类)
  3. @Table(name = "tbl_user") //@Table来指定和哪个数据表对应;如果省略默认表名就是user;
  4. public class User {
  5. @Id //这是一个主键
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)//自增主键
  7. private Integer id;
  8. @Column(name = "last_name",length = 50) //这是和数据表对应的一个列
  9. private String lastName;
  10. @Column //省略默认列名就是属性名
  11. private String email;

2)、编写一个Dao接口来操作实体类对应的数据表(Repository)

  1. //继承JpaRepository来完成对数据库的操作
  2. public interface UserRepository extends JpaRepository<User,Integer> {
  3. }

3)、基本的配置JpaProperties

  1. spring:
  2. jpa:
  3. hibernate:
  4. # 更新或者创建数据表结构
  5. ddlauto: update
  6. # 控制台显示SQL
  7. showsql: true

SpringBoot与数据层的更多相关文章

  1. Spring Boot 系列(九)数据层-集成Spring-data-jpa

    实际开发中,不可避免地会对数据进行反复的增删改查操作,然而这部分工作是十分繁琐枯燥的.那么,随即而生的ORM框架就能很好的解决这个问题. 我们常用的ORM框架有:Hibernate.Mybatis.J ...

  2. Spring Boot 2.X(二):集成 MyBatis 数据层开发

    MyBatis 简介 概述 MyBatis 是一款优秀的持久层框架,支持定制化 SQL.存储过程以及高级映射.它采用面向对象编程的方式对数据库进行 CRUD 的操作,使程序中对关系数据库的操作更方便简 ...

  3. SpringBoot之数据访问和事务-专题三

    SpringBoot之数据访问和事务-专题三 四.数据访问 4.1.springboot整合使用JdbcTemplate 4.1.1 pom文件引入 <parent> <groupI ...

  4. 【开源】OSharp框架解说系列(5.2):EntityFramework数据层实现

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  5. 【开源】OSharp框架解说系列(5.1):EntityFramework数据层设计

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  6. 【类库】私房干货.Net数据层方法的封装

    [类库]私房干货.Net数据层方法的封装 作者:白宁超 时间:2016年3月5日22:51:47 摘要:继上篇<Oracle手边常用70则脚本知识汇总>文章的发表,引起很多朋友关注.便促使 ...

  7. 23、ASP.NET MVC入门到精通——业务层和数据层父类及接口-T4模板

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 在上一篇中,我们已经把项目的基本框架搭起来了,这一篇我们就来实现业务层和数据层的父接口及父类. 1.我们先来定义一个业务层父接口IBaseB ...

  8. 【转】Caffe初试(四)数据层及参数

    要运行caffe,需要先创建一个模型(model),如比较常用的Lenet,Alex等,而一个模型由多个层(layer)构成,每一层又由许多参数组成.所有的参数都定义在caffe.proto这个文件中 ...

  9. C#在数据层过滤属性中的主键

    C#使用泛型+反射做为数据层时,一个很都头疼的问题,如何让C#属性在程序里识别出哪个属性是主键,在拼接SQL时,不能把主键拼接到SQL语句里. 这个需要自定义一个属性.新建一个类文件,命名为Prosp ...

随机推荐

  1. 提示用户输入一个1-40之间的数字,使用if语句根据输入数字的大小进行判断,如果输入的数字在

    提示用户输入一个1-40之间的数字,使用if语句根据输入数字的大小进行判断,如果输入的数字在 num_user=input('输入一个1-40之间的整数:') num_int=int(num_user ...

  2. 【开发必备】2018最新中国国内可用API合集

    中国国内可用API合集 笔记 OneNote - OneNote支持获取,复制,创建,更新,导入与导出笔记,支持为笔记添加多媒体内容,管理权限等.提供SDK和Demo. 为知笔记 - 为知笔记Wind ...

  3. i++和++i的区别(主要为返回的值的区别)

    初学者经常会搞不清i++,和++i  的关系 i++   是把i的值拿过来,然后再+1++i   是吧i的值直接+1,之后再用

  4. 《Java基础知识》一维,二维数组的申明和使用

    为什么要使用数组: 因为不使用数组计算多个变量的时候太繁琐,不利于数据的处理. --------   数组也是一个变量,是存储一组相同类型的变量 声明一个变量就是在内存中划出一块合适的空间 声明一个数 ...

  5. Bootstrap模板-Amaretti.2.6.2

    密罐地址: 点我下载

  6. Dubbo简介与基本概念

    场景 分布式系统的发展演变以及RPC简介: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555049 Dubbo简介 Ap ...

  7. 集合框架关于<list接口><map接口>的运用

    集合: 集合就是一个容器,他可以存储对象,我们说集合就是一个可变的数组 集合框架特点 1.list和set集合同时实现了collection接口 2.set集合存储唯一,无序的对象. 3.list 存 ...

  8. jQuery模仿ToDoList实现简单的待办事项列表

    功能:在文本框中输入待办事项按下回车后,事项会出现在未完成列表中:点击未完成事项前边的复选框后,该事项会出现在已完成列表中,反之亦然:点击删除按钮会删除该事项:双击事项可以修改事项的内容.待办事项的数 ...

  9. 使用Navicat Keygen激活(破解)Navicat Premium 12

    1.到Navicat官网下载使用版本进行安装,具体操作不再详述.Navcat官网下载链接:http://www.navicat.com.cn/download/navicat-premium : 2. ...

  10. <深度学习>Tensorflow遇到的坑之一

    AttributeError: module 'tensorflow' has no attribute 'random_normal' AttributeError: module 'tensorf ...