暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Springboot+JPA+Hibernate+GBase 8s示例

liaosnet 2023-02-03
757

使用的环境:
IntelliJ IDEA 2018.3.6 x64

新创建项目(New Project),选Spring Initializr,SDK使用1.8,使用默认的https://start.spring.io创建
image001.png

项目元数据:
注意Artifact,Packaging和Java Version
image002.png
Dependencies中选上Web -> Spring Web,注意选择Sping Boot的版本,这里使用的是2.7.3
image003.png
指定项目名称和路径
image004.png
完成后,生成项目。

在项目的根目录下的pom.xml中增加依赖(dependency)

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>

image005.png
新建目录jars,引入GBase 8s的jdbc驱动和hibernate方言包(需要与hibernate版本对应或者兼容)
image006.png
这里使用到hibernate版本是5.6.10,对应的GBase 8s方言包可以在https://gbasedbt.com/dl/hibernate/3.0/ 中找到。

在项目src\main\resources下的application.properties配置文件中,增加spring和jpa的配置信息

spring.datasource.driver-class-name=com.gbasedbt.jdbc.Driver spring.datasource.username=gbasedbt spring.datasource.password=GBase123 spring.datasource.url=jdbc:gbasedbt-sqli://h01.gbasedbt.com:9089/testdb:GBASEDBTSERVER=gbase01;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;IFX_LOCK_MODE_WAIT=60 # 让控制器输出json字符串格式 spring.jackson.serialization.indent-output=true spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true # 设置数据库引擎 spring.jpa.database-platform=org.hibernate.dialect.GBasedbtDialect

image007.png

创建pojo类(com.gbasedbt.springboothibernategbase8s.pojo.User.java)
image008.png
创建dao接口(com.gbasedbt.springboothibernategbase8s.dao.UserDao.java)
image009.png
创建service接口(com.gbasedbt.springboothibernategbase8s.service.UserService.java)
image010.png
创建serviceimpl类(com.gbasedbt.springboothibernategbase8s.serviceimpl.UserServiceImpl.java)
image011.png
创建Controller类(com.gbasedbt.springboothibernategbase8s.UserController.java)
image012.png

启动环境
image013.png
在浏览器中插入用户
image014.png
在浏览器中查询用户(以id为条件)
image015.png
在浏览器中删除用户(以id为条件)
image016.png
后台日志显示相应的语句
image017.png

源代码:
springboot-hibernate-gbase8s.zip

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论