本文主要是介绍springboot集成springDataJpa详细过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、导入jar包
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><!-- 这里的单元测试至少为4.12版本 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- jpa --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.20</version><scope>provided</scope></dependency></dependencies>
二、新建application.yml配置文件
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8username: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driverjpa:database-platform: org.hibernate.dialect.MySQL5InnoDBDialecthibernate:ddl-auto: updateshow-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<Student,Integer>,JpaSpecificationExecutor<Student>{}
五、新建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<Student> 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 {@Autowiredprivate StudentRepository studentRepository;@Overridepublic List<Student> selectAll(){return studentRepository.findAll();}}
这篇关于springboot集成springDataJpa详细过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!