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 {

    @Bean
    public DataSource dataSource() {
        //整合Druid
        DruidDataSource 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;

    }

    @Bean
    public 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;
    }

    @Bean
    public 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 {

    @Autowired
    private CustomerRepository customerRepository;

    @Test
    public void select() {
        Optional byId = customerRepository.findById(5L);
        Customer customer = byId.get();
        System.out.println(customer);
    }

    @Test
    public void insert() {
        Customer customer = new Customer();
        customer.setCustAddress("南环路");
        customer.setCustName("豪哥");
        Customer save = customerRepository.save(customer);
        save.setCustAddress("刘备");
        System.out.println(customer);
    }

    @Test
    public void update() {
        Customer customer = new Customer();
        customer.setCustId(1L);
        customer.setCustAddress("栖霞路");
        customer.setCustName("张飞");
        Customer save = customerRepository.save(customer);
    }

    @Test
    public void remove() {
        Customer customer = new Customer();
        customer.setCustId(1L);

         customerRepository.delete(customer);
    }
}

你可能感兴趣的:(hibernate,hibernate)