参考文献:(早期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的实现,与上面的引用文章内容有出入。早起版本请参见引用文章