蚀struts2的休眠JPA配置(eclipse struts2 hibernate jpa con

2019-10-29 12:23发布

我使用Eclipse来构建网络应用程序,其中我想使用的框架支柱和冬眠。 我不使用maven这一次,只是因为我想知道如何做,如果我不使用Maven的它的工作。

版:
2.5支柱
冬眠5.2.14

现在,工作组是:

META-INF和WEB-INF是:

如图所示,图像,persistence.xml中是在META-INF和web.xml文件是在WEB-INF。

web.xml文件(在web应用程序标记的所有部分):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <display-name>LearningStruts</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

struts.xml中(后DOCTYPE):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.locale" value="zh_CN"></constant>
    <constant name="struts.custom.i18n.resources" value="mess" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
</struts>

因为我用的注解,所有的configs很短。

persistence.xml中(持久性的所有部分):

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
          http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
    version="2.1">

    <persistence-unit name="CRM">
        <description>
            Persistence unit for Hibernate User Guide
        </description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" 
                value="jdbc:mysql://localhost:3306/test_db" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

在一个类中,我使用注释注入的EntityManagerFactory:

public class UserDao {

    @PersistenceUnit(unitName = "CRM")
    private EntityManagerFactory emf;

    public void insert() {
        System.out.println(emf);
    }
}

但输出为null。 为什么?

Answer 1:

这是你可以做什么来创建EntityManager

EntityManager entityManager = Persistence.createEntityManagerFactory("CRM").createEntityManager();

注释可能不使用豆。 您可以了解更多如何框架例如从整合开始 。

您还可以阅读这个答案与Spring集成的例子。



Answer 2:

这个班为了能在它注入,要么是被管理@Statelss@StatefulServlet ...或Spring管理的,类似的东西



文章来源: eclipse struts2 hibernate jpa configuration