23. Spring Boot JPA BaseDao 配置 文章


 

 參考文獻:(早期JPA版本的描述)

https://blog.csdn.net/yingxiake/article/details/51017797

https://www.jianshu.com/p/73f48095a7bf

https://www.cnblogs.com/ityouknow/p/5891443.html

https://www.jianshu.com/p/da4f584d6e14 

 

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

 

spring:
  datasource:
    jpa:
        show-sql: true #控制台顯示SQL
        hibernate:
           ddl-auto: update #更新或創建表

 

 

package com.example.jdbc.model;

import javax.persistence.*;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id;

    @Column
    private String name;

    @Column
    private Integer age;

    @Column
    private Integer sex;

    get() set()......
}

 

 

package com.example.jdbc.dao;

import com.example.jdbc.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserDao extends JpaRepository<User,Integer> {

}

 

 

 

自己拓展JPA,不光定義接口,還要有實現類和自定義的方法(名稱隨意起) 

 

package com.example.jdbc.dao;

/** * Created with IDEA * author:Guchunchao * Date:2018/12/3 * Time:下午6:02 */ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.repository.NoRepositoryBean; import java.io.Serializable; import java.util.List; @NoRepositoryBean public interface BaseDao<T,ID extends Serializable> extends JpaRepository<T,ID>, JpaSpecificationExecutor<T> { public List<Object[]> selectBySQL(String sql); }

 

 

package com.example.jdbc.dao;

/** * Created with IDEA * author:Guchunchao * Date:2018/12/3 * Time:下午6:03 */ import org.springframework.data.jpa.repository.support.JpaEntityInformation; import org.springframework.data.jpa.repository.support.JpaEntityInformationSupport; import org.springframework.data.jpa.repository.support.SimpleJpaRepository; import javax.persistence.EntityManager; import java.io.Serializable; import java.util.List; /** * 解決方法 * This is a bug in IntelliJ that is fixed in 13.1. http://youtrack.jetbrains.com/issue/IDEA-120977 * Class doesn't contain matching constructor for autowiring */ public class BaseDaoImpl<T, ID extends Serializable> extends SimpleJpaRepository<T,ID> implements BaseDao<T,ID> { private EntityManager entityManager; public BaseDaoImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) { super(entityInformation, entityManager); } //父類沒有不帶參數的構造方法,這里手動構造父類 public BaseDaoImpl(Class<T> domainClass, EntityManager entityManager) { this(JpaEntityInformationSupport.getEntityInformation(domainClass, entityManager), entityManager); this.entityManager = entityManager; } //通過EntityManager來完成自定義的查詢 @Override public List<Object[]> selectBySQL(String sql) { return entityManager.createNativeQuery(sql).getResultList(); } }

 

 

package com.example.jdbc.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

import javax.persistence.EntityManager;
import java.io.Serializable;

public class BaseDaoFactoryBean<R extends JpaRepository<T, ID>, T,
        ID extends Serializable> extends JpaRepositoryFactoryBean<R, T, ID> {

    public BaseDaoFactoryBean(Class<? extends R> repositoryInterface) {
        super(repositoryInterface);
    }

    @Override
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager em) {
        return new BaseDaoFactory(em);
    }

    //創建一個內部類,該類不用在外部訪問
    private static class BaseDaoFactory<T, I extends Serializable>
            extends JpaRepositoryFactory {

        private final EntityManager em;

        public BaseDaoFactory(EntityManager em) {
            super(em);
            this.em = em;
        }

        //此方法以經在父類方法中實現了,所以不用再自定義了
//        @Override
//        protected JpaRepositoryImplementation getTargetRepository(RepositoryInformation information) {
//            return super(information, em);
//        }

        //設置具體的實現類的class
        @Override
        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
            return BaseDaoImpl.class;
        }
    }

}

 

 

package com.example.jdbc;

import com.example.jdbc.dao.BaseDaoFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableJpaRepositories(repositoryFactoryBeanClass = BaseDaoFactoryBean.class, basePackages ="com.example.jdbc.dao")
@EnableTransactionManagement
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SpringBootJdbcApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootJdbcApplication.class, args);
    }
}

 

 

注意:這是SpringBoot 2.1.1版本集成的JPA的實現,與上面的引用文章內容有出入。早起版本請參見引用文章

 


免責聲明!

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



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