Eclipse Struts 2 Hibernate JPA配置 [英] Eclipse Struts 2 Hibernate JPA configuration

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

问题描述

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

版本:

  • Struts 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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsdid ="WebApp_ID";版本="3.1"<显示名称> LearningStruts</显示名称><过滤器><过滤器名称> struts2</过滤器名称>< filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><过滤器映射><过滤器名称> struts2</过滤器名称>< url-pattern>/*</url-pattern></filter-mapping></web-app> 

struts.xml (在DOCTYPE之后):

 <?xml version ="1.0"encoding ="UTF-8"?<!DOCTYPE撑杆公开-//Apache Software Foundation//DTD Struts配置2.5//EN";"http://struts.apache.org/dtds/struts-2.5.dtd">< struts><常数名称="struts.locale".value ="zh_CN"</constant><常量名称="struts.custom.i18n.resources"value ="mess"/><常数名称="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/persistencehttp://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd版本="2.1"<持久性单元名称="CRM"><说明>Hibernate用户指南的持久性单元</说明>< provider> org.hibernate.jpa.HibernatePersistenceProvider</provider><属性><属性名称="javax.persistence.jdbc.driver"值="com.mysql.jdbc.Driver";/><属性名称="javax.persistence.jdbc.url"值="jdbc:mysql://localhost:3306/test_db"/><属性名称="javax.persistence.jdbc.user"value ="root"/><属性名称="javax.persistence.jdbc.password"value ="root"/><属性名称="hibernate.show_sql";值=真"/><属性名称="hibernate.hbm2ddl.auto";值=更新";/></properties></persistence-unit></persistence> 

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

 公共类UserDao {@PersistenceUnit(unitName ="CRM")私有EntityManagerFactory emf;公共无效insert(){System.out.println(emf);}} 

,但输出为 null .

为什么?

解决方案

这是创建 EntityManager 的方法:

  EntityManagerEntityManager = Persistence.createEntityManagerFactory("CRM").createEntityManager(); 

bean可能不使用注释.您可以从入门了解更多有关如何集成框架示例的信息.

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

I'm using Eclipse to build a web app, in which I want to use the framework Struts 2 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 Struts 2 Hibernate JPA配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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