一、引入pom

<dependency>
<groupId>me.lovegao</groupId>
<artifactId>gdao</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>

需要先将gdao项目(https://github.com/shuimutong/gdao.git)打包加到本地仓库。

0.0.2版本:tag:dev-1-init

0.0.3版本:tag:dev-2-tag-fix

二、连接配置

1、0.0.2版本

##驱动名称
driverName=com.mysql.jdbc.Driver
##连接url
connectionUrl=jdbc:mysql://localhost:3306/simple?useServerPrepStmts=false&rewriteBatchedStatements=true&connectTimeout=1000&useUnicode=true&characterEncoding=utf-8
##用户名
userName=ss
##用户密码
userPassword=111111
##初始化连接数
initConnectionNum=10
##最大连接数
maxConnectionNum=50
##最大查询等待时间
maxQueryTime=3

2、0.0.3配置

##驱动名称
driverName=com.mysql.jdbc.Driver
##连接url
connectionUrl=jdbc:mysql://localhost:3306/simple?useServerPrepStmts=false&rewriteBatchedStatements=true&connectTimeout=1000&useUnicode=true&characterEncoding=utf-8
##用户名
userName=simple
##用户密码
userPassword=123456
##初始化连接数
initConnectionNum=10
##最大连接数
maxConnectionNum=50
##最大查询等待时间
maxQueryTime=3
#########v03##########
#归还连接时检测连接,true false
checkConnectionWhenReturn=true
#连接检测语句(检测必填)
checkConnectionValidationQuery=select 1
#定时检测连接间隔时长(分钟)
periodCheckConnectionTimeMin=10
#连接泄露检测
connectionLeakCheck=true
#连接泄露检测间隔时长(分钟),需大于0
connectionLeakCheckPeriodTimeMin=10
#强制归还连接时长(小时),0代表不检测
forceReturnConnectionTimeHour=6

3、0.0.3版本更新简介

0.0.3版本相当于上一个版本,主要是增加了连接检测。

连接检测包括3部分:归还连接时检测,连接定时检测,连接泄露检测。

1)检测必须配置的字段:checkConnectionValidationQuery=select 1

2)归还连接时检测

在归还连接时检测连接是否可用。

配置字段:checkConnectionWhenReturn=true

3)连接定时检测

定时检测连接池中的连接是否可用。如果不可用,就进行关闭处理,防止连接池中存放的连接是无用连接。

配置字段:periodCheckConnectionTimeMin=10,如果值为0,则不进行此项检测

4)连接泄露检测

防止连接被借出之后不归还,可能导致连接池数量很多,但是无可用连接。

配置字段:

forceReturnConnectionTimeHour=6,如果连接借出时长超过这个值,就会关闭连接,然后从连接池之后移除,如果为0,则代表不检测;

connectionLeakCheckPeriodTimeMin=10,检测是由线程进行检测的,这个字段表示线程每个多久检测一次

三、定义数据库对应的类和建表

1、UserDo

 package me.lovegao.gdao.dos;

 import me.lovegao.gdao.bean.annotation.GColumn;
import me.lovegao.gdao.bean.annotation.GId;
import me.lovegao.gdao.bean.annotation.GTable; @GTable("t_user")
public class UserDo {
@GId(isAutoIncrease=true)
@GColumn(name="id")
private long id; @GColumn(name="name")
private String name; @GColumn(name="age")
private int age; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }

2、对应表sql

CREATE TABLE `t_user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(255) NOT NULL DEFAULT '' COMMENT '名称',
`age` int(15) NOT NULL DEFAULT 0 COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='测试-用户表';

三、创建UserDao

package me.lovegao.gdao.dao;

import me.lovegao.gdao.dos.UserDo;
import me.lovegao.gdao.orm.BaseDao;
import me.lovegao.gdao.sqlexecute.ISqlExecutor; public class UserDao extends BaseDao<UserDo, Long> {
public UserDao(ISqlExecutor sqlExecutor) {
super(sqlExecutor);
} }

四、执行

public static void main(String[] args) throws Exception {
ISqlExecutor sqlExecutor = initDao();
addTest(sqlExecutor);
}
public static void addTest(ISqlExecutor sqlExecutor) throws Exception {
//实例化UserDao
UserDao userDao = new UserDao(sqlExecutor);
UserDo[] uds = listUser(); //获取测试数据
System.out.println("准备开始添加");
userDao.add(uds[0]); //添加数据
System.out.println("开始添加---");
long t1 = System.currentTimeMillis();
for(int i=1; i<uds.length; i++) {
userDao.add(uds[i]);
}
long t2 = System.currentTimeMillis();
double useTime = t2 - t1;
//5000条,totalUseTime:8629.0ms,avgUseTime:1.7261452290458092ms
System.out.println("totalUseTime:" + useTime + "ms,avgUseTime:" + useTime/(uds.length-1) + "ms");
}
//初始化资源管理,sql执行
public static ISqlExecutor initDao() throws Exception {
Properties prop = new Properties();
String confPath = "/mysql.properties";
prop.load(DaoTest.class.getResourceAsStream(confPath));
//0.0.2版本
DaoResourceManager daoResource = new DaoResourceManager(prop);
//0.0.3版本
DaoResourceManager daoResource = new DaoResourceManager(prop, true);
//获取sql执行器
ISqlExecutor sqlExecutor = daoResource.getSqlExecutor();
return sqlExecutor;
}

最新文章

  1. ASP.NET Core HTTP 管道中的那些事儿
  2. 网页Loading效果
  3. jQuery利用JSON数据动态生成表格
  4. 纯css3绘制扇形
  5. Ext.net 异常统一管理,铥掉可恶的 Request Failure
  6. iOS学习笔记---oc语言第八天
  7. 插入到Mysql数据库中的汉字乱码
  8. jquery提示气泡
  9. VIM IDE
  10. position relative
  11. 浅谈如何保证discuz插件安全
  12. C#调用Python,报错No module named os
  13. RHEL Linux常用指令
  14. PHP中利用pcntl实现多进程(模拟多线程)实例(转)
  15. 【P2015】二叉苹果树 (树形DP分组背包)
  16. Crazy Circuits HDU - 3157(有源汇有上下界最小流)
  17. Bitcoin 使用及配置记录
  18. CBV流程之View源码解析
  19. nexus7 升级失败后手动刷系统
  20. day 113 爬虫框架

热门文章

  1. hdu1702 ACboy needs your help again![简单STL 栈 队列]
  2. 定制比特币btc地址address
  3. 解决vs2010按ctrl+f5,调试窗口一闪而过的方法
  4. ThinkPHP5 基础知识入门 [入门必先了解]
  5. elementUI-tree组件 懒加载
  6. H3C Telnet 配置
  7. php 类的属性--???
  8. UIView的intrinsicContentSize方法,在按钮中重写
  9. [LeetCode] 534. Design TinyURL 设计短网址
  10. golang 基于channel封装资源池(可用于封装redis、mq连接池)