• jpa动态创建EntityManagerFactory 态设置数据库连接 EntityManager;



    //jpa动态创建EntityManagerFactory 态设置数据库连接EntityManager;createEntityManagerFactory(String persistenceUnitName, Map properties)方法中properties的参数将覆盖persistence.xml文件配置的参数。
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("hibernate.connection.url","jdbc:sqlserver://localhost;DatabaseName=test");
    properties.put("hibernate.connection.username","sa");
    properties.put("hibernate.connection.password","1234");
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaper", properties);
    EntityManager em=emf.createEntityManager();

    ======persistence.xml=============================
    <?xml version="1.0" encoding="UTF-8"?>

    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
     version="1.0">

     <!--定义持久单元 -->
     <persistence-unit name="jpaper"
      transaction-type="RESOURCE_LOCAL">
      <!--这里采用Hibernate提供的持久化类 -->
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <!--定义持久化属性 -->
      <properties>
       <!--定义方言、fetch深度、是否显示sql -->
       <property name="hibernate.dialect" value="org.hibernate.dialect.SybaseDialect" />
       <property name="hibernate.max_fetch_depth" value="3" />
       <property name="hibernate.show_sql" value="true" />
       <!--定义是否自动生成表,create表示每次加载都重新生成,update表示每次加载只是更新表 -->
       <!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
       <!--定义数据库连接的相关属性 -->
       <property name="hibernate.connection.driver_class"
        value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
       <property name="hibernate.connection.url"
        value="jdbc:sqlserver://192.168.1.181;DatabaseName=smartbusiness" />
       <!--定义数据库连接的用户名、密码等 -->
       <property name="hibernate.connection.username" value="sa" />
       <property name="hibernate.connection.password" value="1.0" />
      </properties>
     </persistence-unit>
    </persistence>

  • 相关阅读:
    项目总结
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    浅谈Vue与swiper轮播图框架结合小案例
  • 原文地址:https://www.cnblogs.com/qgc88/p/3467227.html
Copyright © 2020-2023  润新知