gdaoの使用例

ポンポンの最初に、導入

< 依存性> 
    < のgroupId > me.lovegao </ のgroupId > 
    < たartifactId > gdao </ たartifactId > 
    < バージョン> 0.0.2-SNAPSHOT </ バージョン> 
</ 依存>

最初にする必要がgdaoプロジェクトのパッケージには、ローカルリポジトリに追加されます。

第二に、対応するクラス定義データベースとテーブルを構築

1、UserDo

1  パッケージme.lovegao.gdao.dos。
2  
3  インポートme.lovegao.gdao.bean.annotation.GColumn。
4  インポートme.lovegao.gdao.bean.annotation.GId。
5  インポートme.lovegao.gdao.bean.annotation.GTable。
6  
7 @GTable( "t_user" 8  パブリック クラスUserDo {
 9      @GId(isAutoIncrease = 10      @GColumn(名= "ID" 11      プライベート ロングID。
12      
13      @GColumn(名= "名前" 14      民間文字列の名前。
15      
16      @GColumn(名前= "年齢" 17      プライベート int型の年齢;
18      
19      公衆 ロングのgetId(){
 20          リターンID。
21      }
 22      公共 ボイド SETID(長いID){
 23          この .ID = ID。
24      }
 25      パブリック文字列のgetName(){
 26          リターン名。
27      }
 28      公共 ボイドのsetName(文字列名){
 29          この .nameの=名;
30      }
 31      公共 int型getAge(){
 32          リターン年齢。
33      }
 34      公共 ボイド setAge(int型の年齢){
 35          この .age = 年齢。
36      }
 37      
38 }

図2に示すように、対応テーブルSQL

CREATE  TABLEを`t_user`(
  ` id` BIGINT20)符号なしNOT  NULL AUTO_INCREMENTのCOMMENT '主键' 
  `name`のVARCHAR255NOT  NULL  DEFAULT  '' COMMENT '名称' 
  ` age`のint型15NOT  NULL  DEFAULT  0 COMMENT '年龄' PRIMARY  KEY ( `id`)
)ENGINE = InnoDBのAUTO_INCREMENT =1  DEFAULT CHARSET = UTF8 COMMENT = 'テスト-ユーザテーブル'

3、MySQL設定

##ドライブ名
DRIVERNAME =はcom.mysql.jdbc.Driver 
##接続URL 
connectionURL = JDBC:MySQLの:// localhostの:? 3306 / useServerPrepStmtsシンプル=偽&rewriteBatchedStatements = trueに&のconnecttimeout = 1000 &useUnicode = trueに&characterEncoding = UTF-8 
##ユーザ名
のuserName = SS 
##ユーザパスワード
のuserPassword = 111111 
##の接続数を初期化
initConnectionNum = 10 
##接続の最大数
maxConnectionNum = 50 
##最大クエリレイテンシ
maxQueryTime = 3

第三に、UserDaoを作成

パッケージme.lovegao.gdao.dao。

輸入me.lovegao.gdao.dos.UserDo;
輸入me.lovegao.gdao.orm.BaseDao。
輸入me.lovegao.gdao.sqlexecute.ISqlExecutor。

パブリック クラス UserDaoは延び BaseDao <UserDo、ロング> {
     パブリックUserDao(ISqlExecutor sqlExecutor){
         スーパー(sqlExecutor)を、
    } 
    
}

第四に、実装

パブリック 静的 ボイドメイン(文字列[]引数)がスロー{例外
    ISqlExecutor sqlExecutor = initDao()。
    addTest(sqlExecutor)。
} 
パブリック 静的 ボイド addTest(ISqlExecutor sqlExecutor)がスロー例外{ 
    UserDao userDao = 新しいUserDao(sq​​lExecutor)を、
    UserDo [] UDS = LISTUSER()。
    System.out.println( "准备开始添加" ); 
    userDao.add(UDS [ 0 ])。
    System.out.println( "开始添加---" );
    長い T1 =System.currentTimeMillis();
    以下のためにINTは i = 1; iはuds.length <; iは++ ){ 
        userDao.add(UDS [I])。
    } 
    長い T2 = にSystem.currentTimeMillis();
    二重 useTime = T2 - T1。
    // 5000条、totalUseTime:8629.0ms、avgUseTime:1.7261452290458092ms 
    するSystem.out.println( "totalUseTime:" + useTime + "MS、avgUseTime:" + useTime /(uds.length-1)+ "MS" )。
} 
パブリック 静的 ISqlExecutor initDao()スロー例外{ 
    プロパティ小道具 = 新しい)(プロパティ。
    文字列のConfPath= "/mysql.properties" 
    prop.load(DaoTest。クラス.getResourceAsStream(のConfPath)); 
    DaoResourceManager daoResource = 新しいDaoResourceManager(プロパ)。
    ISqlExecutor sqlExecutor = daoResource.getSqlExecutor()。
    返すsqlExecutorを。
}

 

おすすめ

転載: www.cnblogs.com/shuimutong/p/11391491.html
おすすめ