美文网首页
Apache Olingo - JPA Processor Ex

Apache Olingo - JPA Processor Ex

作者: LiuliuZhang | 来源:发表于2018-01-31 15:31 被阅读0次

1 项目准备

1.1 MYSQL数据库

安装
解压下载的mysql,命令行进入\MySQL\bin
运行命令初始化mysqld --initialize-insecure --user=mysql,当出现Service successfully installed表示MySQL服务已经安装
输入:net start mysql启动服务,出现MySQL服务启动成功,表示这一次配置完毕
登陆
输入mysql -u root -p然后回车,提示输入密码,如果没有密码就回车。
常用命令
show databases;#列出所有的服务器上的数据库
create database if not exists test;#创建一个数据库
drop database test;#删除数据库
show tables from test;#显示一个数据库中的表
use test;#使用数据库
创建表

create table Employee(
  id int primary key auto_increment,
  firstName varchar(20),
  lastName varchar(20)
 );

1.2 Eclipse配置

安装Eclipse Data Tools
在Eclipse中,点击Install New Software,添加url http://download.eclipse.org/datatools/updates/1.13/1.13.0.201603142002/repository/
安装成功后,可以通过 Window->Show View->Data Source Explorer 打开,右击Database Connection->New,选中MySQL。
出现Unable to locate JAR/zip in file...错误时,可以在JAR List中删除里面的jar,再添加正确的mysql jar包。


创建Maven项目
新建Maven项目,选择war打包。创建成功后,右击项目,在Properties->Project Facts中,勾选JPA

在webapp下创建WEB-INF/web.xml文件
pom.xml
按照下表创建依赖

编辑pom.xml,然后Maven Install

  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>2.7.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.olingo</groupId>
      <artifactId>olingo-odata2-core</artifactId>
      <version>2.0.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.olingo</groupId>
      <artifactId>olingo-odata2-api</artifactId>
      <version>2.0.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.olingo</groupId>
      <artifactId>olingo-odata2-jpa-processor-api</artifactId>
      <version>2.0.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.olingo</groupId>
      <artifactId>olingo-odata2-jpa-processor-core</artifactId>
      <version>2.0.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.olingo</groupId>
      <artifactId>olingo-odata2-jpa-processor-ref</artifactId>
      <version>2.0.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>eclipselink</artifactId>
      <version>2.5.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>javax.persistence</artifactId>
      <version>2.0.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.32</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

2 实现OData Service

2.1 创建JPA Model

选中项目右击 New -> Other -> JPA Entities from Tables, 选中相应的数据库连接并选择相应的表,得到相应的model类


2.2 创建ODataJPAServiceFactory

代码如下

package main;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;

public class EmployeeListServiceFactory extends ODataJPAServiceFactory {
  private static final String PERSISTENCE_UNIT_NAME = "test-web";
  @Override
  public ODataJPAContext initializeODataJPAContext()
      throws ODataJPARuntimeException {
        ODataJPAContext oDatJPAContext = this.getODataJPAContext(); 
        try {   
          EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); 
          oDatJPAContext.setEntityManagerFactory(emf);  
          oDatJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME); 
          return oDatJPAContext;    
        } catch (Exception e) { 
          throw new RuntimeException(e);    
        }

  }

}

2.3 配置web.xml

配置Servlet,拦截/emplist.svc/*请求

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    id="WebApp_ID" version="3.0">  
    <display-name>RestProjectTest</display-name>  
    <servlet>  
        <servlet-name>ODataServlet</servlet-name>  
        <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>  
        <init-param>  
            <param-name>javax.ws.rs.Application</param-name>  
            <param-value>org.apache.olingo.odata2.core.rest.app.ODataApplication</param-value>  
        </init-param>  
        <init-param>  
            <param-name>org.apache.olingo.odata2.service.factory</param-name>  
            <param-value>main.EmployeeListServiceFactory</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>ODataServlet</servlet-name>  
        <url-pattern>/emplist.svc/*</url-pattern>  
    </servlet-mapping>  
</web-app>    

2.4 配置JPA的persistence.xml文件

配置model类及数据库连接属性

    <persistence-unit name="test-web" transaction-type="RESOURCE_LOCAL">
          <class>model.Employee</class>
          <properties>
                 <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test"/>
                 <property name="javax.persistence.jdbc.user" value="root"/>
                 <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
          </properties>
       </persistence-unit>

2.5 运行项目

部署到Tomcat服务器,通过相关url查看


相关文章

网友评论

      本文标题:Apache Olingo - JPA Processor Ex

      本文链接:https://www.haomeiwen.com/subject/xumxzxtx.html