springboot 整合springDataJPA

〇、搭建springboot环境

一、添加依赖

  • mysql

      <!-- mysql驱动 -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    </dependency>
  • springdatajpa

      <!-- springdata jpa依赖 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

配置文件(src/main/resources/application.properties)

########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/springboot
spring.datasource.username = root
spring.datasource.password = root
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
########################################################
### 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

持久层

import org.springframework.data.repository.CrudRepository;
import com.xujie.pojo.Target; public interface TargetRepository extends CrudRepository<Target,Integer>{ }

Entity

使用jpa的时候,一般先开发entity.因为如果配置文件配置了spring.jpa.hibernate.ddl-auto = update的话,jpa可以直接根据entity创建表;

示例:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity(name="target")
public class Target {
@Id
@GeneratedValue
private int tid;
@Column(name="tname",length=20)
private String tname; public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
@Override
public String toString() {
return "Target [tid=" + tid + ", tname=" + tname + "]";
}
}

service

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.xujie.pojo.Target;
import com.xujie.repository.TargetRepository;
import com.xujie.service.TargetService; @Service
public class TargetServiceimpl implements TargetService { // @Resource是根据名称注入 @Autowired是根据类型注入
@Resource
private TargetRepository targetRepository; //添加
@Override
public void save(Target target) {
this.targetRepository.save(target);
} //删除
@Override
public void delete(Integer id) {
this.targetRepository.delete(id);
} //查询
@Override
public List<Target> findAll(){
return (List<Target>) this.targetRepository.findAll();
}
}

controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xujie.pojo.Target;
import com.xujie.service.TargetService; @RestController
public class TargetController { @Autowired
private TargetService targetService; //添加
@GetMapping("/save")
public void save() {
Target target = new Target();
target.setTname("减肥");
this.targetService.save(target);
} //删除
@GetMapping("/delete/{id}")
public void delete(@PathVariable Integer id) {
this.targetService.delete(id);
} //查询所有
@GetMapping("/findAll")
public List<Target> findAll() {
return this.targetService.findAll(); }
}

springboot 整合springDataJPA的更多相关文章

  1. springboot整合springdata-jpa

    1.简介  SpringData : Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储.其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 No ...

  2. SpringBoot整合SpringDataJPA及在页面yaml中显示

    SpringBoot整合SpringDataJPA及在页面yaml中显示 1:创建对应的数据表 2:添加依赖 3:配置数据源 1:创建对应的数据表 CREATE TABLE `user` ( `id` ...

  3. springboot整合springdatajpa时jar冲突

    1.springboot整合springdatajpa测试时报No bean named 'entityManagerFactory' available错误 2.运行springboot主程序时报以 ...

  4. 从无到有Springboot整合Spring-data-jpa实现简单应用

    本文介绍Springboot整合Spring-data-jpa实现简单应用 Spring-data-jpa是什么?这不由得我们思考一番,其实通俗来说Spring-data-jpa默认使用hiberna ...

  5. SpringBoot整合SpringDataJPA,今天没啥事情就看了一下springboot整合springdataJPA,实在是香啊,SQL语句都不用写了

    SpringBoot整合SpringDataJPA 1.JPA概念 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映 ...

  6. 七、springboot整合Spring-data-jpa

    1.Spring Data JPA是什么 由Spring提供的一个用于简化JPA开发的框架.可以在几乎不用写实现的情况下,实现对数据的访问和操作.除了CRUD外,还包括如分页.排序等一些常用的功能 1 ...

  7. 【使用篇二】SpringBoot整合SpringDataJPA(18)

    一.pom.xml添加依赖 <dependencies> <!--web--> <dependency> <groupId>org.springfram ...

  8. 十六、springboot整合Spring-data-jpa(二)之通用DAO接口与添加自定义方法

    @NoRepositoryBean:Spring Data Jpa在启动时就不会去实例化BaseRepository这个接口 1.通用接口: import org.springframework.da ...

  9. springBoot整合Spring-Data-JPA, Redis Redis-Desktop-Manager2020 windows

    源码地址:https://gitee.com/ytfs-dtx/SpringBoot Redis-Desktop-Manager2020地址: https://ytsf.lanzous.com/b01 ...

随机推荐

  1. 6.爬虫 requests库讲解 总结

    requests库的总结: 用ProcessOn根据前面的几节内容做了个思维导图:

  2. DFS做题小结

    一.深入理解DFS 采用递归写法 深度优先,思路就是沿着一条路一直走,直到走到死胡同,原路返回,返回到有多条道路的地方换其他路走.直到这条支路全部都访问过了,按照原路返回,回到起点,如果起点还有别的支 ...

  3. android多点触控自由对图片缩放

    在系统的相册中,观看相片就可以用多个手指进行缩放. 要实现这个功能,只需要这几步: 1.新建项目,在项目中新建一个ZoomImage.java public class ZoomImageView e ...

  4. lintcode-96-链表划分

    96-链表划分 给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前. 你应该保留两部分内链表节点原有的相对顺序. 样例 给定链表 1->4->3->2-&g ...

  5. 使用 TListView 控件(2)

    本例效果图: 代码文件: unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, ...

  6. Dubbo 的 Helloworld

    前提条件 安装好了 ZooKeeper 作为注册中心 服务端 <?xml version="1.0" encoding="UTF-8"?> < ...

  7. 通过 servletContext设置点击次数 利用的是全局变量的特性

  8. android中常见的命名及其特点详解

    Paseal命名法 Paseal命名法特点:String MyName-DelphiInt MyAge每个单词首字母大写 Camel命名法 Camel(驼峰的意思)命名法特点:String myNam ...

  9. ashx文件和aspx

    ashx文件和aspx文件有什么不同? 我们先新建一个ashx文件看看: <%@ WebHandler Language="C#" Class="Handler&q ...

  10. BZOJ4602 SDOI2016齿轮(搜索)

    dfs一遍给每个齿轮随便标个值看是否矛盾就行了. #include<iostream> #include<cstdio> #include<cmath> #incl ...