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 ...
随机推荐
- 对 a = [lambda : x for x in range(3)] 的理解
上面的语句创建了一个列表 a ,其中有三个元素,每个元素都是一个 lambda 匿名函数. >>> a = [lambda : x for x in range(3)] >&g ...
- PC(win10)上搭建 kubernetes + docker 集群环境
最近kubernetes很火,加上我又在寻找适合快速搭建测试环境的方法,kubernetes的理念很适合用于测试环境的搭建. 因此在学习的过程中写下此教程(记录)以供回顾. 0x00 环境准备 0x0 ...
- STL应用——hdu1702(队列+堆栈)
水题 练习一下堆栈和队列的使用 #include <iostream> #include <cstdio> #include <algorithm> #includ ...
- java面向对象课程设计-数学表达式计算器
项目简介 设计一个计算器,其能够: 1)由用户输入一个简单的四则运算表达式,求出其计算结果后显示. 2)特殊数学函数,如:绝对值.取整.三角函数.倒数.平方根.平方.立方等. 3)对一定范围内的数字将 ...
- PokeCats开发者日志(四)
现在是PokeCats游戏开发的第八天的上午,感觉游戏做得差不多了,来写一下开发者日志吧! (1)增加闯关模式,一共30关. (2)更改了最后一关的主题,更换了背景,将树桩改为礼物盒. ...
- linux下搜索find命令拾遗
强制删除项目下面的所有.svn文件目录,find . -name ‘.svn’ -exec rm -rf {} \; empty显示所有的空白文件,并显示详细:find . -empty size显示 ...
- JSP表单提交出现中文乱码的解决方法
1)post方式 在servlet的doGet( ) doPost( ) 中增加以下代码: response.setContentType("text/html;charset=utf- ...
- [洛谷P1278]单词游戏
题目大意:给一个有$n(n\leqslant16)$个单词的字典,求单词接龙的最大长度 题解:发现$n$很小,可以状压,令$f_{i,j}$表示选的数的状态为$i$,最后一个字母是$j$的最大长度. ...
- [洛谷P2839][国家集训队]middle
题目大意:给你一个长度为$n$的序列$s$.$Q$个询问,问在$s$中的左端点在$[a,b]$之间,右端点在$[c,d]$之间的子段中,最大的中位数. 强制在线. 题解:区间中位数?二分答案,如果询问 ...
- [51nod1482]部落信号 单调栈
~~~题面~~~ 题解: 可以发现这是一道单调栈的题目,首先来考虑数字没有重复时如何统计贡献. 因为这是一个环,而如果我们从最高的点把环断开,并把最高点放在链的最后面(顺时针移动),那么因为在最高点两 ...