• Spring : JPA的单独使用


    title: 如何单独使用spring data jpa

    引用pom文件:

            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-jpa</artifactId>
                <version>2.1.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.3.5.Final</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-c3p0</artifactId>
                <version>5.3.5.Final</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>

    编写配置类:

    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);
    
            List<Student> students = studentRepository.findAll();
            System.out.println(students);
    
        }
    }
  • 相关阅读:
    linux-Windows文件上传Linux
    linux-ifconfig 查看没有IP
    springBoot 发布war/jar包到tomcat(idea)
    linux-常用命令
    (转)JVM各种内存溢出是否产生dump
    数据库缓存的几种方式
    使用jprofiler分析dump文件一个实例
    Hibernate之一级缓存和二级缓存
    最佳实践 缓存穿透,瞬间并发,缓存雪崩的解决方法
    缓存与数据库一致性之三:缓存穿透、缓存雪崩、key重建方案
  • 原文地址:https://www.cnblogs.com/cccy0/p/10647582.html
Copyright © 2020-2023  润新知