title: 如何单独使用spring data jpa
引用pom文件:
org.springframework.data spring-data-jpa 2.1.5.RELEASE org.hibernate hibernate-core 5.3.5.Final org.hibernate hibernate-c3p0 5.3.5.Final mysql mysql-connector-java 5.1.47 com.mchange c3p0 0.9.5.2
编写配置类:
package com.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.orm.jpa.JpaTransactionManager;import org.springframework.orm.jpa.JpaVendorAdapter;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;import org.springframework.transaction.PlatformTransactionManager;import javax.persistence.EntityManagerFactory;import java.util.Properties;/** * @author Zhai * 2019/04/02 15:10 */@Configuration@ComponentScan(basePackages = {"com"})// 指定Repository所在的包@EnableJpaRepositories(basePackages = {"com.domain"})public class JpaConfig { // 名字必须是entityManagerFactory,或者把@bean中name属性设置为entityManagerFactory @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); // 设置数据库(如果在hibernate中配置了连接池,则不需要设置)// em.setDataSource(dataSource()); // 指定Entity所在的包 em.setPackagesToScan("com.domain"); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); // 配置属性 Properties properties = new Properties(); properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); properties.setProperty("hibernate.connection.url", "jdbc:mysql://10.8.3.38:3306/test"); properties.setProperty("hibernate.connection.username", "root"); properties.setProperty("hibernate.connection.password", "root"); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); properties.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider"); properties.setProperty("hibernate.c3p0.min_size", "1"); properties.setProperty("hibernate.c3p0.max_size", "10"); properties.setProperty("hibernate.hbm2ddl.auto", "create"); properties.setProperty("hibernate.show_sql", "true"); properties.setProperty("format_sql", "true"); em.setJpaProperties(properties); return em; } // 名字必须是transactionManager,或者把@bean中name属性设置为transactionManager @Bean public PlatformTransactionManager transactionManager( EntityManagerFactory emf) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(emf); return transactionManager; }}
测试代码:
package com;import com.config.JpaConfig;import com.domain.Student;import com.domain.StudentRepository;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.util.List;/** * @author Zhai * 2019/04/02 14:27 */public class JpaTest { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(JpaConfig.class); // 获取repository StudentRepository studentRepository = context.getBean(StudentRepository.class); Student student1 = new Student(); studentRepository.save(student1); Liststudents = studentRepository.findAll(); System.out.println(students); }}