SpringBoot实现的JPA封装了JPA的特性, Repository是封装了jpa的特性(我是这么理解的)

1在pom.xml引入mysql, spring-data-jpa依赖

2.在src/main/resource/下新建applicatoin.properties配置文件,并配置数据库连接

3.在application.properties配置jpa配置信息

4.编写实例

热部署pom.xml配置

<!-- spring boot devtools 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>

  

<!-- spring boot devtools的plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,呢个devtools不会起作用,即应用不会restart -->
<fork>true</fork>
</configuration>
</plugin>

  

1.加入依赖

 <!-- mysql数据库驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!-- 添加spring-boot-data-jpa依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

  

2.applicaton.properties配置mysql

########################################################
###datasource mysql
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password =
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

  

3.配置jpa

########################################################
### Java Persistence Api
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

  

4.测试实例:

Spring Boot JPA 总结
---------------------
1、需要添加相应的依赖包;
2、需要在application.properties文件添加配置信息;
3、需要创建一个实体类,比如Cat;
4、需要创建一个接口继承CrudRepository;
5、需要创建一个Service;
6、需要创建一个Controller;
7、代码测试;

cat.java

package com.muyang.boot1.demo.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; /**
* 创建一个实体类
* 如何持久化
* 1.使用@Entity进行实体类的持久化操作,当JPA检测到我们的实体类中有@Entity的 注解的时候
* 会在数据库中生成对应的表结构
* 2.如何制定主键及生成策略
* 使用@id来生成主键
* @author Administrator
*
*/
@Entity
public class Cat { /**
* 使用@Id生成主键
* 受用@GeneratedValue(strategy=GenerationType.AUTO)指定主键测策略,即mysql的自增Id
*/
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id; private String catName; private String catAge; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getCatName() {
return catName;
} public void setCatName(String catName) {
this.catName = catName;
} public String getCatAge() {
return catAge;
} public void setCatAge(String catAge) {
this.catAge = catAge;
} }

  

catRepository.java

package com.muyang.boot1.demo.repository;

import org.springframework.data.repository.CrudRepository;

import com.muyang.boot1.demo.bean.Cat;

public interface CatRepository extends CrudRepository<Cat, Integer> {

}

  

catService.java增删改查

package com.muyang.boot1.demo.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.muyang.boot1.demo.bean.Cat;
import com.muyang.boot1.demo.repository.CatRepository; @Service
public class CatService { @Resource
private CatRepository catRepository; /**
*
* update, save, delete操作
*/ //保存数据
public void save(Cat cat)
{
catRepository.save(cat);
} //删除数据
public void delete(int id)
{
catRepository.delete(id);
} //查询
public Iterable<Cat> getAll() {
return catRepository.findAll();
} }

  

catController.java控制器

package com.muyang.boot1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.muyang.boot1.demo.bean.Cat;
import com.muyang.boot1.demo.service.CatService; @RestController
@RequestMapping("/cat")
public class CatController { @Autowired
private CatService catService; //增加
@RequestMapping(value="/add")
public String save()
{
Cat cat = new Cat();
cat.setCatName("大五");
cat.setCatAge("22");
catService.save(cat);
return "save ok.";
} @RequestMapping("/del")
public String delete(int id)
{
catService.delete(id);
return "delete ok.";
} @RequestMapping(value="/getAll", produces="application/json; charset=utf-8")
public Iterable<Cat> getAll()
{
return catService.getAll();
} }

  

http://localhost:8080/cat/add

http://localhost:8080/cat/getAll

spring boot: spring-data-jpa (Repository/CrudRepository) 数据库操作, @Entity实体类持久化的更多相关文章

  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. 在Spring Data JPA 中使用Update Query更新实体类

    对于 Spring Data JPA 使用的时间不长,只有两年时间.但是踩过坑的却不少. 使用下列代码 @Modifying @Query("update User u set u.firs ...

  3. ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段

    ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...

  4. IDEA 通过数据库生成entity实体类

    IDEA利用数据库生成entity类教程 1.在左上角有一个View 选项 2. 然后选择 TOOL Windows 3. 然后选择Database然后会弹出一个窗口 4.选择+号 5.选择data ...

  5. Spring Boot使用Spring Data Jpa对MySQL数据库进行CRUD操作

    只需两步!Eclipse+Maven快速构建第一个Spring Boot项目 构建了第一个Spring Boot项目. Spring Boot连接MySQL数据库 连接了MySQL数据库. 本文在之前 ...

  6. spring boot + spring data jpa

    Spring Data Repository的核心接口是Repository(好像也没什么好惊讶的).这个接口需要领域类(Domain Class)跟领域类的ID类型作为参数.这个接口主要是让你能知道 ...

  7. Spring Boot,Spring Data JPA多数据源支持配置

    1 配置文件 wisely.primary.datasource.driverClassName=oracle.jdbc.OracleDriver wisely.primary.datasource. ...

  8. 基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD

    完成一个RESTful服务,提供几个访问接口,用来操作较简单的联系人信息,数据保存在Sql Server数据库中. 1.使用STS创建工程. 使用STS创建RESTful工程,可以参考: <用S ...

  9. 使用spring boot中的JPA操作数据库

    前言 Spring boot中的JPA 使用的同学都会感觉到他的强大,简直就是神器一般,通俗的说,根本不需要你写sql,这就帮你节省了很多时间,那么下面我们来一起来体验下这款神器吧. 一.在pom中添 ...

随机推荐

  1. 集合框架—HashMap

    HashMap提供了三个构造函数:       HashMap():构造一个具有默认初始容量 (16) 和默认加载因子 (0.75) 的空 HashMap.       HashMap(int ini ...

  2. codeforces 461C

    这题说的是 给了一张长方形的纸 1*n 然后可以按照不同的做法去折这个纸张 他有两种操作,操作1 给了一个pi 点 然后将左边的纸往右边折,第2种操作是给了一个L 和 R 然后计算出 L和R 之间的纸 ...

  3. 谷歌浏览器 URL无法访问

    使用谷歌浏览器老是会崩溃,或者访问的时候发现“URL无法访问”等失败的问题,连淘宝都没法访问,这个让人很恼火, 最后在扩展应用那里搜到个URL的redirect,问题解决了,~~发现没有再出现类似问题 ...

  4. 第一次使用crontab linux选择编辑器问题

    第一次使用crontab linux选择编辑器问题 第一次使用crontab 时,会出现no crontab for root - using an empty one“Select a editor ...

  5. php new stdClass array 实例代码

    php new stdClass array 实例代码 $searchResults = array ();// //$obj = array ("rs"=>array(), ...

  6. WireShark学习

    1.打开wireshark->Capture->Interface->选择你的网卡(选中)->Start 2.OK抓包开始,工具栏上有stop,点击停止抓包 3.过滤,这个你可 ...

  7. Ubuntu 16.04下为Android编译OpenCV 3.2.0 Manager

    http://johnhany.net/2016/07/build-opencv-manager-for-android-on-ubuntu/ 最近想在Android上尝试一下SIFT和SURF匹配算 ...

  8. 架构和性能优化的核心原则(康神sf讲座学习笔记)

    其实架构性能优化的核心就是分,分为分离.分层.分布. 分离动静分离静态资源.动态页面的分离 比如,一个页面有很多静态图片,静态的图片.动态数据.静态CSS.js,图片一般用cdn,但静态资源在使用域名 ...

  9. Redis的两种持久化方式-快照持久化(RDB)和AOF持久化

    Redis为了内部数据的安全考虑,会把本身的数据以文件形式保存到硬盘中一份,在服务器重启之后会自动把硬盘的数据恢复到内存(redis)的里边,数据保存到硬盘的过程就称为“持久化”效果. redis有两 ...

  10. 20145321《网络对抗》Exp2 后门原理与实践

    实验内容 (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作Shell, 任务计划启动 (3)使用MSF meterpreter生成可执行文件,利用ncat或 ...