springboot集成springDataJpa详细过程

一、导入jar包 


    org.springframework.boot
    spring-boot-starter-parent
    2.0.2.RELEASE
     
 



    
    
      junit
      junit
      4.12
      test
    

    
      org.springframework.boot
      spring-boot-starter-web
    

    
    
      org.springframework.boot
      spring-boot-starter-data-jpa
    
    
      mysql
      mysql-connector-java
    

    
      org.springframework.boot
      spring-boot-starter-test
      test
    

    
    
      org.projectlombok
      lombok
      1.16.20
      provided
    

  

二、新建application.yml配置文件

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    hibernate:
      ddl-auto: update
    show-sql: true

三、新建domain类  这里使用lombok插件不用写get和set方法

package com.aossci.microservices.domain;

import lombok.*;

import javax.persistence.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@ToString
@Table(name = "student")
public class Student {
    // 主键,自动递增
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    // 用户名
    private String username;
    // 密码
    private String password;
    // 性别 true为女 false为男
    private Boolean gender;
    // 成绩
    private int grade;
    // 是否删除
    private boolean isDel;
}

 四、新建dao层

package com.aossci.microservices.dao;

import com.aossci.microservices.domain.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface StudentRepository extends JpaRepository,JpaSpecificationExecutor{

}

五、新建service类

package com.aossci.microservices.service;

import com.aossci.microservices.common.BaseResp;
import com.aossci.microservices.domain.Student;
import com.aossci.microservices.query.StudentQuery;

import java.util.List;

public interface StudentService {


    /**
     * 查询所有
     * @return
     */
    List  selectAll();

}

六、新建service的实现类

package com.aossci.microservices.service.impl;

import com.aossci.microservices.common.BaseResp;
import com.aossci.microservices.dao.StudentRepository;
import com.aossci.microservices.domain.Student;
import com.aossci.microservices.query.StudentQuery;
import com.aossci.microservices.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true,rollbackFor = Exception.class)
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;
    
    @Override
    public List selectAll(){
        return studentRepository.findAll();
    }

}

 

你可能感兴趣的:(springboot)