博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Data之Hello World
阅读量:7280 次
发布时间:2019-06-30

本文共 2161 字,大约阅读时间需要 7 分钟。

1. 概述

  • SpringData : 注意目标是使数据库的访问变得方便快捷;支持NoSQL和关系数据存储;
    • 支持NoSQL存储:
      • MongoDB(文档数据库)
      • Neo4j(图形数据库)
      • Redis(键/值存储)
      • Hbase(列族数据库)
    • 关系数据库存储:
      • JDBC
      • JPA

2. Spring Data 入门程序 Hello World

2.1 使用 Spring Data JPA 进行持久层开发需要的四个步骤:

  • 配置Spring整合JPA;
  • 在Spring配置中配置Spring Data,让Spring为声明的接口创建代理对象;
  • 声明持久层的接口,该接口继承Repository,Repository是一个标记型接口,它不包含任何方法,如果必要,
    Spring Data 可实现Repository其他子接口,其中定义了一些常用的增删改查,以及分页相关的方法;
  • 在接口中声明需要的方法;

1222878-20180610182002733-374632862.png

// Spring 配置文件 applicationContext.xml
org.hibernate.cfg.ImprovedNamingStrategy
org.hibernate.dialect.MySQL5InnoDBDialect
true
true
update
// db.propertiesjdbc.username=rootjdbc.password=rootjdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql:///jpa// com.noodles.springdata// Person 类@Table(name="JPA_PERSONS")@Entitypublic class Person{ private Integer id; private String lastName; private String email; private Date birth; @GeneratedValue @Id privte Integer getId(){ return id; } private void SetId(Integer id){ this.id = id; } ...(省略getter和setter)}// PersonRepository.java(接口)public interface PersonRepository extends Repository
{ Person getByLastName(String lastName);}// 测试类com.noodles.springdata.test// SpringDataTestpublic class SpringDataTest{ private ApplicationContext ctx = null; { ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testHelloWorld(){ PersonRepository pr = ctx.getBean(PersonRepository.class); Person person = pr.getByLastName("李四"); System.out.println(person); }}

参考资料:

转载于:https://www.cnblogs.com/linkworld/p/9163936.html

你可能感兴趣的文章
邮件发送
查看>>
matlab练习程序(简单图像融合)
查看>>
【C#学习笔记】自我复制
查看>>
Texture lod计算
查看>>
ResultJsonInfo<T>
查看>>
UnicodeString基本操作(Ring3)
查看>>
读取配置
查看>>
自己动手下载Windows 8 的语言包使其可以离线使用
查看>>
漫话web渗透 : xss跨站攻击day1
查看>>
Python学习笔记(三)—第五天,文件读写以及基本的序列化
查看>>
Javascript-蔬菜运算价格
查看>>
2011 Michigan Invitational Programming Contest
查看>>
腾讯的微信小程序开发环境下常用快捷键汇总
查看>>
《统一沟通-微软-实战》-6-部署-1-前端服务器-2-准备基础结构和系统
查看>>
PureFTPD配置指南
查看>>
创建DDL触发器捕捉schema所有对象改变的记录
查看>>
RAC执行root.sh报libcap.so.1: cannot open shared object file
查看>>
解决Lync边缘服务器受限制的外部呼叫
查看>>
neo4j简单学习
查看>>
IOS NSInvocation用法
查看>>