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. 模仿WIN32程序处理消息

    #include "stdafx.h" #include "MyMessage.h" #include <conio.h> using namesp ...

  2. java编译 Error: Could not find or load main class java执行包main方法

    在java源文件开头有包声明语句,编译的时候需要指定生成的class文件路径. 解决方法: javac -d your_path your_class.java 例如:javac -d . First ...

  3. 在Linux 中进入单用户模式的技巧

    在这篇简短的文章中,我们将向你介绍在 SUSE 12 Linux 中进入单用户模式的步骤.在排除系统主要问题时,单用户模式始终是首选.单用户模式禁用网络并且没有其他用户登录,你可以排除许多多用户系统的 ...

  4. web前端----jQuery事件

    事件 常用事件 click(function(){...}) hover(function(){...}) blur(function(){...}) focus(function(){...}) c ...

  5. Javascript 判断对象是否相等

    在Javascript中相等运算包括"==","==="全等,两者不同之处,不必多数,本篇文章我们将来讲述如何判断两个对象是否相等? 你可能会认为,如果两个对象 ...

  6. bzoj3505 / P3166 [CQOI2014]数三角形

    P3166 [CQOI2014]数三角形 前置知识:某两个点$(x_{1},,y_{1}),(x_{2},y_{2})\quad (x_{1}<x_{2},y_{1}<y_{2})$所连成 ...

  7. eclipse启动失败Could not create the Java virtual machine

    一直使用idea,今天有研究一个新功能,因为公司项目在idea,不想动idea或是切换工作空间之类.于是把我的老eclipse启起来,可谁成想,直接报错: 点击确定之后: 最后找到解决办法: 删掉 C ...

  8. python程序转为exe文件

    python开发者向普通windows用户分享程序,要给程序加图形化的界面(传送门:这可能是最好玩的python GUI入门实例! http://www.jianshu.com/p/8abcf73ad ...

  9. Python3基础 list remove 删除元素

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  10. UNIX系统的显示时间何时会到尽头

    本文转载自:http://www.cnblogs.com/dfcao/p/expertCprogramming_intr0.html 本文分为三个小块: 一.UNIX系统中时间的存储形式: 二. ti ...