ITPub博客

首页 > 数据库 > 国内数据库 > MyBatis+SpringBoot+GBase8s 整合示例

MyBatis+SpringBoot+GBase8s 整合示例

原创 国内数据库 作者:wj_2021 时间:2021-08-23 10:33:16 0 删除 编辑

1.  创建工程

登入 ,选择如下:

然后点击GENERATE 按钮。

点击后,浏览器会下载如下压缩包:

 

将压缩包解压后,使用eclipse import 功能导入工程,如下图所示:

 

点击next 导入成功。

2.  添加依赖

pom.xml 中添加如下内容:

< dependency >

     < groupId > cn.gbase.jdbc </ groupId >

     < artifactId > gbase8s </ artifactId >

     < version > v87 </ version >

     < scope > system </ scope >

     < systemPath > E:\jar\gbasedbtjdbc_3.3.0_2_36477d.jar </ systemPath >

</ dependency >

E:\jar\gbasedbtjdbc_3.3.0_2_36477d.jar GBase8s jdbc 包在本地的存放路径。

 

3.  配置数据源

application.properties 中配置 如下配置项:

spring.datasource.driverClassName= com.gbasedbt.jdbc.Driver

spring.datasource.url= jdbc:gbasedbt- sqli :// localhost :12345/test:gbasedbtserver=ol_gbasedbt1210

spring.datasource.username= gbasedbt

spring.datasource.password= GBase123!

 

4.  创建表

在数据库test 中创建 city

create   table  city (id int eger   primary   key  , name varchar (20) , state varchar (20) , country varchar (20) ) ;

 

5.  创建类

创建City 实体类

public   class  City implements  Serializable {

 

private   static   final   long   serialVersionUID  = 1L;

 

private  Long id ;

 

private  String name ;

 

private  String state ;

 

private  String country ;

 

public  Long getId() {

return   this . id ;

}

 

public   void  setId(Long id ) {

this . id  = id ;

}

 

public  String getName() {

return   this . name ;

}

 

public   void  setName(String name ) {

this . name  = name ;

}

 

public  String getState() {

return   this . state ;

}

 

public   void  setState(String state ) {

this . state  = state ;

}

 

public  String getCountry() {

return   this . country ;

}

 

public   void  setCountry(String country ) {

this . country  = country ;

}

 

@Override

public  String toString() {

return  getId() + ","  + getName() + ","  + getState() + ","  + getCountry();

}

 

}

 

创建CityDao

@Component

public   class  CityDao {

 

private   final  SqlSession sqlSession ;

 

public  CityDao(SqlSession sqlSession ) {

this . sqlSession  = sqlSession ;

}

 

public  City selectCityById( long   id ) {

return   this . sqlSession .selectOne( "selectCityById" , id );

}

 

}

 

创建CityMapper 接口

@Mapper

public   interface  CityMapper {

 

@Select ( "select id, name, state, country from city where state = #{state}" )

City  findByState( @Param ( "state" ) String state );

 

}

 

修改 Gbase8stestApplication 类,内容如下

@SpringBootApplication

public   class  Gbase8stestApplication implements  CommandLineRunner {

private   final  CityDao cityDao ;

 

public   static   void  main(String[] args ) {

SpringApplication. run (Gbase8stestApplication. class , args );

}

 

public  Gbase8stestApplication(CityDao cityDao ) {

this . cityDao  = cityDao ;

}

 

@Override

public   void  run(String... args ) {

System. out .println( this . cityDao . selectCityById (1));

}

}

 

6.  创建xml 文件

创建CityMapper.xml

<!DOCTYPE mapper

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

        "

<mapper   namespace= "com.example.gbase8stest.mapper.CityMapper" >

     <select   id= "selectCityById"   resultType= "City" >

        select id, name, state, country from city where id = #{id}

     </select>

</mapper>

 

创建mybatis-configure.xml

<!DOCTYPE configuration

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

        "

<configuration>

     <typeAliases>

         <package   name= "com.example.gbase8stest.domain" />

     </typeAliases>

     <mappers>

         <mapper   resource= "CityMapper.xml" />

     </mappers>

</configuration>

 

7.  测试

GBase8s 中执行如下 SQL 语句:

insert   into   test: city ( id, name, state, country) values  ( 1, 'San Francisco' , 'CA' , 'US' ) ;

 

启动Gbase8stestApplication 类,输出结果如下:

  .   ____          _            __ _ _

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )

  '  |____| .__|_| |_|_| |_\__, | / / / /

 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v2.5.3)

 

2021-08-20 18:03:51.702 DEBUG 34544 --- [           main] s.m.x.mapper.CityMapper.selectCityById   : ==>  Preparing: select id, name, state, country from city where id = ?

2021-08-20 18:03:51.866 DEBUG 34544 --- [           main] s.m.x.mapper.CityMapper.selectCityById   : ==> Parameters: 1(Long)

2021-08-20 18:03:51.977 TRACE 34544 --- [           main] s.m.x.mapper.CityMapper.selectCityById   : <==    Columns: id, name, state, country

2021-08-20 18:03:51.977 TRACE 34544 --- [           main] s.m.x.mapper.CityMapper.selectCityById   : <==        Row: 1, San Francisco, CA, US

2021-08-20 18:03:51.983 DEBUG 34544 --- [           main] s.m.x.mapper.CityMapper.selectCityById   : <==      Total: 1

1,San Francisco,CA,US

从结果中可以看到查询到的结果。

 


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

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

注册时间:2021-01-25

  • 博文量
    35
  • 访问量
    11829