Spring boot中使用Spring-data-jpa方便快捷的访问数据库(推荐)

2025-05-29 0 27

什么是jpa

jpa(java persistence api)是sun官方提出的java持久化规范。它为java开发人员提供了一种对象/关联映射工具来管理java应用中的关系数据。他的出现主要是为了简化现有的持久化开发工作和整合orm技术

spring data jpa 是 spring 基于 orm 框架、jpa 规范的基础上封装的一套jpa应用框架,可使开发者用极简的代码即可实现对数据的访问和操作。它提供了包括增删改查等在内的常用功能,且易于扩展!学习并使用 spring data jpa 可以极大提高开发效率!

spring boot中使用jdbctemplate访问数据库

数据源配置

首先,为了连接数据库需要引入jdbc支持,在pom.xml中引入如下配置

?

1

2

3

4
<dependency>

<groupid>org.springframework.boot</groupid>

<artifactid>spring-boot-starter-jdbc</artifactid>

</dependency>

嵌入式数据库支持

嵌入式数据库通常用于开发和测试环境。spring-boot提供自动配置的嵌入式数据库有h2、hsql、derby,你不需要提供任何连接配置就能使用。

如h2的依赖

?

1

2

3

4

5
<dependency>

<groupid>com.h2database</groupid>

<artifactid>h2</artifactid>

<scope>runtime</scope>

</dependency>

mysql数据库支持

?

1

2

3

4

5
<dependency>

<groupid>mysql</groupid>

<artifactid>mysql-connector-java</artifactid>

<version>5.1.38</version>

</dependency>

编辑配置信息

在 src/main/resources/application.properties 中配置数据源信息

?

1

2

3

4
spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.driver

使用jdbctemplate操作数据库

spring的jdbctemplate是自动配置的,你可以直接使用@autowired来注入到你自己的bean中来使用。

通过jdbctemplate实现demoservice中定义的数据访问操作

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
@service

public class demoserivce {

@autowired

private jdbctemplate jdbctemplate;

public void create(string name, integer age) {

jdbctemplate.update("insert into demo(name, age) values(?, ?)", name, age);

}

public void deletebyname(string name) {

jdbctemplate.update("delete from demowhere name = ?", name);

}

public integer getalldemo() {

return jdbctemplate.queryforobject("select count(1) from demo", integer.class);

}

public void deletealldemo() {

jdbctemplate.update("delete from demo");

}

}

创建对userservice的单元测试用例,通过创建、删除和查询来验证数据库操作的正确性。

测试用例要增加依赖

?

1

2

3

4

5
<dependency>

<groupid>org.springframework.boot</groupid>

<artifactid>spring-boot-starter-test</artifactid>

<scope>test</scope>

</dependency>

测试代码

?

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
@runwith(springjunit4classrunner.class)

@springapplicationconfiguration(main.class)

public class applicationtests {

@autowired

private demoserivce demoserivce;

@before

public void setup() {

// 准备,清空表

demoserivce.deletealldemo();

}

@test

public void test() throws exception {

// 插入5个

demoserivce.create("a", 1);

demoserivce.create("b", 2);

demoserivce.create("c", 3);

demoserivce.create("d", 4);

demoserivce.create("e", 5);

assert.assertequals(5, demoserivce.getalldemo().intvalue());

demoserivce.deletebyname("a");

demoserivce.deletebyname("e");

// 查数据库,应该有5个

assert.assertequals(3, demoserivce.getalldemo().intvalue());

}

}

spring boot中使用spring-data-jpa

为了解决这些大量枯燥的数据操作语句,我们第一个想到的是使用orm框架,比如:hibernate。通过整合hibernate之后,我们以操作java实体的方式最终将数据改变映射到数据库表中。

为了解决抽象各个java实体基本的“增删改查”操作,我们通常会以泛型的方式封装一个模板dao来进行抽象简化,但是这样依然不是很方便,我们需要针对每个实体编写一个继承自泛型模板dao的接口,再编写该接口的实现。虽然一些基础的数据访问已经可以得到很好的复用,但是在代码结构上针对每个实体都会有一堆dao的接口和实现。

由于模板dao的实现,使得这些具体实体的dao层已经变的非常“薄”,有一些具体实体的dao实现可能完全就是对模板dao的简单代理,并且往往这样的实现类可能会出现在很多实体上。spring-data-jpa的出现正可以让这样一个已经很“薄”的数据访问层变成只是一层接口的编写方式。

使用方法

添加依赖

?

1

2

3

4
<dependency

<groupid>org.springframework.boot</groupid>

<artifactid>spring-boot-starter-data-jpa</artifactid>

</dependency>

编辑配置信息

在 src/main/resources/application.properties 中配置数据源信息

?

1

2

3

4

5
spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.driver

spring.jpa.properties.hibernate.hbm2ddl.auto=update

spring.jpa.properties.hibernate.hbm2ddl.auto是hibernate的配置属性,其主要作用是:自动创建、更新、验证数据库表结构。该参数的几种配置如下

  • create: 每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表
  • create-drop:每次加载hibernate时根据model类生成表,但是sessionfactory一关闭,表就自动删除
  • update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结构
  • validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值

创建实体

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
@entity

public class demoentity {

@id

@generatedvalue

private long id;

private string title;

private string content;

public demoentity() {

}

public demoentity(string title, string content) {

this.title = title;

this.content = content;

}

// get set 略

}

创建dao

?

1

2

3

4

5

6

7
public interface demorepository extends jparepository<demoentity, long> {

demoentity findbytitle(string title);

demoentity findbytitleandcontent(string title, string content);

// @query("select u from demoentity u where u.content=:content")

@query("from demoentity u where u.content=:content")

demoentity sqlfind(@param("content") string content);

}

sql中不要写表名,要写实体名,他会自动转化为表名的。

通过解析方法名创建查询

上面 findbytitle(string title) 与 findbytitleandcontent(string title, string content) ,没有写sql,但框架会自动按名字对上面的方对创建sql。

单元测试

?

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

30

31

32

33

34
@runwith(springjunit4classrunner.class)

@springapplicationconfiguration(main.class)

public class unittest {

@autowired

demorepository demorepository;

@test

public void test()

{

for(int i=0;i<10;i++)

{

demorepository.save(new demoentity("title"+i, "content"+i));

}

assert.assertequals(10, demorepository.findall().size());

}

@test

public void testfindbytitle()

{

demoentity res = demorepository.findbytitle("title8");

assert.assertequals("title8", res.gettitle());

}

@test

public void testfindbytitleandcontent()

{

demoentity res = demorepository.findbytitleandcontent("title9", "content9");

assert.assertequals("title9", res.gettitle());

assert.assertequals("content9", res.getcontent());

}

@test

public void testsqlfind()

{

demoentity res = demorepository.sqlfind("content7");

assert.assertequals("content7", res.getcontent());

}

}

总结

以上所述是小编给大家介绍的spring boot中使用spring-data-jpa方便快捷的访问数据库,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://blog.fengcl.com/2018/05/03/spring-data-jpa/

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring boot中使用Spring-data-jpa方便快捷的访问数据库(推荐) https://www.kuaiidc.com/111966.html

相关文章

发表评论
暂无评论