DAO层框架介绍:gavin

特性:

1.支持分表分库

2.支持读写分离

3.加载一次可以系统在任意地方调用事务

简单的使用方式:

1.配置数据库连接信息,可以配置n个,读写分开配置

    <DataGroup Id="readTemplates" ClassPath="com.gavin.plugin.database.DataEngine" Driver="com.mysql.jdbc.Driver">
        <Database URI="jdbc:mysql://127.0.0.1:3306/Templates?Unicode=true&amp;characterEncoding=utf8" LoadLevel="100" KeyIV="DECSECURITYKEYABCDEFG|EiJPWIgQQDgoJXlRy91SZncpdZgwQEHi">    
            <User UserName="root" Password="root"/>
            <ConnectionPool>
                <MinConnectionCount>1</MinConnectionCount>
                <MaxConnectionCount>5</MaxConnectionCount>
            </ConnectionPool>
        </Database>
    </DataGroup>
    
    <DataGroup Id="writeTemplates" ClassPath="com.gavin.plugin.database.DataEngine" Driver="com.mysql.jdbc.Driver">
        <Database URI="jdbc:mysql://127.0.0.1:3306/Templates?Unicode=true&amp;characterEncoding=utf8" LoadLevel="100" KeyIV="DECSECURITYKEYABCDEFG|EiJPWIgQQDgoJXlRy91SZncpdZgwQEHi">
            <User UserName="root" Password=""/>
            <ConnectionPool>
                <MinConnectionCount>1</MinConnectionCount>
                <MaxConnectionCount>5</MaxConnectionCount>
            </ConnectionPool>
        </Database>
    </DataGroup>    

2.配置分表分库规则,可以配置多个表

    <Sharding Resource="templates/sharding.xml" Encoding="utf-8"/>

<?xml version="1.0" encoding="UTF-8"?>
<TableRule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="DataService.xsd" Id="write">
<!-- 表名  数据库标识   单库表数量  (哈希或)容量        库数量 总表数量,不填默认10000表 -->
<BigTable Name="templatesRecord" GroupId="writeTemplates" GroupTableCount="10" GroupTableCapacity="1" GroupCount="5" HashedIdCount="50"/>

<BigTable Name="tradeDetail" GroupId="writeTemplates" GroupTableCount="100" GroupTableCapacity="1" GroupCount="100" />
</TableRule>

3.配置事务插件路径,可以配置多个,适用于分模块开发互不影响

    <Plugin Resource="user/plugin.xml" Encoding="utf-8"/>
    <Plugin Resource="pay/plugin.xml" Encoding="utf-8"/>

4.编写事务逻辑,如下:查询分页  传值(pageNumber,pageSize)

    <SQLTrans TransName="queryPage" TransFlag="0" DataGroupId="readTemplates">
        <SelectRecordSet OutputId="{list}">
            <OutputSQL>select * from {TI:tbTemplatesRecord, strUserName} limit {startIndex},{pageSize}</OutputSQL>
        </SelectRecordSet>
        
        <!--判断第一条数据的金额是否等于100,是则打印日志-->
        <If Value1="{list[0].lMoney}" Type="Long" Operator="=" Value2="100">
            <Then>
                <Log Text="金额等于100" Level="info"/>
            </Then>
        </If>
        <SelectField OutputId="{totalCount}">
            <OutputSQL>select count(*) from  {TI:tbTemplatesRecord, strUserName}</OutputSQL>
        </SelectField>
        <OnException>
            <Return Info="TemplatesService.xml.query.Error.{userId}:异常" Code="-1" Text="数据库异常"></Return>
        </OnException>        
        <Return Info="查询信息成功" Code="0" Text="查询信息成功" >
            <ReturnItem FieldId="{totalCount}" ValueId="{totalCount}"/>
            <ReturnItem FieldId="{list}" ValueId="{list}"/>
        </Return>
    </SQLTrans>

    注意:
        一个<SQLTrans>标签代表一个事务,事务必须在同一个SQLTrans标签中完成
        一个<SQLTrans>里面写多条sql语句进行数据库操作,也可以返回多个结果值

5.service层调用方式:

   Request query=Request.build("TemplatesService", "queryPage").page(0, 10).set("type", username).set("strUserName", username);
        Page<Datas> datas=DBTrans.page(query,Datas.class);
        if(datas==null){//查询失败
            
        }

从上面的步骤可以看出一个完整的事务,需要步骤4和步骤5

6.通过事务标签属性实现读写分离:

    写:<SQLTrans TransName="insert" TransFlag="1" DataGroupId="writeTemplates" BigTableGroupId="write">    
    读:<SQLTrans TransName="query" TransFlag="0" DataGroupId="readTemplates" BigTableGroupId="read">    

7.通过表达式实现分表分库,目前有两种方式

    ai:按指定字段数值顺序分表分库
    TI:按指定字段哈希值分表分库
    <OutputSQL>select * from {TI:tbTemplatesRecord, strUserName} limit {startIndex},{pageSize}</OutputSQL>

8.在项目任意地方可调用事务,通过DBTrans操作数据库的方式主要有4种,分别为:
 

     插件启动方式:
    DBTrans.getInstance().config("templates/config.properties").start();    

    8.1.查询一条数据并使用Respones对象返回
        DBTrans.beanResult(Request,Class<?>);
    8.2.查询分页信息(需要返回:totalCount和list),如果查询失败或异常则返回null
        DBTrans.page(Request,Class<?>);
    8.3.查询list集合(需要返回:list),如果查询失败或异常则返回null
        DBTrans.list(Request,Class<?>);
    8.4.查询一个对象,如果查询失败或异常则直接报错RuntimeException()
        DBTrans.bean(Request,Class<?>);
    8.5.执行数据库事务(这是一个万能方法,可以执行上面所有的操作并且可以有多个返回值)
        DBTrans.execute(Request);    

<<终>>

猜你喜欢

转载自blog.csdn.net/t_332741160/article/details/81408848