springboot 整合springDataJPA
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的更多相关文章
- springboot整合springdata-jpa
1.简介 SpringData : Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储.其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 No ...
- SpringBoot整合SpringDataJPA及在页面yaml中显示
SpringBoot整合SpringDataJPA及在页面yaml中显示 1:创建对应的数据表 2:添加依赖 3:配置数据源 1:创建对应的数据表 CREATE TABLE `user` ( `id` ...
- springboot整合springdatajpa时jar冲突
1.springboot整合springdatajpa测试时报No bean named 'entityManagerFactory' available错误 2.运行springboot主程序时报以 ...
- 从无到有Springboot整合Spring-data-jpa实现简单应用
本文介绍Springboot整合Spring-data-jpa实现简单应用 Spring-data-jpa是什么?这不由得我们思考一番,其实通俗来说Spring-data-jpa默认使用hiberna ...
- SpringBoot整合SpringDataJPA,今天没啥事情就看了一下springboot整合springdataJPA,实在是香啊,SQL语句都不用写了
SpringBoot整合SpringDataJPA 1.JPA概念 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映 ...
- 七、springboot整合Spring-data-jpa
1.Spring Data JPA是什么 由Spring提供的一个用于简化JPA开发的框架.可以在几乎不用写实现的情况下,实现对数据的访问和操作.除了CRUD外,还包括如分页.排序等一些常用的功能 1 ...
- 【使用篇二】SpringBoot整合SpringDataJPA(18)
一.pom.xml添加依赖 <dependencies> <!--web--> <dependency> <groupId>org.springfram ...
- 十六、springboot整合Spring-data-jpa(二)之通用DAO接口与添加自定义方法
@NoRepositoryBean:Spring Data Jpa在启动时就不会去实例化BaseRepository这个接口 1.通用接口: import org.springframework.da ...
- springBoot整合Spring-Data-JPA, Redis Redis-Desktop-Manager2020 windows
源码地址:https://gitee.com/ytfs-dtx/SpringBoot Redis-Desktop-Manager2020地址: https://ytsf.lanzous.com/b01 ...
随机推荐
- BZOJ 4184 shallot 线性基+分治
Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻她会给小葱一颗小葱苗或者是从小葱手里拿走一颗小葱苗,并且让小葱从 ...
- get? post? put? delete? head? trace? options? http请求方法
http1.1协议里面定义了八种请求方法: get:用作获取,读取数据 post:向指定的资源提交数据 put:更新,向指定的资源上传一个内容,比如说:更新一个用户的头像或者替换掉已有的一个视频 de ...
- SQL SERVER 时间相关操作笔记
1.DATEADD函数: A. MSDN上的示例:http://msdn.microsoft.com/zh-cn/library/ms186819%28v=sql.90%29.aspx
- Redis--各个数据类型最大存储量
原文地址:https://redis.io/topics/data-types Strings类型:一个String类型的value最大可以存储512M Lists类型:list的元素个数最多为2^3 ...
- Flink State的两张图
streamTask的invoke方法中,会循环去调用task上的每个operator的initializeState方法,在这个方法中,会真正创建除了savepointStream的其他三个对象, ...
- JSP九大内置对象及其方法
内置对象包括 request response pageContext session application out config page exception 1.out (1)clear()清除 ...
- BZOJ4602 SDOI2016齿轮(搜索)
dfs一遍给每个齿轮随便标个值看是否矛盾就行了. #include<iostream> #include<cstdio> #include<cmath> #incl ...
- SetLocalTime设置本地时间
/***************************************************************** 函数名:EnableSetTimePriviledge 功 能:开 ...
- npm错误总结
You cannot publish over the previously published version 1.0.1." : xxx 发布时一定要修改package.json的版本号 ...
- linux bash学习(一)
1.请你以 read 指令的用途,撰写一个 script ,他可以让使用者输入:1. first name 与 2. last name, 最后并且在屏幕上显示:“Your full name is: ...