eclipse struts2休眠jpa配置 [英] eclipse struts2 hibernate jpa configuration

查看:128
本文介绍了eclipse struts2休眠jpa配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用eclipse构建一个Web应用程序,我想在其中使用框架Struts和Hibernate.这次我不使用Maven,只是因为我想知道如果不使用Maven,如何使它工作.

版本:
支撑杆2.5
冬眠5.2.14

现在,工作集是:

META-INF和WEB-INF是:

如图所示,persistence.xml位于META-INF中,而web.xml文件位于WEB-INF中.

web.xml(所有部分都在web-app标签中):

<?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>

因为我使用注释,所以所有配置都很短.

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);
    }
}

,但输出为空. 为什么?

解决方案

这是创建EntityManager的方法:

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

bean可能不使用注释.您可以从入门了解更多信息.

您还可以阅读答案,以获取与Spring集成的示例.

I'm using eclipse to build a web app, in which I want to use the framework struts and hibernate. I'm not using maven this time, only because I want to know how to make it work if I do not use maven.

Version:
struts 2.5
hibernate 5.2.14

Now, the work set is:

META-INF and WEB-INF is:

As the image shown, persistence.xml is in META-INF and a web.xml file is in WEB-INF.

web.xml (all part in web-app tag):

<?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 (after 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>

Because I use annotation, all the configs are short.

persistence.xml (all part in persistence) is:

<?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>

In a class, I use annotation to inject EntityManagerFactory:

public class UserDao {

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

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

but the output is null. Why?

解决方案

This is what you can do to create EntityManager:

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

The annotation might not used by the bean. You can learn more how to integrate frameworks example from Getting Started.

You can also read this answer for examples integration with Spring.

这篇关于eclipse struts2休眠jpa配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆