123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /*
- * ********************************************************************************************************************
- *
- * iFHS7.
- * ;BBMBMBMc rZMBMBR BMB
- * MBEr:;PBM, 7MBMMEOBB: BBB RBW
- * XK: BO SB. :SZ MBM. c;; ir BBM :FFr :SSF: ;xBMB:r iuGXv. i:. iF2;
- * DBBM0r. :D S7 ;XMBMB GMBMu. MBM: BMB MBMBBBMBMS WMBMBMBBK MBMBMBM BMBRBMBW .MBMBMBMBB
- * :JMRMMD .. , 1MMRM1; ;MBMBBR: MBM ;MB: BMB: MBM. RMBr sBMH BM0 UMB, BMB. KMBv
- * ;. XOW B1; :uM: 1RE, i .2BMBs rMB. MBO MBO JMB; MBB MBM BBS 7MBMBOBM: MBW :BMc
- * OBRJ.SEE MRDOWOR, 3DE:7OBM . ;BMB RMR7BM BMB MBB. BMB ,BMR .BBZ MMB rMB, BMM rMB7
- * :FBRO0D0 RKXSXPR. JOKOOMPi BMBSSWBMB; BMBB: MBMB0ZMBMS .BMBOXRBMB MBMDE RBM2;SMBM; MBB xBM2
- * iZGE O0SHSPO. uGZ7. sBMBMBDL :BMO OZu:BMBK, rRBMB0; ,EBMB xBMBr:ER. RDU :OO;
- * ,BZ, 1D0 RPSFHXR. xWZ .SMr . .BBB
- * :0BMRDG RESSSKR. 2WOMBW; BMBMR
- * i0BM: SWKHKGO MBDv
- * .UB OOGDM. MK, Copyright (c) 2015-2019. 斯伯坦机器人
- * , XMW ..
- * r All rights reserved.
- *
- * ********************************************************************************************************************
- */
- package com.persagy.mybatis
- import com.persagy.base.extensions.toJson
- import com.persagy.database.*
- import com.persagy.database.extensions.keyColumnList
- import com.persagy.database.extensions.keyFieldList
- import com.persagy.mybatis.extensions.toInsertRow
- import com.persagy.mybatis.extensions.toUpdateRow
- import org.slf4j.LoggerFactory
- /**
- * Mybatis基础Dao操作
- *
- * @author 庞利祥 <sybotan@126.com>
- */
- abstract class SAbstractMybatisDao<ENTITY: Any>(entityClazz: Class<ENTITY>) : SAbstractDao<ENTITY>(entityClazz) {
- /** 静态对象 */
- companion object {
- /** 日志记录器 */
- private val logger = LoggerFactory.getLogger(SAbstractMybatisDao::class.java)
- } // companion object
- /** sql映射对象*/
- abstract val mapper: IBaseMapper<ENTITY>
- /** 读取记录行解析器 */
- private val rowParser = SRowParser(entityClazz)
- /**
- * 在数据库中插入实体
- *
- * @param entity 插入数据库的实体
- * @return 手入是否成功
- */
- override fun insert(entity: ENTITY): Boolean {
- val entityMap = entity.toInsertRow()
- val result = mapper.insert(entityClass, entityMap)
- try {
- if (entityMap.containsKey("_id")) {
- val id = entityMap["_id"]
- val keyField = entity::class.java.keyFieldList()[0]
- val accessible = keyField.isAccessible //canAccess(entity)
- keyField.isAccessible = true
- keyField.set(entity, id)
- keyField.isAccessible = accessible
- }
- } catch (e: Exception) {
- // DO NOTHING
- }
- return result
- } // Fun insert()
- /**
- * 在数据库中插入实体
- *
- * @param entity 插入数据库的实体
- * @return 手入是否成功
- */
- override fun replace(entity: ENTITY): Boolean {
- return try {
- mapper.replace(entityClass, entity.toInsertRow(), entity.javaClass.keyColumnList())
- } catch (e: Exception) {
- e.printStackTrace()
- false
- }
- } // Fun replace()
- /**
- * 根据条件删除
- *
- * @param args 删除条件
- * @return 删除是否成功
- */
- override fun delete(args: List<SFilter>): Boolean {
- return try {
- mapper.delete(entityClass, args, HashMap())
- } catch (e: Exception) {
- e.printStackTrace()
- false
- }
- } // Fun delete()
- /**
- * 删除所有记录
- *
- * @return 删除是否成功
- */
- override fun deleteAll(): Boolean {
- return try {
- mapper.deleteAll(entityClass)
- } catch (e: Exception) {
- e.printStackTrace()
- false
- }
- } // Fun deleteAll()
- /**
- * 根据条件更新
- *
- * @param projection 将被更新的字段列表(以逗号分隔),为空表示更新所有非空字段
- * @param args 删除条件
- * @return 更新是否成功
- */
- override fun update(entity: ENTITY, projection: ArrayList<String>?, args: List<SFilter>?): Boolean {
- return try {
- mapper.update(entityClass, entity.toUpdateRow(), projection, args, HashMap())
- } catch (e: Exception) {
- e.printStackTrace()
- false
- }
- } // Fun update()
- /**
- * 根据条件查询记录
- *
- * @param args 查询条件
- * @return 查询构造器
- */
- override fun select(args: List<SFilter>): SQueryBuilder<ENTITY> {
- // 以下循环解决 “SingletonList cannot be cast to java.util.ArrayList” 错误。
- val filterList = ArrayList<SFilter>()
- args.forEach {
- filterList.add(it)
- }
- return SQueryBuilder(this, filterList)
- } // Fun select()
- /**
- * 根据条件查询所有记录
- *
- * @return 查找的记录
- */
- override fun selectAll(): SQueryBuilder<ENTITY> {
- val sQueryBuilder = SQueryBuilder(this, ArrayList())
- // sQueryBuilder.count = 999999999
- sQueryBuilder.limit(999999999)
- logger.debug("查询记录:${sQueryBuilder.toJson()}")
- return sQueryBuilder
- } // Fun selectAll()
- /**
- * 执行查询操作
- *
- * @param builder 查询查询构造器
- * @return 查询到的记录
- */
- override fun execQuery(builder: SQueryBuilder<ENTITY>): ArrayList<ENTITY> {
- val result = ArrayList<ENTITY>()
- for (row in mapper.execQuery(entityClass, builder, HashMap())) {
- try {
- result.add(rowParser.parseRow(row))
- } catch (e: Exception) {
- // DO NOTHING
- }
- }
- return result
- } // Fun exec()
- /**
- * 执行统计个数操作
- *
- * @param builder 查询查询构造器
- * @return 查询到的记录
- */
- override fun execCount(builder: SQueryBuilder<ENTITY>): Long {
- val list = mapper.execCount(entityClass, builder, HashMap())
- if (list.size == 1) {
- return list[0]
- }
- return list.size.toLong()
- } // Fun execCount()
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // 级联操作相关
- /**
- * 查询级联关系
- *
- * @param table 表名
- * @param id1 对象表ID字段名
- * @param id2 级联对象ID字段名
- * @param self 自身级联
- * @param filter 附加过滤条件
- * @param idList 对象ID列表
- * @return 关系列表
- */
- override fun relationQuery(table: String, id1: String, id2: String, self: Boolean, filter: String, idList: ArrayList<Any>): ArrayList<HashMap<String, Any>> {
- return mapper.relationQuery(table, id1, id2, self, filter, idList, HashMap());
- } // Fun cascadeIdList()
- } // Class SAbstractMybatisDao
|