JavaSE中使用JPA(Hibernate 5.0 + mysql)框架

项目背景

项目是Spark程序,原先的项目采用的是jdbc的形式连接数据库,十分繁琐且并发性能十分孱弱,故找出了在非Spring环境中使用JPA的方法

使用方法

  1. maven依赖
            
        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-entitymanagerartifactId>
            <version>${project.hibernate.version}version>
        dependency>
        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-validatorartifactId>
            <version>5.2.4.Finalversion>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.46version>
        dependency>
    
  2. 新建persistence.xml文件
    在resources下新建META_INF文件夹,新建persistence.xml,文件内容如下
	
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
        
        <provider>org.hibernate.ejb.HibernatePersistenceprovider>
        <properties>
            
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            
            <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="root"/>
            
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            
            <property name="hibernate.show_sql" value="true"/>
            
            <property name="hibernate.format_sql" value="true"/>
            
            <property name="hibernate.use_sql_comments" value="false"/>
            
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        properties>
    persistence-unit>
persistence>

JPA工厂

建立单例的JPA工厂,只在第一次调用的时候生成,另外在实体类上注解@Entity(name = “user”),name的属性是数据库表名

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
* 类描述: 主要是获取JPA工厂
 *
 *  开启事务 EntityTransaction transaction = entityManager.getTransaction();
 *          transaction.begin();
 *          entityManager.persist(blackIp);
 *  提交事务 transaction.commit();
* @author licanfeng
* @date 2019/3/22 14:27
* @version 1.0
*/
public enum JPAFactoryEnum {
    /**
     * 枚举实现单例获取工厂
     */
    INSTANCE;

    /**
     * 1.创建EntityManagerFactory 实体管理器工厂 EntityManagerFactory是获得实体管理器EntityManager对象的入口
     */
    private static EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa");

    /**
     * 获取JPA
     * @return
     */
    public EntityManagerFactory  getEntityManagerFactory() {
        return entityManagerFactory;
    }

}

性能测试

    public void test1() {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        //测试结果 单机情况下 10w条数据插入两分钟
        for (int i = 0; i < 100000; i++) {
            Runnable syncRunnable = new Runnable() {
                @Override
                public void run() {
                    EntityManager entityManager = JPAFactoryEnum.INSTANCE.getEntityManagerFactory().createEntityManager();
                    //3.开启事务
                    EntityTransaction transaction = entityManager.getTransaction();
                    transaction.begin();
                    //4.进行持久化操作
                    User user= new User ();
                    //内部类只能访问外部类中的成员变量,不能访问方法中定义的变量,如果要访问方法中的变量,就要把方法中的变量声明为final
                    user.setId(String.valueOf(UUID.randomUUID()));
                    user.setNumber(100);
                    user.setIs_black_party(1);
                    user.setDelete_flag(0);
                    user.setGmt_create(new Date());
                    user.setGmt_update(new Date());
                    entityManager.persist(user);
                    //5.提交事务
                    transaction.commit();
                    entityManager.close();
                }
            };
            executorService.execute(syncRunnable);
        }
    }


}

测试结果表明,使用ORM框架有效提高了并发性,原JDBC方式超过100个并发就会导致数据库连接过多

你可能感兴趣的:(Spark)