zoukankan      html  css  js  c++  java
  • 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);
    
        }
    }
  • 相关阅读:
    (离线算法 LCA) hdu 2874
    (树形DP) hdu 4118
    (树的重心) poj 1655
    (线性基) bzoj 2115
    (线性基) bzoj 2460
    (边双联通+树直径) hdu 4612
    (拓扑图+DP) poj 3249
    (求割点和除去割点后的联通块数目) poj 1523
    (边双联通) poj 3352
    (DP求最长路) hdu 4607
  • 原文地址:https://www.cnblogs.com/cccy0/p/10647582.html
Copyright © 2011-2022 走看看