Kotlin语言写用Bee查询数据库的例子(多表关联查询)

原创
10/14 11:23
阅读数 9

用Kotlin语言写Bee查询数据库的例子(多表关联查询)

Tables表:

 Columns表:

 

package com.k.test

import org.teasoft.bee.osql.annotation.JoinTable;
import org.teasoft.bee.osql.annotation.JoinType;
import java.io.Serializable;

data class Tables(
var tableId: Int ?= null,
var tableName : String ?= null,
@JoinTable (mainField = "table_id",
subField = "table_id",
joinType = JoinType.JOIN
,subClass = "Columns"
)
var columns:List<Columns> ?= null,
//var columns:Columns ?= null,
): Serializable

 

package com.k.test

import java.io.Serializable;

data class Columns(
	var tableId: Int? = null,
	var columnId: Int? = null,
	var columnName: String? = null
) : Serializable

 


import com.k.test.Columns
import com.k.test.Tables
import org.teasoft.honey.osql.core.BeeFactory

fun main(){
	
val suid = BeeFactory.getHoneyFactory().suidRich
	println(" ------ query tables-------")
println(suid.select(Tables(tableId=1)))
	println("------query columns -------")
println(suid.select(Columns(tableId = 1)))
val moreTable = BeeFactory.getHoneyFactory().moreTable
println(moreTable.select(Tables(tableId = 1)))
}

 

 

日志:

 ------ query tables-------
[Bee] LoggerFactory Use the Logger is : org.teasoft.honey.logging.SystemLogger
[INFO] [Bee] ========= get the dbName from the Connection is :MySQL
[INFO] [Bee] select SQL: select table_id,table_name from tables where table_id=?   [values]: 1
[INFO] [Bee] select SQL:  ( ExecutableSql )
select table_id,table_name from tables where table_id=1
[INFO] [Bee]  | <--  select rows: 1
[Tables(tableId=1, tableName=t1, columns=null)]
------query columns -------
[INFO] [Bee] select SQL: select table_id,column_id,column_name from columns where table_id=?   [values]: 1
[INFO] [Bee] select SQL:  ( ExecutableSql )
select table_id,column_id,column_name from columns where table_id=1
[INFO] [Bee]  | <--  select rows: 1
[Columns(tableId=1, columnId=1, columnName=c1)]
[INFO] [Bee] select SQL: select tables.table_id,tables.table_name,columns.table_id,columns.column_id,columns.column_name from tables,columns where tables.table_id=columns.table_id and tables.table_id=?   [values]: 1
[INFO] [Bee] select SQL:  ( ExecutableSql )
select tables.table_id,tables.table_name,columns.table_id,columns.column_id,columns.column_name from tables,columns where tables.table_id=columns.table_id and tables.table_id=1
[INFO] [Bee]  | <--  ( select raw record rows: 1 )
[INFO] [Bee]  | <--  select rows: 1
[Tables(tableId=1, tableName=t1, columns=[Columns(tableId=1, columnId=1, columnName=c1)])]

 

 

下面讨论下,

//var columns:List<Columns> ?= null,
var columns:Columns ?= null,

用List和不用List的区别;

在Columns表添加一条记录:

在data class Tables里,修改以下两条语句

//var columns:List<Columns> ?= null,
var columns:Columns ?= null,

,然后运行:

 ------ query tables-------
[Bee] LoggerFactory Use the Logger is : org.teasoft.honey.logging.SystemLogger
[INFO] [Bee] ========= get the dbName from the Connection is :MySQL
[INFO] [Bee] select SQL: select table_id,table_name from tables where table_id=?   [values]: 1
[INFO] [Bee] select SQL:  ( ExecutableSql )
select table_id,table_name from tables where table_id=1
[INFO] [Bee]  | <--  select rows: 1
[Tables(tableId=1, tableName=t1, columns=null)]
------query columns -------
[INFO] [Bee] select SQL: select table_id,column_id,column_name from columns where table_id=?   [values]: 1
[INFO] [Bee] select SQL:  ( ExecutableSql )
select table_id,column_id,column_name from columns where table_id=1
[INFO] [Bee]  | <--  select rows: 2
[Columns(tableId=1, columnId=1, columnName=c1), Columns(tableId=1, columnId=2, columnName=C2)]
[INFO] [Bee] select SQL: select tables.table_id,tables.table_name,columns.table_id,columns.column_id,columns.column_name from tables,columns where tables.table_id=columns.table_id and tables.table_id=?   [values]: 1
[INFO] [Bee] select SQL:  ( ExecutableSql )
select tables.table_id,tables.table_name,columns.table_id,columns.column_id,columns.column_name from tables,columns where tables.table_id=columns.table_id and tables.table_id=1
[INFO] [Bee]  | <--  select rows: 2
[Tables(tableId=1, tableName=t1, columns=Columns(tableId=1, columnId=1, columnName=c1)), Tables(tableId=1, tableName=t1, columns=Columns(tableId=1, columnId=2, columnName=C2))]

用数据库客户端查询:

 得到两条记录, 和用Bee查询回来的是一样的.

 

 

有时候, 我们只想显示一条主数据,  当有需要时,点击再显示详情.  那这时, List就派上用场了.

在data class Tables里,使用语句:

var columns:List<Columns> ?= null,

运行:

 ------ query tables-------
[Bee] LoggerFactory Use the Logger is : org.teasoft.honey.logging.SystemLogger
[INFO] [Bee] ========= get the dbName from the Connection is :MySQL
[INFO] [Bee] select SQL: select table_id,table_name from tables where table_id=?   [values]: 1
[INFO] [Bee] select SQL:  ( ExecutableSql )
select table_id,table_name from tables where table_id=1
[INFO] [Bee]  | <--  select rows: 1
[Tables(tableId=1, tableName=t1, columns=null)]
------query columns -------
[INFO] [Bee] select SQL: select table_id,column_id,column_name from columns where table_id=?   [values]: 1
[INFO] [Bee] select SQL:  ( ExecutableSql )
select table_id,column_id,column_name from columns where table_id=1
[INFO] [Bee]  | <--  select rows: 2
[Columns(tableId=1, columnId=1, columnName=c1), Columns(tableId=1, columnId=2, columnName=C2)]
[INFO] [Bee] select SQL: select tables.table_id,tables.table_name,columns.table_id,columns.column_id,columns.column_name from tables,columns where tables.table_id=columns.table_id and tables.table_id=?   [values]: 1
[INFO] [Bee] select SQL:  ( ExecutableSql )
select tables.table_id,tables.table_name,columns.table_id,columns.column_id,columns.column_name from tables,columns where tables.table_id=columns.table_id and tables.table_id=1
[INFO] [Bee]  | <--  ( select raw record rows: 2 )
[INFO] [Bee]  | <--  select rows: 1
[Tables(tableId=1, tableName=t1, columns=[Columns(tableId=1, columnId=1, columnName=c1), Columns(tableId=1, columnId=2, columnName=C2)])]

一条主数据里,关联了一个List, 而List里有两条数据.

 

注: 从表用List类型, 从V1.9.8开始.

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部