object Employees : Table<Nothing>("t_employee") { val id = int("id").primaryKey() val name = varchar("name") val job = varchar("job") val managerId = int("manager_id") val hireDate = date("hire_date") val salary = long("salary") val departmentId = int("department_id") }
5.使用
可在任意位置通过 @Inject 注入直接使用
1 2 3 4 5 6 7 8
@Inject lateinitvar database: Database
func test(){ for (row in database.from(Employees).select()) { println(row[Employees.name]) } }
/** * Created by vince on Jun 15, 2022. */ abstractclassBaseService<E : Entity<E>, T : Table<E>>(privateval tableObject: T) { @Inject protectedlateinitvar database: Database
/** * Insert the given entity into the table and return the effected record number. */ openfunadd(entity: E): Int { return database.sequenceOf(tableObject).add(entity) }
/** * Update properties of the given entity to the table and return the effected record number. */ openfunupdate(entity: E): Int { return database.sequenceOf(tableObject).update(entity) }
/** * Delete records that satisfy the given [predicate]. */ openfundeleteIf(predicate: (T) -> ColumnDeclaring<Boolean>): Int { return database.sequenceOf(tableObject).removeIf(predicate) }
/** * Return true if all records match the given [predicate]. */ openfunallMatched(predicate: (T) -> ColumnDeclaring<Boolean>): Boolean { return database.sequenceOf(tableObject).all(predicate) }
/** * Return true if at least one record match the given [predicate]. */ openfunanyMatched(predicate: (T) -> ColumnDeclaring<Boolean>): Boolean { return database.sequenceOf(tableObject).any(predicate) }
/** * Return true if no records match the given [predicate]. */ openfunnoneMatched(predicate: (T) -> ColumnDeclaring<Boolean>): Boolean { return database.sequenceOf(tableObject).none(predicate) }
/** * Return the number of records in the table. */ openfuncount(): Int { return database.sequenceOf(tableObject).count() }
/** * Return the number of records matching the given [predicate] in the table. */ openfuncount(predicate: (T) -> ColumnDeclaring<Boolean>): Int { return database.sequenceOf(tableObject).count(predicate) }
/** * Return an entity object matching the given [predicate]. */ openfunfindOne(predicate: (T) -> ColumnDeclaring<Boolean>): E? { return database.sequenceOf(tableObject).find(predicate) }
/** * Return a list of entities matching the given [predicate]. */ openfunfindList(predicate: (T) -> ColumnDeclaring<Boolean>): List<E> { return database.sequenceOf(tableObject).filter(predicate).toList() }
/** * Return all entities in the table. */ openfunfindAll(): List<E> { return database.sequenceOf(tableObject).toList() } }