springboot 整合springDataJPA

news/2025/2/8 10:59:00 标签: Springboot, spring-data-jpa

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();

    }
}

http://www.niftyadmin.cn/n/1639109.html

相关文章

[CKEditor那点事儿]使用java开发ckeditor的文件上传功能

关于CKEditor的使用&#xff0c;网络上有无数的文章&#xff0c;这里不再赘述。而关于java支持的文件上传功能&#xff0c;网络上同样有千千万万的文章&#xff0c;但是遍历十几二十篇就会发现&#xff0c;千篇一律的抄袭&#xff0c;各种爬虫程序带来的互联网信息垃圾给我们的…

SpringBoot Jpa使用时碰到的问题总结

1.Jpa的映射策略 在springboot使用jpa的话&#xff0c;会有这么一行配置&#xff1a; spring.jpa.hibernate.naming.physical-strategyorg.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl这是jpa的命名策略。查阅资料后&#xff0c;解释是这样的&#xff1…

[CKEditor那点事儿]使用java开发ckeditor的浏览服务器文件功能

接上篇文章&#xff0c;本章节讲述使用java语言的ckeditor服务器文件浏览功能。 // 虽然两篇文章的代码都带有文件类型参数&#xff0c;但是此处不进行判断非法格式&#xff0c;仅以图片类型为主介绍控件功能的开发过程。 上篇文章中&#xff0c;在CKEDITOR的图片选择页面&am…

[ajax]多个异步请求初始化JST渲染数据的时间差问题

今天犯了个很有点巧妙的问题&#xff0c;通常而言在开发一个大页面的时候&#xff0c;这里的大页面指的是页面表单控件诸如select中的数据是被动态初始化进去的而不是写死在页面中的&#xff0c;为了提升页面的访问性能&#xff0c;通畅会采用异步的方式去获取&#xff0c;也就…

SpringBoot项目通过 spring data elasticsearch使用elasticsearch

依赖添加 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>配置添加 spring.data.elasticsearch.cluster-nodes10.111.27.202:9300实体标注 Doc…

在centos8中升级并安装最新版本docker-ce

查看你当前的linux内核版本是否符合官方对linux版本的要求&#xff1a; uname -r 卸载旧版本(如果安装过旧版本的话)&#xff1a; sudo yum remove docker docker-common docker-selinux docker-engine 安装需要的软件包&#xff1a; sudo yum install -y yum-utils devic…

【必收藏】不得不看的Spring相关资源整合

Spring是于2003年兴起的一个轻量级的Java开发框架&#xff0c;致力于简化Java EE 的企业级应用开发。而Spring Boot是由Pivotal团队提供的全新框架&#xff0c;Spring Boot简化了基于Spring的应用开发&#xff0c;该框架使用了特定的方式来进行配置&#xff0c;从而使开发人员不…

[JQUERY-AJAX-JST] 动态渲染的页面事件无法绑定问题的解决方案

一个页面是使用JST动态进行渲染的&#xff0c;其中有一段代码是用于图片上传&#xff0c;如下&#xff1a; 上传商品照片&#xff08;最多5张&#xff09; <form id"form1" name"form1" lang"zh-cn" action"upload&quo…