本文主要是介绍SpringDataJpa 的JavaConfig配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.写入配置类
package com.kuang.config;import com.alibaba.druid.pool.DruidDataSource;
import com.mysql.jdbc.Driver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;@Configuration
@EnableJpaRepositories(basePackages = "com.kuang.repositories") //启动JPA
@EnableTransactionManagement //开启事务
public class ApplicationConfig {@Beanpublic DataSource dataSource() {//整合DruidDruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setUsername("root");druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");druidDataSource.setUrl("jdbc:mysql://localhost:3306/springdata_jpa?useUnicode=true&useSSL=false&characterEncoding=UTF-8");druidDataSource.setPassword("2001");return druidDataSource;}@Beanpublic LocalContainerEntityManagerFactoryBean entityManagerFactory() {HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();vendorAdapter.setGenerateDdl(true);vendorAdapter.setShowSql(true);LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();factory.setJpaVendorAdapter(vendorAdapter);factory.setPackagesToScan("com.kuang.pojo");//实体类的包全限定名factory.setDataSource(dataSource());return factory;}@Beanpublic PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {JpaTransactionManager txManager = new JpaTransactionManager();txManager.setEntityManagerFactory(entityManagerFactory);return txManager;}
}
2.更换测试类
package com.kuang.test;import com.kuang.config.ApplicationConfig;
import com.kuang.pojo.Customer;
import com.kuang.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.Optional;//@ContextConfiguration("/spring.xml")
@ContextConfiguration(classes = ApplicationConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringDataJpaTest {@Autowiredprivate CustomerRepository customerRepository;@Testpublic void select() {Optional<Customer> byId = customerRepository.findById(5L);Customer customer = byId.get();System.out.println(customer);}@Testpublic void insert() {Customer customer = new Customer();customer.setCustAddress("南环路");customer.setCustName("豪哥");Customer save = customerRepository.save(customer);save.setCustAddress("刘备");System.out.println(customer);}@Testpublic void update() {Customer customer = new Customer();customer.setCustId(1L);customer.setCustAddress("栖霞路");customer.setCustName("张飞");Customer save = customerRepository.save(customer);}@Testpublic void remove() {Customer customer = new Customer();customer.setCustId(1L);customerRepository.delete(customer);}
}
这篇关于SpringDataJpa 的JavaConfig配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!