Springboot + Openjpa 整合 GBase8s 实践「建议收藏」

Springboot + Openjpa 整合 GBase8s 实践「建议收藏」本文我们将先来介绍JPA以及OpenJPA之间的关系,然后通过一个手把手的应用案例来讲述Springboot和Openjpa整合GBase8s。那么就让我们开始吧。JPAJPA(JavaPersistenceAPI)作为JavaEE5.0平台标准的ORM规范,将得到所有JavaEE服务器的支持。Sun这次吸取了之前EJB规范惨痛失败的经历,在充分吸收现有ORM框架的基础上,得到了一个易于使用、伸缩性强的ORM规范。从目前的开发社区的反应上看,JPA受到了极大…

大家好,又见面了,我是你们的朋友全栈君。

Springboot + Openjpa 整合 GBase8s 实践「建议收藏」

 

本文我们将先来介绍JPA以及OpenJPA之间的关系,然后通过一个手把手的应用案例来讲述 Springboot 和 Openjpa 整合 GBase8s 。那么就让我们开始吧。

JPA

JPA(Java Persistence API)作为Java EE 5.0平台标准的ORM规范,将得到所有JavaEE服务器的支持。Sun这次吸取了之前EJB规范惨痛失败的经历,在充分吸收现有ORM框架的基础上,得到了一个易于使用、伸缩性强的 ORM规范。从目 前的开发社区的反应上看,JPA受到了极大的支持和赞扬,JPA作为ORM领域标准化整合者的目标应该不难实现。

JPA由EJB 3.0软件专家组开发,作为 JSR-220实现的一部分。但它不囿于EJB3.0,你可以在 Web应用、甚至桌面应用中使用。JPA的宗旨是为POJO提供持久化标准规范,由此可见,经过这几年的实践探索,能够脱离容器独立运行,方便开发和测试的理念已经深入人心了。

JPA包括以下 3方面的技术:

(1)ORM映射元数据,JPA支持XML和JDK 5.0注解两种元数据的形式,元数据描述对象和表之间的映射关系,框架据此将实体对象持久化到数据库表中;

(2)JPA 的API,用来操作实体对象,执行CRUD操作,框架在后台替我们完成所有的事情,开发者从繁琐的JDBC和SQL代码中解脱出来。

(3)查询语言,这是持久化操作中很重要的一个方面,通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合。

OpenJPA

OpenJPA 是 Apache 组织提供的开源项目,它实现了 EJB 3.0 中的 JPA 标准,为开发者提供功能强大、使用简单的持久化数据管理框架。OpenJPA 封装了和关系型数据库交互的操作,让开发者把注意力集中在编写业务逻辑上。

OpenJPA 可以作为独立的持久层框架发挥作用,也可以轻松的与其它 Java EE 应用框架或者符合 EJB 3.0 标准的容器集成。

除了对 JPA 标准的支持之外,OpenJPA 还提供了非常多的特性和工具支持让企业应用开发变得更加简单,减少开发者的工作量,包括允许数据远程传输/离线处理、数据库/对象视图统一工具、使用缓存(Cache)提升企业应用效率等。

数据远程传输 / 离线处理

JPA 标准规定的运行环境是 “本地” 和 “在线” 的。本地是指 JPA 应用中的 EntityManager 必须直接连接到指定的数据库,而且必须和使用它的代码在同一个 JVM 中。在线是指所有针对实体的操作必须在一个 EntityManager 范围中运行。这两个特征,加上 EntityManager 是非序列化的,无法在网络上传输,导致 JPA 应用无法适用于企业应用中的 C/S 实现模式。OpenJPA 扩展了这部分接口,支持数据的远程传输和离线处理。

数据库 / 对象视图统一工具

使 用 OpenJPA 开发企业应用时,保持数据库和对象视图的一致性是非常重要的工作,OpenJPA 支持三种模式处理数据库和对象视图的一致性:正向映射(Forward Mapping)、反向映射(Reverse Mapping)、中间匹配(Meet-in-the-Middle Mapping),并且为它们提供了相应的工具支持。

正向映射 是指使用 OpenJPA 框架中提供的 org.apache.openjpa.jdbc.meta.MappingTool 工具从开发者提供的实体以及在实体中提供的对象 / 关系映射注释生成相应的数据库表。

反向映射 是指 OpenJPA 框架中提供的 org.apache.openjpa.jdbc.meta.ReverseMappingTool 工具从数据库表生成符合 JPA 标准要求的实体以及相应的对象 / 关系映射注释内容。

中间匹配 是指开发者负责创建数据库表、符合 JPA 标准的实体和相应的对象 / 关系映射注释内容,使用 OpenJPA 框架中提供的 org.apache.openjpa.jdbc.meta.MappingTool 工具校验二者的一致性。 使用缓存提升效率

性能是企业应用重点关注的内容之一,缓存是提升企业系统性能的重要手段之一。OpenJPA 针对数据持久化提供多种层次、多方面的缓存支持,包括数据、查询、汇编查询的缓存等。这些缓存的应用可以大幅度的提高企业应用的运行效率。

(以上引用自:百度百科)

工程实践

本案例整合 springbootopenjpa 完成基础数据操作的 restful 服务,工程目录如下 : 另外:openjpa原生并不支持gbase8s,厂商提供扩展版本,可以联系厂商获取对应的openjpa版本。

Springboot + Openjpa 整合 GBase8s 实践「建议收藏」

 

maven配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>wang.datahub</groupId>
	<artifactId>spring-boot-openjpa-gbasedbt</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>lijiaqi</name>
	<description>Spring boot app that uses openJpa with gbasedbt instead of hibernate</description>

 <!--   <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.2.5.RELEASE</version>
		<relativePath/>
	</parent>-->

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
		<openjpa.version>3.1.0</openjpa.version>
		<spring.boot.version>1.2.5.RELEASE</spring.boot.version>
		<spring.version>4.2.1.RELEASE</spring.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
			<version>${spring.boot.version}</version>
			<exclusions>
				<exclusion>
					<artifactId>hibernate-entitymanager</artifactId>
					<groupId>org.hibernate</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
			<version>${spring.boot.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.openjpa</groupId>
			<artifactId>openjpa-all</artifactId>
			<version>${openjpa.version}</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/libs/openjpa-all-3.1.0.jar</systemPath>
		</dependency>
		<dependency>
			<groupId>com.gbase</groupId>
			<artifactId>gbasedbtjdbc</artifactId>
			<version>1.0</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/libs/gbasedbtjdbc.jar</systemPath>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<version>${spring.boot.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<version>${spring.boot.version}</version>

		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
					<encoding>${project.build.sourceEncoding}</encoding>
					<compilerArgument>-Xlint:all</compilerArgument>
					<showWarnings>true</showWarnings>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>${spring.boot.version}</version>
				<dependencies>
					<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>springloaded</artifactId>
						<version>1.2.4.RELEASE</version>
					</dependency>
					<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>spring-instrument</artifactId>
						<version>${spring.version}</version>
					</dependency>
				</dependencies>
				<configuration>
					<agent>${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar</agent>
					<agent>${settings.localRepository}/org/apache/openjpa/openjpa/${openjpa.version}/openjpa-${openjpa.version}.jar</agent>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.openjpa</groupId>
				<artifactId>openjpa-maven-plugin</artifactId>
				<version>${openjpa.version}</version>
				<configuration>
					<includes>**/entity/*.class</includes>
					<addDefaultConstructor>true</addDefaultConstructor>
					<enforcePropertyRestrictions>true</enforcePropertyRestrictions>
					<sqlFile>src/main/resources/schema.sql</sqlFile>
					<persistenceXmlFile>src/main/resources/META-INF/persistence.xml</persistenceXmlFile>
				</configuration>
				<executions>
					<execution>
						<id>enhancer</id>
						<phase>process-classes</phase>
						<goals>
							<goal>enhance</goal>
						</goals>
					</execution>
				</executions>
<!--				<dependencies>-->
<!--					<dependency>-->
<!--						<groupId>org.apache.openjpa</groupId>-->
<!--						<artifactId>openjpa</artifactId>-->
<!--						<version>${openjpa.version}</version>-->
<!--					</dependency>-->
<!--				</dependencies>-->
			</plugin>
		</plugins>
	</build>
	

</project>
jpa配置,创建persistence.xml放在 src\main\resources\META-INF
<?xml version="1.0"?>
<persistence version="1.0"
             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">
​
    <persistence-unit name="persistence-unit" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<!--        <class>com.openjpa.example.entity.Employee</class>-->
<!--        <class>com.openjpa.example.entity.Manager</class>-->
        <class>wang.datahub.example.entity.Userinfo</class>
        <properties>
            <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO, SQL=TRACE" />
            <property name="openjpa.jdbc.MappingDefaults" value="IndexLogicalForeignKeys=false,IndexDiscriminator=false"/>
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
            <property name="openjpa.jdbc.DBDictionary" value="gbasedbt"/>
            <property name="openjpa.ConnectionURL"
                      value="jdbc:gbasedbt-sqli://172.24.110.229:90881/t1:GBASEDBTSERVER=ol_gbasedbt1210_1;NEWCODESET=UTF8,zh_cn.UTF8,57372;DATABASE=t1;DB_LOCALE=en_US.819;"/>
            <property name="openjpa.ConnectionDriverName"
                      value="com.gbasedbt.jdbc.Driver"/>
            <property name="openjpa.ConnectionUserName"
                      value="gbasedbt"/>
            <property name="openjpa.ConnectionPassword"
                      value="dafei1288"/>
            <property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
        </properties>
    </persistence-unit>
</persistence>
​

系统配置application.yml

server:
  port: 8088
#  context-path: /
​
#spring-jpa-data数据库配置信息   org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
spring:
  jpa:
    show-sql: true
    generate-ddl: true
    hibernate:
      ddl-auto: create-drop
  datasource:
    driver-class-name: com.gbasedbt.jdbc.Driver
    url: jdbc:gbasedbt-sqli://172.24.110.229:9088/t1:GBASEDBTSERVER=ol_gbasedbt1210_1;NEWCODESET=UTF8,zh_cn.UTF8,57372;DATABASE=t1;DB_LOCALE=en_US.819;
    username: gbasedbt
    password: dafei1288
​

创建实体类:

package wang.datahub.example.entity;
​
import javax.persistence.*;
​
@Entity
@Table
public class Userinfo {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
​
    @Column
    private String username;
​
    @Column
    private int age;
​
    public int getId() {
        return id;
    }
​
    public void setId(int id) {
        this.id = id;
    }
​
    public String getUsername() {
        return username;
    }
​
    public void setUsername(String username) {
        this.username = username;
    }
​
    public int getAge() {
        return age;
    }
​
    public void setAge(int age) {
        this.age = age;
    }
​
    @Override
    public String toString() {
        return "Userinfo{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}
​

创建UserinfoRepository 用于完成持久化操作

package wang.datahub.example.repository;
​
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import wang.datahub.example.entity.Userinfo;
​
@Repository
public interface UserinfoRepository extends JpaRepository<Userinfo, String> {
}
​

创建UserinfoService接口,以及其实现类,这里仅以增加记录和查询记录为例:

package wang.datahub.example.service;
​
import wang.datahub.example.entity.Userinfo;
​
import java.util.List;
​
public interface UserinfoService {
    List<Userinfo> getAll();
​
    Userinfo saveOne(Userinfo userinfo);
}
​
UserinfoServiceImpl实现

package wang.datahub.example.service.impl;
​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wang.datahub.example.entity.Userinfo;
import wang.datahub.example.repository.UserinfoRepository;
import wang.datahub.example.service.UserinfoService;
​
import java.util.List;
​
@Service
public class UserinfoServiceImpl implements UserinfoService {
​
    @Autowired
    private UserinfoRepository userinfoRepository;
​
    @Override
    public List<Userinfo> getAll(){
        return userinfoRepository.findAll();
    }
​
    @Override
    public Userinfo saveOne(Userinfo userinfo){
        System.out.println(userinfo);
        return userinfoRepository.saveAndFlush(userinfo);
    }
​
}

创建Rest接口 UserinfoController

package wang.datahub.example.controller;
​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import wang.datahub.example.entity.Userinfo;
import wang.datahub.example.service.UserinfoService;
​
import java.util.List;
​
@RestController
public class UserinfoController {
    @Autowired
    private UserinfoService userinfoService;
​
​
    @RequestMapping(value = "/userinfo", method = RequestMethod.GET)
    public List<Userinfo> getAll(){
        return userinfoService.getAll();
    }
​
    @RequestMapping(value="/userinfo",produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public Userinfo save(@RequestBody  Userinfo userinfo){
        return userinfoService.saveOne(userinfo);
    }
​
​
}

创建启动类 App

package wang.datahub.example;
​
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter;
import org.springframework.transaction.jta.JtaTransactionManager;
​
import javax.persistence.spi.PersistenceProvider;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
​
​
@EnableAutoConfiguration
@ComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
​
    @Configuration
    public static class GBasedbtJpaConfig extends JpaBaseConfiguration{
​
​
        @Override
        protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
            OpenJpaVendorAdapter jpaVendorAdapter = new OpenJpaVendorAdapter();
            jpaVendorAdapter.setShowSql(true);
//        jpaVendorAdapter.setDatabase(Database.INFORMIX);
            return jpaVendorAdapter;
        }
​
        @Override
        protected Map<String, Object> getVendorProperties() {
            HashMap<String, Object> map = new HashMap<String, Object>();
//        map.put("openjpa.ConnectionDriverName","com.gbasedbt.jdbc.Driver");
//        map.put("openjpa.ConnectionURL","jdbc:gbasedbt-sqli://172.24.110.229:9088/t1:GBASEDBTSERVER=ol_gbasedbt1210_1;NEWCODESET=UTF8,zh_cn.UTF8,57372;DATABASE=t1;DB_LOCALE=en_US.819;");
//        map.put("openjpa.ConnectionUserName","gbasedbt");
//        map.put("openjpa.ConnectionPassword","dafei1288");
​
            return map;
        }
    }
}
测试新增数据
POST http://localhost:8088/userinfo
Accept: application/json
Content-Type: application/json
​
{"username":  "jacky", "age":  111}

返回结果

POST http://localhost:8088/userinfo
​
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 05 Nov 2021 03:13:34 GMT
​
{
  "id": 3252,
  "username": "jacky",
  "age": 111
}
​
Response code: 200 (OK); Time: 244ms; Content length: 40 bytes
​

测试新增数据:

GET http://localhost:8088/userinfo
Accept: application/json
返回结果:
GET http://localhost:8088/userinfo
​
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 05 Nov 2021 03:12:07 GMT
​
[
  {
    "id": 3201,
    "username": "mm",
    "age": 11
  },
  {
    "id": 3202,
    "username": "dd",
    "age": 22
  },
  {
    "id": 3251,
    "username": "dd",
    "age": 22
  }
]
​
Response code: 200 (OK); Time: 33ms; Content length: 112 bytes

参考链接:

OpenJPA简介_Jerry_新浪博客

OpenJPA_百度百科

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/130500.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • Tomcat面试题+http面试题+Nginx面试题+常见面试题

    Tomcat面试题+http面试题+Nginx面试题+常见面试题Tomcat面试题1、Tomcat的缺省端口是多少?怎么修改?答:缺省端口是8080,若要修改,可以进入Tomcat的安装目录下找到conf目录下的server.xml文件,找到该文件中的Connector字段中的port。2、Tomcat有哪几种connector运行模式(服务的请求方式)?答:三种。修改它的运行模式需要在主配置文件中找到connector字段中的protocol进行修改…

  • Linux的redis启动过程详解「建议收藏」

    Linux的redis启动过程详解「建议收藏」目录redis的启动设置redis密码redis-server后台启动redis的启动在我的linux启动redis首先进入到安装redis的目录下cd/usr/local/redis/redis/redis-4.0.2/src启动redis-server服务./redis-server这种启动为前台启动,所以一般直接再VM中启动然后在Xshell中远程控制,启动redis客户端./redis-cli可以看到会有默认端口6379这时就

  • CodeForces 484A Bits

    CodeForces 484A Bits

  • 电商如何提高用户体验_购物网站的设计思路

    电商如何提高用户体验_购物网站的设计思路电子商务是一个复杂的系统,很多小伙伴在设计电商网站的时候会遇到不少的问题。作为电子商务的典型模式之一,B2C网上零售网站要考虑的因素很多,有时候很多意想不到的因素都可能决定一个用户最终是否在该网站完成购物。在消费者注意力越来越分散的今天,如果我们希望潜在客户更多的停留在自己的网站中,进而进行购买,就需要多多从用户的角度出发,提供简便愉快的购物体验,改善用户体验。以下给大家6点建议,能从一…

  • vue中父组件传值给子组件,父组件值改变,子组件不能重新渲染[通俗易懂]

    vue中父组件传值给子组件,父组件值改变,子组件不能重新渲染[通俗易懂]1在子组件中用watch()监听值的改变,不同的类型的要用不同的监听方法props:{echartStyle:{type:Object,default(){return{}}},titleText:{type:String,default:”},tooltipFormatt…

    2022年10月31日
  • 鲜为人知的QQ自动强制加好友代码

    是的,你也许见过强行聊天的代码: tencent://Message/?Uin=574201314&amp;websiteName=www.oicqzone.com&amp;Menu=yes 但是你应该不知道,还有强行加好友的代码:tencent://AddContact/?fromId=45&amp;fromSubId=1&amp;subcmd=all&amp;uin…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号