ITPub博客

首页 > 数据库 > 国内数据库 > 如何通过mybatis+GBase8s jdbc实现GBase8s数据库操作

如何通过mybatis+GBase8s jdbc实现GBase8s数据库操作

原创 国内数据库 作者:wj_2021 时间:2021-09-23 10:47:30 0 删除 编辑

      mybatis是一款在持久层使用的SQL映射框架,可以将SQL语句单独写在XML配置文件中,或者使用带有注解的Mapper映射类来完成数据库记录到Java实体的映射。它支持定制化SQL、存储过程及高级映射。MyBatis框架是对JDBC轻量级的封装,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。

       本文通过实例介绍如何通过mybatis中间件实现连接GBase8s数据库,实现数据查询与添加过程。

 

1、首先,工程中引入mybatis包文件,以及GBase8s驱动程序。

 

2 配置mybatis-config.xml文件

<? xml   version = "1.0"   encoding = "UTF-8" ?>

<! DOCTYPE   configuration

   PUBLIC   "-//mybatis.org//DTD Config 3.0//EN"  

   " >

< configuration >

< properties >

< property   name = "username"   value = "gbase"   />

< property   name = "password"   value = " ********* "   />

</ properties >

< settings >

< setting   name = "mapUnderscoreToCamelCase"   value = "false"   />

< setting   name = "logImpl"   value = "STDOUT_LOGGING"   />

</ settings >

< typeAliases >

< typeAlias   type = "model.SysUser"   alias = "SysUser"   />

< typeAlias   type = "model.User"   alias = "User"   />

< typeAlias   type = "model.Role"   alias = "Role"   />

</ typeAliases >

< environments   default = "development" >

< environment   id = "development" >

< transactionManager   type = "JDBC" ></ transactionManager >

< dataSource   type = "POOLED" >

< property   name = "driver"   value = "com.gbase.jdbc.Driver"   />

< property   name = "url"

value = " jdbc:gbasedbt-sqli://1 27.0.0.1 :5555/test:GBASEDBTSERVER=ol_gbasedbt1210_2; "   />

< property   name = "username"   value = "${username}"   />

< property   name = "password"   value = "${password}"   />

</ dataSource >

</ environment >

</ environments >

< mappers >

< mapper   resource = "mapper/UserMapper.xml"   />

</ mappers >

</ configuration >

【配置dataSource 数据源】

1)url 属性,配置为 GBase8s 连接地址。

格式如:

jdbc:gbasedbt-sqli://ip: port /test:GBASEDBTSERVER= 实例名 ;

其中jdbc:gbasedbt-sqli: 为协议名称,

IP :数据库服务器 ip 地址

port :数据库服务器 端口地址

GBASEDBTSERVER 字段表示内容为 GBase8s 实例名称。

2) username 属性,为 GBase8s 数据库用户名

3) password 属性,为 GBase8s 数据库密码

【配置Mapping 文件】

mapper/UserMapper.xml 文件如下:

 

<? xml   version = "1.0"   encoding = "UTF-8" ?>

<! DOCTYPE   mapper

   PUBLIC   "-//mybatis.org//DTD Mapper 3.0//EN"

   " >

< mapper   namespace = "demo.IUserOperation" >

< select   id = "query"   parameterType = "int"   resultMap = "syncUser" >

select

company_id ,office_id

from user where id=#{id}

</ select >

< insert   id = "insertUser"   parameterType = "User" >

INSERT INTO USER

(id,company_id,office_id)

VALUES (#{id}, #{role.company}, #{role.office})

</ insert >

< select   id = "find"   resultMap = "syncUser" >

select * from user

< where >

< foreach   collection = "ids"   open = " and id in("   close = ")"   item = "id"

separator = "," >

#{id}

</ foreach >

</ where >

</ select >

< resultMap   id = "syncUser"   type = "User" >

< result   property = "company_id"   column = "company_id"   />

< result   property = "office_id"   column = "office_id"   />

</ resultMap >

 

</ mapper >

3、配置实体类

package  demo;

import  java.util.List;

import  model.User;

public   interface   IUserOperation  {

public   int  insertUser(User User );

public  User query( int   id );

public  List<User> find(User User );

}

 

4、执行用例

1 )查询 GBase8s 数据表 -- 获取指定用户信息

public   void  getUser(){

SqlSession session  = MybatisUtils. getSqlSession ();

IUserOperation vOperation  = session .getMapper(IUserOperation. class );

User u  = vOperation .query(1);

MybatisUtils. closeSession ( session );

}

2 )插入 GBase8s 数据表 -- 新建用户

public   void  insertUser(){

int   recordCount  = 0;

SqlSession session  = MybatisUtils. getSqlSession ();

IUserOperation vOperation  = session .getMapper(IUserOperation. class );

User u  = new  User();

u .setCompany_id( "test" );

u .setOffice_id( "test" );

recordCount  += vOperation .insertUser( u );

session .commit();

session .close();

System. out .print( recordCount );

}

通过以上步骤实现通过Mybatis中间件,插入查询数据。

 

 

 


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69993860/viewspace-2793210/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2021-01-25

  • 博文量
    35
  • 访问量
    11762