SpringBoot——数据层解决方案(未完成,待补)

导读:本篇文章讲解 SpringBoot——数据层解决方案(未完成,待补),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

SQL

SpringBoot——数据层解决方案(未完成,待补)

  • 通用数据源配置格式(以Druid为例)

SpringBoot——数据层解决方案(未完成,待补)

 内置数据源

         当我们的springboot在不使用任何自己加载的数据源的时候,它就会使用这个数据源。

SpringBoot——数据层解决方案(未完成,待补)

SpringBoot——数据层解决方案(未完成,待补)

  •  配置文件中数据源配置格式(以hikari为例),注意:hikari数据源配制url地址要单独配置

SpringBoot——数据层解决方案(未完成,待补)

  • 通用配置无法设置具体的数据源配置信息,仅提供基本的连接相关配置,如需配置,在下一级配置中设置具体设定

SpringBoot——数据层解决方案(未完成,待补)

JdbcTemplate

        好心的springboot为我们提供了默认的数据源解决方案,Spring还为我们提供了一个默认的持久化技术,那就是JdbcTemplate。

SpringBoot——数据层解决方案(未完成,待补)

  • 导入jdbc对应坐标
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

 他是一操作数据库的模板对象,里面使用的技术就是GDP技术,就是自己写结果集的处理。

SpringBoot——数据层解决方案(未完成,待补)

  • 自动装配JdbcTemplate对象

        要想创建一个JdbcTemplate对象, 首先需要一个dataSource的对象,非常好的一点,是datasource已经在环境的配置文件当中加载了,也就是说,只需要直接加载JdbcTemplate对象就行了。

@SpringBootTest
class Springboot15SqlApplicationTests {
    @Test
    void testJdbcTemplate(@Autowired JdbcTemplate jdbcTemplate){  //对象的作用范围变小
    }
}
  •  使用JdbcTemplate实现查询操作(非实体类封装数据的查询操作)
@Test
void testJdbcTemplate(@Autowired JdbcTemplate jdbcTemplate){
    String sql = "select * from tbl_book";
    List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
    System.out.println(maps);
}

SpringBoot——数据层解决方案(未完成,待补)

  • 使用JdbcTemplate实现查询操作(实体类封装数据的查询操作)(查询标准格式)

        使用jdbcTemplate.query()作通用操作,需要传入sql对象行映射rowmaper对象,创建rowmapper对象首先在尖括号当中填写当前查询的对象的模型类型,当前是Book类型,随后直接创建这个接口需要实现一个方法,这个方法如果在前面指定过泛型了,他就默认返回就是你这个泛型,如果前面没指定,这样就是返回Object,可以看到其中有一个ResultSet对象,这里就是封装的结果集,将查询到的值(getXxx)Set给Book对象并返回。

 @Test
    void testJdbcTemplate(@Autowired JdbcTemplate jdbcTemplate){

        String sql = "select * from tbl_book";
//        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
//        System.out.println(maps);
        RowMapper<Book> rm = new RowMapper<Book>() {
            @Override
            public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
                Book temp = new Book();
                temp.setId(rs.getInt("id"));
                temp.setName(rs.getString("name"));
                temp.setType(rs.getString("type"));
                temp.setDescription(rs.getString("description"));
                return temp;
            }
        };
        List<Book> list = jdbcTemplate.query(sql, rm);
        System.out.println(list);
    }
  •  使用JdbcTemplate实现增删改操作
    @Test
    void testJdbcTemplateSave(@Autowired JdbcTemplate jdbcTemplate){
        String sql = "insert into tbl_book values(3,'springboot1','springboot2','springboot3')";
        jdbcTemplate.update(sql);
    }

如果想对JdbcTemplate对象进行相关配置,可以在yml文件中进行设定

SpringBoot——数据层解决方案(未完成,待补)

H2数据库

  • 导入H2数据库对应的坐标
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
  • 将工程设置为web工程,启动工程时启动H2数据库,启动程序运行后访问/h2路径
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 通过配置开启H2数据库控制台访问程序

SpringBoot——数据层解决方案(未完成,待补)

#h2数据库
server:
  port: 80
spring:
  h2:
    console:      #此时这个h2数据库就可以通过控制台进行访问了
      enabled: true
      path: /h2   #设置一个web访问程序,启动路径为指定名称就行了

  datasource:
    url: jdbc:h2:~/test
    hikari:
#      driver-class-name: org.h2.Driver
      username: sa
      password: 123456

mybatis-plus:
  global-config:
    db-config:
      table-prefix: tbl_
      id-type: auto
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  •  操作数据库 

SpringBoot——数据层解决方案(未完成,待补)

SpringBoot——数据层解决方案(未完成,待补)

 SpringBoot——数据层解决方案(未完成,待补)

  •  更多的数据层解决方案

SpringBoot——数据层解决方案(未完成,待补)

NoSQL

市面上常见的NoSQL解决方案

  • Redis
  • Mongo
  • ES

Redis

SpringBoot——数据层解决方案(未完成,待补)

Mongo

ES

(在这里填个坑,日后再来补充)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/99454.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!