服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Spring Data JDBC介绍及实现代码

Spring Data JDBC介绍及实现代码

2021-06-02 14:16解道 Java教程

这篇文章主要介绍了Spring Data JDBC介绍及实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

spring新增了一个新的数据模块:spring data jdbc。spring data jdbc背后的想法是提供对关系数据库的访问,而无需处理jpa的复杂性。jpa提供延迟加载,缓存和脏跟踪等功能。果你需要这些功能会很很棒,但会让猜测jpa的行为比非jpa更难。

延迟加载可能会在你不需要时触发昂贵的语句,或者它可能会因异常而失败。当你想要比较一个实体的两个版本是哪个变成脏数据时,缓存可能会妨碍你,让你很难找到所有持久性操作都通过的那个点。

spring data jdbc目标是实现更简单的模型,不会有缓存,脏数据跟踪或延迟加载。相反,只有在调用数据库方法时才会发出sql语句。方法返回的对象会完全加载,不会有延迟。实体没有“会话”和代理。所有这些都应该使spring data jdbc更易于推理。

当然,这种更简单的方法会导致约束。

我们来看一个简单的例子。

首先,我们需要一个实体:

?
1
2
3
4
5
6
class customer {
  @id
  long id;
  string firstname;
  localdate dob;
}

请注意,不需要getter或setter。如果您意,可以增加。实际上,唯一的要求是实体有一个注释的属性id(即@org.springframework.data.annotation.id,注意不是javax.persistence,后者是jpa)。

接下来,我们需要声明一个存储库。最简单的方法是扩展crudrepository:

?
1
interface customerrepository extends crudrepository<customer, long> {}

最后,我们需要配置applicationcontext以启用存储库的创建:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@configuration
@enablejdbcrepositories (1)
public class customerconfig extends jdbcconfiguration { (2)
 
  @bean
  namedparameterjdbcoperations operations() { (3)
    return new namedparameterjdbctemplate(datasource());
  }
 
  @bean
  platformtransactionmanager transactionmanager() { (4)
    return new datasourcetransactionmanager(datasource());
 }
 
  @bean
  datasource datasource(){ (5)
    return new embeddeddatabasebuilder()
        .generateuniquename(true)
        .settype(embeddeddatabasetype.hsql)
        .addscript("create-customer-schema.sql")
        .build();
  }
}

让我们一步一步地解释。

1. enablejdbcrepositories可以创建存储库。由于它需要存在一些bean,我们需要其余的配置。

2. 继承扩展的jdbcconfiguration将一些默认bean添加到applicationcontext。可以覆盖其方法以自定义spring data jdbc的某些行为。现在,我们使用默认实现。

3. 真正重要的部分是namedparameterjdbcoperations,它在内部用于向数据库提交sql语句。

4. 严格来说,事务管理器不是必需的。不支持跨越多个sql语句的事务。

5. spring data jdbc没有直接使用datasource,但是,由于transactionmanager和namedparameterjdbcoperations需要,将datasource注册为bean是一种确保两者使用相同实例的简单方法。

这就是一切。现在让我们测试玩玩:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@runwith(springrunner.class)
@transactional
@contextconfiguration(classes = customerconfig.class)
public class customerrepositorytest {
 
  @autowired customerrepository customerrepo;
 
  @test
  public void createsimplecustomer() {
 
    customer customer = new customer();
    customer.dob = localdate.of(1904, 5, 14);
    customer.firstname = "albert";
 
    customer saved = customerrepo.save(customer);
 
    assertthat(saved.id).isnotnull();
 
    saved.firstname = "hans albert";
 
    customerrepo.save(saved);
 
    optional<customer> reloaded = customerrepo.findbyid(saved.id);
 
    assertthat(reloaded).isnotempty();
 
    assertthat(reloaded.get().firstname).isequalto("hans albert");
  }
}

@query 注解

你可能不会只使用基本的crud方法crudrepository。可以使用简单的@query注释来指定存储库方法的查询:

?
1
2
@query("select id, first_name, dob from customer where upper(first_name) like '%' || upper(:name) || '%' ")
list<customer> findbyname(@param("name") string name);

请注意,@param如果使用-parameters标志进行编译,则不需要注释。

如果要执行更新或删除语句,可以使用@modifying向方法添加注释。

让我们创建另一个测试以试用新方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@test
public void findbyname() {
 
  customer customer = new customer();
  customer.dob = localdate.of(1904, 5, 14);
  customer.firstname = "albert";
 
  customer saved = customerrepo.save(customer);
 
  assertthat(saved.id).isnotnull();
 
  customer.id= null; (1)
  customer.firstname = "bertram";
 
  customerrepo.save(customer);
 
  customer.id= null;
  customer.firstname = "beth";
 
  customerrepo.save(customer);
 
  assertthat(customerrepo.findbyname("bert")).hassize(2); (2)
}

由于java对象与其对应行之间的连接是id类型,因此设置id为null并再次保存它会在数据库中创建另一行。

我们正在进行不区分大小写(例如)搜索,因此,我们找到“albert”和“bertram”,但不是“beth”。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jdon.com/50191

延伸 · 阅读

精彩推荐