如何整合spring data jpa

1、pom依赖

		<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

2、添加配置

spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3、创建dto对象

@Entity
public class City implements Serializable { private static final long serialVersionUID = 1L; @Id
@SequenceGenerator(name = "city_generator", sequenceName = "city_sequence", initialValue = 23)
@GeneratedValue(generator = "city_generator")
private Long id; @Column(nullable = false)
private String name; @Column(nullable = false)
private String state; @Column(nullable = false)
private String country; @Column(nullable = false)
private String map;
......
}

4、创建操作数据的Repository对象

public interface CityRepository extends Repository<City, Long> {

	Page<City> findAll(Pageable pageable);

	Page<City> findByNameContainingAndCountryContainingAllIgnoringCase(String name,
String country, Pageable pageable); City findByNameAndCountryAllIgnoringCase(String name, String country); }

5、写个简单的Controller触发调用

@Controller
public class CityController { @Autowired
private CityRepository cityRepository; @GetMapping("/")
@ResponseBody
@Transactional(readOnly = true)
public void helloWorld() {
City city = cityRepository.findByNameAndCountryAllIgnoringCase("Bath", "UK");
System.out.println(city); Page<City> cityPage = cityRepository.findAll(new PageRequest(0,
10,
new Sort(Sort.Direction.DESC, "name")));
System.out.println(Arrays.toString(cityPage.getContent().toArray()));
}
}

启动项目后访问http://localhost:8080/,控制台输出:

Bath,Somerset,UK
[Washington,DC,USA, Tokyo,,Japan, Tel Aviv,,Israel, Sydney,New South Wales,Australia, Southampton,Hampshire,UK, San Francisco,CA,USA, Palm Bay,FL,USA, New York,NY,USA, Neuchatel,,Switzerland, Montreal,Quebec,Canada]

到此,一个简单的SpringBoot2.0集成spring-data-jpa就完成了。

spring-data-jpa对一些简单的数据库操作进行了支持。具体的关键字如下:And,Or,Is,Equals,Between,LessThan,LessThanEqual,GreaterThan,GreaterThanEqual,After,Before,IsNull,IsNotNull,NotNull,Like,NotLike,StartingWith,EndingWith,Containing,OrderBy,Not,In,NotIn,TRUE,FALSE,IgnoreCase。spring-data-jpa对这些关键字的支持原理将在源码分析篇讲解,欢迎关注。

如果有复杂一些的sql语句,依靠上面的关键字是肯定不行的,所以spring-data-jpa还提供了注解用来支持自定义sql。在SQL的查询方法上面使用@Query注解,如涉及到删除和修改在需要加上@Modifying。

例:

    @Query("select c from City c where c.id = ?1")
City queryById(long id); @Modifying
@Query("update City c set c.name = ?2 where c.id = ?1")
int updateNameById(long id, String name);

注意:自定义sql的语句是对对象进行操作,风格和hql相似。

SQL数据文件在源码中,源码地址:GitHub


本篇到此结束,如果读完觉得有收获的话,欢迎点赞、关注、加公众号【贰级天災】,查阅更多精彩历史!!!

SpringBoot2.0应用(四):SpringBoot2.0之spring-data-jpa的更多相关文章

  1. Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  2. [每日短篇] 1C - Spring Data JPA (0)

    2019独角兽企业重金招聘Python工程师标准>>> 准备把 Spring Data JPA 完整看一遍,顺便把关键要点记录一下.在写这篇文章的今天,再不用 Spring Boot ...

  3. Spring Data JPA教程,第一部分: Configuration(翻译)

    Spring Data JPA项目旨在简化基于仓库的JPA的创建并减少与数据库交互的所需的代码量.本人在自己的工作和个人爱好项目中已经使用一段时间,它却是是事情如此简单和清洗,现在是时候与你分享我的知 ...

  4. JPA && Spring Data && Spring Data JPA

    1.JPA  Java Persistence API,用于对象持久化的一组API,JPA本身是一组规范,让开发者用同一种方式访问不同的ORM框架.其实也就是java实体对象和关系型数据库建立起映射关 ...

  5. 实例对比 hibernate, spring data jpa, mybatis 选型参考

    原文: 最近重构以前写的服务,最大的一个变动是将mybatis切换为spring data jpa,切换的原因很简单,有两点:第一.它是spring的子项目能够和spring boot很好的融合,没有 ...

  6. javaweb各种框架组合案例(四):maven+spring+springMVC+spring data jpa(hibernate)【失败案例】

    一.失败案例 1. 控制台报错信息 严重: Exception sending context initialized event to listener instance of class org. ...

  7. Spring Data Jpa的四种查询方式

    一.调用接口的方式 1.基本介绍 通过调用接口里的方法查询,需要我们自定义的接口继承Spring Data Jpa规定的接口 public interface UserDao extends JpaR ...

  8. Spring Data JPA教程, 第四部分: JPA Criteria Queries(未翻译)

    The third part of my Spring Data JPA tutorialdescribed how you can create custom queries by using qu ...

  9. Spring Data Jpa (四)注解式查询方法

    详细讲解声明式的查询方法 1 @Query详解 使用命名查询为实体声明查询是一种有效的方法,对于少量查询很有效.一般只需要关心@Query里面的value和nativeQuery的值.使用声明式JPQ ...

  10. spring data jpa开启批量插入、批量更新

    spring data jpa开启批量插入.批量更新 原文链接:https://www.cnblogs.com/blog5277/p/10661096.html 原文作者:博客园--曲高终和寡 *** ...

随机推荐

  1. hw3

    1. 2.将MAXPRIMES的值设为4,t2会发生越界错误,t1不会. 3.令n = 1,此时不满足while语句的判断条件,不执行while循环. 4. 点覆盖:{1,2,3,4,5,6,7,5, ...

  2. 山东省ACM多校联盟省赛个人训练第六场 poj 3335 D Rotating Scoreboard

    山东省ACM多校联盟省赛个人训练第六场 D Rotating Scoreboard https://vjudge.net/problem/POJ-3335 时间限制:C/C++ 1秒,其他语言2秒 空 ...

  3. requests+正则爬取猫眼电影前100

    最近复习功课,日常码农生活. import requests from requests.exceptions import RequestException import re import jso ...

  4. IDEA的Find菜单使用

    想要显示Find菜单在底部,如图: 选中一个类,如Cloneable,然后按键Ctrl+Alt+B(相当于eclipse中的ctrl+T) 显示如图,选择最右上角的固定标签 就会把它固定在控制台菜单中 ...

  5. spring mvc 扩展 标签解析

    spring mvc 标签解析 InterceptorsBeanDefinitionParser http://www.cnblogs.com/fangjian0423/p/springMVC-int ...

  6. mysql 循环写入数据库

    测试过程经常用到插入数据 我们首先建一个函数: delimiter # create procedure test_double() begin declare i int default 0; de ...

  7. 画PCB

    1.AD16Design中Boardshape没有redefine board shape选项来修改板子的形状:{ 解决办法:在英文输入模式下按“1”键整个界面会变绿,此时就会有redefine bo ...

  8. IDEA打开maven项目dependencies红线

    第一步:install报红的项目,从maven库下载需要的包,看看日志还缺哪些本地包,少了就去下,丢到库里.不缺包后,reimport一下一般就OK了,如果还是红的,重启一下就好了. 如果第一步还没好 ...

  9. Centos6.5---samba文件共享服务配置(一)

    Linux---------samba文件共享服务配置(一) samba是一个实现不同操作系统之间文件共享和打印机共享的一种SMB协议的免费软件. https://www.cnblogs.com/zo ...

  10. nova compute enable password injection and filesystem resize

    root@OpenstackIcehouse2:~# cat /etc/nova/nova-compute.conf [DEFAULT] compute_driver=libvirt.LibvirtD ...