springboot 集成jpa

一、springboot 集成 jpa

1、创建项目

springboot 集成jpa_第1张图片

springboot 集成jpa_第2张图片
springboot 集成jpa_第3张图片

springboot 集成jpa_第4张图片

2、再引入依赖项

这里用druid的连接池,所以需要加入

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

3、编写 配置文件

配置文件分成两大部分,一部分是jpa的设置,一部分是spring.datasource的设置

server.port=8071
spring.application.name=jpademo
spring.main.allow-bean-definition-overriding=true
spring.jpa.database=mysql
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.platform=mysql
spring.datasource.url=jdbc:mysql://172.30.100.194:3306/ds0?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=Root,123

4、利用idea自动生成实体类

打开 database对话框添加 数据库连接
springboot 集成jpa_第5张图片
填写正确的信息

springboot 集成jpa_第6张图片
确定后,界面如下:
springboot 集成jpa_第7张图片
然后,在这个表上面点击右键,选择如下位置 点击
springboot 集成jpa_第8张图片
这时,会根据表结构生成辅助类辅助类在 如下图位置:springboot 集成jpa_第9张图片
打开他,然后在 项目中新建包,用于存放实体类,然后,在database视图上,用ctrl+Shift 可以多选表,然后,点击右键,选择如下位置:
springboot 集成jpa_第10张图片
这样,就会生成 对应的实体类,可以稍微修饰下,最终结果如下:

package com.xyzh.jpademo.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Entity
@Table(name="t_order_0")
public class TOrder0 {

  @Id
  private long orderId;
  private long userId;
  private String status;


  public long getOrderId() {
    return orderId;
  }

  public void setOrderId(long orderId) {
    this.orderId = orderId;
  }


  public long getUserId() {
    return userId;
  }

  public void setUserId(long userId) {
    this.userId = userId;
  }


  public String getStatus() {
    return status;
  }

  public void setStatus(String status) {
    this.status = status;
  }

}

5、写repository类

既然用到了jpa,那么就遵循 jpa的方式:

import com.xyzh.jpademo.entity.TOrder0;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OrderRepository extends JpaRepository<TOrder0,Long> {
}

6、依次写service、controller

IOrderService:

import com.xyzh.jpademo.entity.TOrder0;

public interface IOrderService {
     
        TOrder0 insert(TOrder0 record);

        TOrder0 selectByPrimaryKey(Long orderId);

        void deleteByPrimaryKey(Long orderId);
}

OrderServiceImpl

import com.xyzh.jpademo.entity.TOrder0;
import com.xyzh.jpademo.repository.OrderRepository;
import com.xyzh.jpademo.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Optional;
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    OrderRepository repository;

    @Override
    public TOrder0 insert(TOrder0 record) {
        return repository.save(record);
    }

    @Override
    public TOrder0 selectByPrimaryKey(Long orderId) {
        Optional<TOrder0> array= repository.findById(orderId);
        if(array!=null)
            return array.get();
        return null;
    }

    @Override
    public void deleteByPrimaryKey(Long orderId) {
        repository.deleteById(orderId);
    }
}

controller:HelloController

import com.xyzh.jpademo.entity.TOrder0;
import com.xyzh.jpademo.service.IOrderService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    IOrderService orderService;

    @RequestMapping(value = "/hello/{id}",method = RequestMethod.GET)
    public String hello(@PathVariable("id") Long id){
        TOrder0 order0=new TOrder0();
        order0.setOrderId(id);
        order0.setStatus("test");
        order0.setUserId(1L);
        orderService.insert(order0);

        return id+"";
    }
}

7、访问看结果

springboot 集成jpa_第11张图片

数据库也插入值了:
在这里插入图片描述

你可能感兴趣的:(springboot,java,jpa)