SpringBoot+Hibernate+JPA(Demo)


什么是Java Persistence API?

       Java Persistence API提供了一個規范,用於將數據通過Java對象持久化、讀取和管理到數據庫中的關系表。

什么是Hibernate框架?

       Hibernate是Java環境的對象關系映射解決方案。對象關系映射或ORM是將應用程序域模型對象映射到關系

數據庫表的編程技術。Hibernate是一個基於Java的ORM工具,它提供了一個框架,用於將應用程序域對象映射

到關系數據庫表。

       Hibernate提供了Java Persistence API的參考實現,使其成為具有松散耦合優勢的ORM工具的絕佳選擇

       注意:JPA是一個規范,Hibernate是一個JPA提供者或實現。

什么是Spring Data JPA?

       Spring Data是Spring Framework的一部分,Spring Data存儲庫抽象的目標是顯著減少為各種持久性存儲實現

數據訪問層所需的代碼量。

       Spring Data JPA不是JPA提供者。它是一個庫/框架,它在我們的JPA提供程序(如Hibernate)的頂部添加了一

個額外的抽象層。

Hibernate和Spring Data JPA有什么區別?

       Hibernate是一個JPA實現,而Spring Data JPA是一個JPA數據訪問抽象。Spring Data提供了GenericDao自定義

實現的解決方案,它還可以通過方法名稱約定代表您生成JPA查詢。

      Hibernate提供了Java Persistence API的參考實現,使其成為具有松散耦合優勢的ORM工具的絕佳選擇。

 

 

 

pom.xml

<dependencies>
        <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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

    </dependencies>

目錄結構

 

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/sell?characterEncoding=utf-8&serverTimezone=CTT&useSSL=false
  jpa:
    show-sql: true

建表sql:

create table `seller_info` (     
      `seller_id` varchar(32) not null,     
      `username` varchar(32) not null,     
      `password` varchar(32) not null,     
      `openid` varchar(64) not null comment '微信openid',     
      `create_time` timestamp not null default current_timestamp 
                 comment '創建時間',     
      `update_time` timestamp not null default current_timestamp on 
        update current_timestamp comment '修改時間',    
        primary key (`seller_id`) ) comment '賣家信息表'; 

daoobject(實體類):

@Data
@Entity
@DynamicUpdate
public class SellerInfo {

    @Id
    private String sellerId;

    private String username;

    private String password;

    private String openid;

}

repository:

public interface SellerInfoRepository extends JpaRepository<SellerInfo,String> {
    SellerInfo findBySellerId(String sellerId);
}

service:

public interface SellerInfoService {

    /**
     *
     * @param sellerId
     * @return
     */
    SellerInfo findBySellerId(String sellerId);
}

serviceImpl:

@Service
@Slf4j
public class SellerInfoServiceImpl implements SellerInfoService {

    @Autowired
    private SellerInfoRepository repository;

    @Override
    public SellerInfo findBySellerId(String sellerId){
        SellerInfo sellerInfo = repository.findBySellerId(sellerId);
        return sellerInfo;
    }
}

測試類:

@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class SellerInfoServiceImplTest {

    public final String SELLERID="1001";

    @Autowired
    private SellerInfoServiceImpl sellerInfoService;

    @Test
    public void testFindBySellerId() throws Exception {
        SellerInfo result = sellerInfoService.findBySellerId(SELLERID);
        if(result == null){
            log.error(""+SELLERID+"的賣家信息未查出!!】");
            throw new SellerException(ResultEnum.SELLINFO_FIND_FAIL);
        }else{
            log.info(""+SELLERID+"的賣家姓名為:{},手機號:{},地址:{}】",result.getUsername());
        }
    }
}

 

 

https://github.com/liuchunbo24/SpringBoot-Hibernate-JPA-Demo


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM