mirror of
https://gitee.com/dromara/hutool.git
synced 2025-12-04 19:08:04 +08:00
clean history
This commit is contained in:
187
hutool-db/src/test/java/cn/hutool/db/CRUDTest.java
Normal file
187
hutool-db/src/test/java/cn/hutool/db/CRUDTest.java
Normal file
@@ -0,0 +1,187 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.db.ActiveEntity;
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.handler.EntityListHandler;
|
||||
import cn.hutool.db.pojo.User;
|
||||
import cn.hutool.db.sql.Condition;
|
||||
import cn.hutool.db.sql.Condition.LikeType;
|
||||
|
||||
/**
|
||||
* 增删改查测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class CRUDTest {
|
||||
|
||||
private static Db db = Db.use("test");
|
||||
|
||||
@Test
|
||||
public void findIsNullTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", "is null"));
|
||||
Assert.assertEquals(0, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findIsNullTest2() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", "= null"));
|
||||
Assert.assertEquals(0, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findIsNullTest3() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", null));
|
||||
Assert.assertEquals(0, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findBetweenTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", "between '18' and '40'"));
|
||||
Assert.assertEquals(1, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByBigIntegerTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", new BigInteger("12")));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByBigDecimalTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("age", new BigDecimal("12")));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findLikeTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("name", "like \"%三%\""));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findLikeTest2() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("name", new Condition("name", "三", LikeType.Contains)));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findLikeTest3() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("name", new Condition("name", null, LikeType.Contains)));
|
||||
Assert.assertEquals(0, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findInTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("id", "in 1,2,3"));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findInTest2() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("id", new Condition("id", new long[] {1,2,3})));
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllTest() throws SQLException {
|
||||
List<Entity> results = db.findAll("user");
|
||||
Assert.assertEquals(4, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTest() throws SQLException {
|
||||
List<Entity> find = db.find(CollUtil.newArrayList("name AS name2"), Entity.create("user"), new EntityListHandler());
|
||||
Assert.assertFalse(find.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findActiveTest() throws SQLException {
|
||||
ActiveEntity entity = new ActiveEntity(db, "user");
|
||||
entity.setFieldNames("name AS name2").load();
|
||||
Assert.assertEquals("user", entity.getTableName());
|
||||
Assert.assertFalse(entity.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* 对增删改查做单元测试
|
||||
*
|
||||
* @throws SQLException
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void crudTest() throws SQLException {
|
||||
|
||||
// 增
|
||||
Long id = db.insertForGeneratedKey(Entity.create("user").set("name", "unitTestUser").set("age", 66));
|
||||
Assert.assertTrue(id > 0);
|
||||
Entity result = db.get("user", "name", "unitTestUser");
|
||||
Assert.assertSame(66, (int) result.getInt("age"));
|
||||
|
||||
// 改
|
||||
int update = db.update(Entity.create().set("age", 88), Entity.create("user").set("name", "unitTestUser"));
|
||||
Assert.assertTrue(update > 0);
|
||||
Entity result2 = db.get("user", "name", "unitTestUser");
|
||||
Assert.assertSame(88, (int) result2.getInt("age"));
|
||||
|
||||
// 删
|
||||
int del = db.del("user", "name", "unitTestUser");
|
||||
Assert.assertTrue(del > 0);
|
||||
Entity result3 = db.get("user", "name", "unitTestUser");
|
||||
Assert.assertNull(result3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void insertBatchTest() throws SQLException {
|
||||
User user1 = new User();
|
||||
user1.setName("张三");
|
||||
user1.setAge(12);
|
||||
user1.setBirthday("19900112");
|
||||
user1.setGender(true);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName("李四");
|
||||
user2.setAge(12);
|
||||
user2.setBirthday("19890512");
|
||||
user2.setGender(false);
|
||||
|
||||
Entity data1 = Entity.parse(user1);
|
||||
Entity data2 = Entity.parse(user2);
|
||||
|
||||
Console.log(data1);
|
||||
Console.log(data2);
|
||||
|
||||
int[] result = db.insert(CollUtil.newArrayList(data1, data2));
|
||||
Console.log(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void insertBatchOneTest() throws SQLException {
|
||||
User user1 = new User();
|
||||
user1.setName("张三");
|
||||
user1.setAge(12);
|
||||
user1.setBirthday("19900112");
|
||||
user1.setGender(true);
|
||||
|
||||
Entity data1 = Entity.parse(user1);
|
||||
|
||||
Console.log(data1);
|
||||
|
||||
int[] result = db.insert(CollUtil.newArrayList(data1));
|
||||
Console.log(result);
|
||||
}
|
||||
}
|
||||
54
hutool-db/src/test/java/cn/hutool/db/ConcurentTest.java
Normal file
54
hutool-db/src/test/java/cn/hutool/db/ConcurentTest.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.handler.EntityListHandler;
|
||||
|
||||
/**
|
||||
* SqlRunner线程安全测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
@Ignore
|
||||
public class ConcurentTest {
|
||||
|
||||
private Db db;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
db = Db.use("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findTest() {
|
||||
for(int i = 0; i < 10000; i++) {
|
||||
ThreadUtil.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<Entity> find = null;
|
||||
try {
|
||||
find = db.find(CollectionUtil.newArrayList("name AS name2"), Entity.create("user"), new EntityListHandler());
|
||||
} catch (SQLException e) {
|
||||
throw new DbRuntimeException(e);
|
||||
}
|
||||
Console.log(find);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//主线程关闭会导致连接池销毁,sleep避免此情况引起的问题
|
||||
ThreadUtil.sleep(5000);
|
||||
}
|
||||
}
|
||||
56
hutool-db/src/test/java/cn/hutool/db/DbTest.java
Normal file
56
hutool-db/src/test/java/cn/hutool/db/DbTest.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.func.VoidFunc1;
|
||||
import cn.hutool.db.sql.Condition;
|
||||
import cn.hutool.log.StaticLog;
|
||||
|
||||
/**
|
||||
* Db对象单元测试
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class DbTest {
|
||||
|
||||
@Test
|
||||
public void findTest() throws SQLException {
|
||||
Db.use();
|
||||
|
||||
List<Entity> find = Db.use().find(Entity.create("user").set("age", 18));
|
||||
Assert.assertEquals("王五", find.get(0).get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByTest() throws SQLException {
|
||||
Db.use();
|
||||
|
||||
List<Entity> find = Db.use().findBy("user",
|
||||
Condition.parse("age", "> 18"),
|
||||
Condition.parse("age", "< 100")
|
||||
);
|
||||
for (Entity entity : find) {
|
||||
StaticLog.debug("{}", entity);
|
||||
}
|
||||
Assert.assertEquals("unitTestUser", find.get(0).get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void txTest() throws SQLException {
|
||||
Db.use().tx(new VoidFunc1<Db>() {
|
||||
|
||||
@Override
|
||||
public void call(Db db) throws SQLException {
|
||||
db.insert(Entity.create("user").set("name", "unitTestUser2"));
|
||||
db.update(Entity.create().set("age", 79), Entity.create("user").set("name", "unitTestUser2"));
|
||||
db.del("user", "name", "unitTestUser2");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
92
hutool-db/src/test/java/cn/hutool/db/DsTest.java
Normal file
92
hutool-db/src/test/java/cn/hutool/db/DsTest.java
Normal file
@@ -0,0 +1,92 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.ds.DSFactory;
|
||||
import cn.hutool.db.ds.c3p0.C3p0DSFactory;
|
||||
import cn.hutool.db.ds.dbcp.DbcpDSFactory;
|
||||
import cn.hutool.db.ds.druid.DruidDSFactory;
|
||||
import cn.hutool.db.ds.hikari.HikariDSFactory;
|
||||
import cn.hutool.db.ds.pooled.PooledDSFactory;
|
||||
import cn.hutool.db.ds.tomcat.TomcatDSFactory;
|
||||
|
||||
/**
|
||||
* 数据源单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class DsTest {
|
||||
|
||||
@Test
|
||||
public void defaultDsTest() throws SQLException {
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hikariDsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new HikariDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void druidDsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new DruidDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tomcatDsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new TomcatDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dbcpDsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new DbcpDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void c3p0DsTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new C3p0DSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hutoolPoolTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new PooledDSFactory());
|
||||
DataSource ds = DSFactory.get("test");
|
||||
Db db = Db.use(ds);
|
||||
List<Entity> all = db.findAll("user");
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
}
|
||||
48
hutool-db/src/test/java/cn/hutool/db/EntityTest.java
Normal file
48
hutool-db/src/test/java/cn/hutool/db/EntityTest.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.pojo.User;
|
||||
|
||||
/**
|
||||
* Entity测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class EntityTest {
|
||||
|
||||
@Test
|
||||
public void parseTest() {
|
||||
User user = new User();
|
||||
user.setId(1);
|
||||
user.setName("test");
|
||||
|
||||
Entity entity = Entity.create("testTable").parseBean(user);
|
||||
Assert.assertEquals(Integer.valueOf(1), entity.getInt("id"));
|
||||
Assert.assertEquals("test", entity.getStr("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest2() {
|
||||
User user = new User();
|
||||
user.setId(1);
|
||||
user.setName("test");
|
||||
|
||||
Entity entity = Entity.create().parseBean(user);
|
||||
Assert.assertEquals(Integer.valueOf(1), entity.getInt("id"));
|
||||
Assert.assertEquals("test", entity.getStr("name"));
|
||||
Assert.assertEquals("user", entity.getTableName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void entityToBeanIgnoreCaseTest() {
|
||||
Entity entity = Entity.create().set("ID", 2).set("NAME", "testName");
|
||||
User user = entity.toBeanIgnoreCase(User.class);
|
||||
|
||||
Assert.assertEquals(Integer.valueOf(2), user.getId());
|
||||
Assert.assertEquals("testName", user.getName());
|
||||
}
|
||||
}
|
||||
71
hutool-db/src/test/java/cn/hutool/db/FindBeanTest.java
Normal file
71
hutool-db/src/test/java/cn/hutool/db/FindBeanTest.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.pojo.User;
|
||||
|
||||
/**
|
||||
* Entity测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class FindBeanTest {
|
||||
|
||||
Db db;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
db = Db.use("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllBeanTest() throws SQLException {
|
||||
List<User> results = db.findAll(Entity.create("user"), User.class);
|
||||
|
||||
Assert.assertEquals(4, results.size());
|
||||
Assert.assertEquals(Integer.valueOf(1), results.get(0).getId());
|
||||
Assert.assertEquals("张三", results.get(0).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void findAllListTest() throws SQLException {
|
||||
List<List> results = db.findAll(Entity.create("user"), List.class);
|
||||
|
||||
Assert.assertEquals(4, results.size());
|
||||
Assert.assertEquals(1, results.get(0).get(0));
|
||||
Assert.assertEquals("张三", results.get(0).get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllArrayTest() throws SQLException {
|
||||
List<Object[]> results = db.findAll(Entity.create("user"), Object[].class);
|
||||
|
||||
Assert.assertEquals(4, results.size());
|
||||
Assert.assertEquals(1, results.get(0)[0]);
|
||||
Assert.assertEquals("张三", results.get(0)[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllStringTest() throws SQLException {
|
||||
List<String> results = db.findAll(Entity.create("user"), String.class);
|
||||
Assert.assertEquals(4, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllStringArrayTest() throws SQLException {
|
||||
List<String[]> results = db.findAll(Entity.create("user"), String[].class);
|
||||
|
||||
Assert.assertEquals(4, results.size());
|
||||
Assert.assertEquals("1", results.get(0)[0]);
|
||||
Assert.assertEquals("张三", results.get(0)[1]);
|
||||
}
|
||||
}
|
||||
38
hutool-db/src/test/java/cn/hutool/db/HsqldbTest.java
Normal file
38
hutool-db/src/test/java/cn/hutool/db/HsqldbTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.Entity;
|
||||
|
||||
/**
|
||||
* HSQLDB数据库单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class HsqldbTest {
|
||||
|
||||
private static final String DS_GROUP_NAME = "hsqldb";
|
||||
|
||||
@Before
|
||||
public void init() throws SQLException {
|
||||
Db db = Db.use(DS_GROUP_NAME);
|
||||
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");
|
||||
db.insert(Entity.create("test").set("a", 1).set("b", 11));
|
||||
db.insert(Entity.create("test").set("a", 2).set("b", 21));
|
||||
db.insert(Entity.create("test").set("a", 3).set("b", 31));
|
||||
db.insert(Entity.create("test").set("a", 4).set("b", 41));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connTest() throws SQLException {
|
||||
List<Entity> query = Db.use(DS_GROUP_NAME).query("select * from test");
|
||||
Assert.assertEquals(4, query.size());
|
||||
}
|
||||
}
|
||||
65
hutool-db/src/test/java/cn/hutool/db/MySQLTest.java
Normal file
65
hutool-db/src/test/java/cn/hutool/db/MySQLTest.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.func.VoidFunc1;
|
||||
|
||||
/**
|
||||
* MySQL操作单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class MySQLTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void insertTest() throws SQLException {
|
||||
for (int id = 100; id < 200; id++) {
|
||||
Db.use("mysql").insert(Entity.create("user")//
|
||||
.set("id", id)//
|
||||
.set("name", "测试用户" + id)//
|
||||
.set("text", "描述" + id)//
|
||||
.set("test1", "t" + id)//
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 事务测试<br>
|
||||
* 更新三条信息,低2条后抛出异常,正常情况下三条都应该不变
|
||||
*
|
||||
* @throws SQLException
|
||||
*/
|
||||
@Test(expected=SQLException.class)
|
||||
@Ignore
|
||||
public void txTest() throws SQLException {
|
||||
Db.use("mysql").tx(new VoidFunc1<Db>() {
|
||||
|
||||
@Override
|
||||
public void call(Db db) throws Exception {
|
||||
int update = db.update(Entity.create("user").set("text", "描述100"), Entity.create().set("id", 100));
|
||||
db.update(Entity.create("user").set("text", "描述101"), Entity.create().set("id", 101));
|
||||
if(1 == update) {
|
||||
// 手动指定异常,然后测试回滚触发
|
||||
throw new RuntimeException("Error");
|
||||
}
|
||||
db.update(Entity.create("user").set("text", "描述102"), Entity.create().set("id", 102));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void pageTest() throws SQLException {
|
||||
PageResult<Entity> result = Db.use("mysql").page(Entity.create("user"), new Page(2, 10));
|
||||
for (Entity entity : result) {
|
||||
Console.log(entity.get("id"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
39
hutool-db/src/test/java/cn/hutool/db/NamedSqlTest.java
Normal file
39
hutool-db/src/test/java/cn/hutool/db/NamedSqlTest.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.db.sql.NamedSql;
|
||||
|
||||
public class NamedSqlTest {
|
||||
|
||||
@Test
|
||||
public void parseTest() {
|
||||
String sql = "select * from table where id=@id and name = @name1 and nickName = :subName";
|
||||
|
||||
Map<String, Object> paramMap = MapUtil.builder("name1", (Object)"张三").put("age", 12).put("subName", "小豆豆").build();
|
||||
|
||||
NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
//未指定参数原样输出
|
||||
Assert.assertEquals("select * from table where id=@id and name = ? and nickName = ?", namedSql.getSql());
|
||||
Assert.assertEquals("张三", namedSql.getParams()[0]);
|
||||
Assert.assertEquals("小豆豆", namedSql.getParams()[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest2() {
|
||||
String sql = "select * from table where id=@id and name = @name1 and nickName = :subName";
|
||||
|
||||
Map<String, Object> paramMap = MapUtil.builder("name1", (Object)"张三").put("age", 12).put("subName", "小豆豆").put("id", null).build();
|
||||
|
||||
NamedSql namedSql = new NamedSql(sql, paramMap);
|
||||
Assert.assertEquals("select * from table where id=? and name = ? and nickName = ?", namedSql.getSql());
|
||||
//指定了null参数的依旧替换,参数值为null
|
||||
Assert.assertNull(namedSql.getParams()[0]);
|
||||
Assert.assertEquals("张三", namedSql.getParams()[1]);
|
||||
Assert.assertEquals("小豆豆", namedSql.getParams()[2]);
|
||||
}
|
||||
}
|
||||
69
hutool-db/src/test/java/cn/hutool/db/OracleTest.java
Normal file
69
hutool-db/src/test/java/cn/hutool/db/OracleTest.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.Page;
|
||||
import cn.hutool.db.PageResult;
|
||||
import cn.hutool.db.sql.Query;
|
||||
import cn.hutool.db.sql.SqlBuilder;
|
||||
import cn.hutool.db.sql.SqlUtil;
|
||||
|
||||
/**
|
||||
* Oracle操作单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class OracleTest {
|
||||
|
||||
@Test
|
||||
public void oraclePageSqlTest() {
|
||||
Page page = new Page(1, 10);
|
||||
Entity where = Entity.create("PMCPERFORMANCEINFO").set("yearPI", "2017");
|
||||
final Query query = new Query(SqlUtil.buildConditions(where), where.getTableName());
|
||||
query.setPage(page);
|
||||
|
||||
SqlBuilder find = SqlBuilder.create(null).query(query).orderBy(page.getOrders());
|
||||
final int[] startEnd = page.getStartEnd();
|
||||
SqlBuilder builder = SqlBuilder.create(null).append("SELECT * FROM ( SELECT row_.*, rownum rownum_ from ( ")//
|
||||
.append(find)//
|
||||
.append(" ) row_ where rownum <= ").append(startEnd[1])//
|
||||
.append(") table_alias")//
|
||||
.append(" where table_alias.rownum_ >= ").append(startEnd[0]);//
|
||||
|
||||
String ok = "SELECT * FROM "//
|
||||
+ "( SELECT row_.*, rownum rownum_ from ( SELECT * FROM PMCPERFORMANCEINFO WHERE yearPI = ? ) row_ "//
|
||||
+ "where rownum <= 10) table_alias where table_alias.rownum_ >= 0";//
|
||||
Assert.assertEquals(ok, builder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void insertTest() throws SQLException {
|
||||
for (int id = 100; id < 200; id++) {
|
||||
Db.use("orcl").insert(Entity.create("T_USER")//
|
||||
.set("ID", id)//
|
||||
.set("name", "测试用户" + id)//
|
||||
.set("TEXT", "描述" + id)//
|
||||
.set("TEST1", "t" + id)//
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void pageTest() throws SQLException {
|
||||
PageResult<Entity> result = Db.use("orcl").page(Entity.create("T_USER"), new Page(2, 10));
|
||||
for (Entity entity : result) {
|
||||
Console.log(entity.get("ID"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
41
hutool-db/src/test/java/cn/hutool/db/PostgreTest.java
Normal file
41
hutool-db/src/test/java/cn/hutool/db/PostgreTest.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.Page;
|
||||
import cn.hutool.db.PageResult;
|
||||
|
||||
/**
|
||||
* PostgreSQL 单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class PostgreTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void insertTest() throws SQLException {
|
||||
for (int id = 100; id < 200; id++) {
|
||||
Db.use("postgre").insert(Entity.create("user")//
|
||||
.set("id", id)//
|
||||
.set("name", "测试用户" + id)//
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void pageTest() throws SQLException {
|
||||
PageResult<Entity> result = Db.use("postgre").page(Entity.create("user"), new Page(2, 10));
|
||||
for (Entity entity : result) {
|
||||
Console.log(entity.get("id"));
|
||||
}
|
||||
}
|
||||
}
|
||||
40
hutool-db/src/test/java/cn/hutool/db/SessionTest.java
Normal file
40
hutool-db/src/test/java/cn/hutool/db/SessionTest.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.func.VoidFunc1;
|
||||
|
||||
/**
|
||||
* 事务性数据库操作单元测试
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class SessionTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void transTest() {
|
||||
Session session = Session.create("test");
|
||||
try {
|
||||
session.beginTransaction();
|
||||
session.update(Entity.create().set("age", 76), Entity.create("user").set("name", "unitTestUser"));
|
||||
session.commit();
|
||||
} catch (SQLException e) {
|
||||
session.quietRollback();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void txTest() throws SQLException {
|
||||
Session.create("test").tx(new VoidFunc1<Session>() {
|
||||
@Override
|
||||
public void call(Session session) throws SQLException {
|
||||
session.update(Entity.create().set("age", 78), Entity.create("user").set("name", "unitTestUser"));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
48
hutool-db/src/test/java/cn/hutool/db/SqlServerTest.java
Normal file
48
hutool-db/src/test/java/cn/hutool/db/SqlServerTest.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.Page;
|
||||
import cn.hutool.db.PageResult;
|
||||
|
||||
/**
|
||||
* SQL Server操作单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class SqlServerTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void createTableTest() throws SQLException {
|
||||
Db.use("sqlserver").execute("create table T_USER(ID bigint, name varchar(255))");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void insertTest() throws SQLException {
|
||||
for (int id = 100; id < 200; id++) {
|
||||
Db.use("sqlserver").insert(Entity.create("T_USER")//
|
||||
.set("ID", id)//
|
||||
.set("name", "测试用户" + id)//
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void pageTest() throws SQLException {
|
||||
PageResult<Entity> result = Db.use("sqlserver").page(Entity.create("T_USER"), new Page(2, 10));
|
||||
for (Entity entity : result) {
|
||||
Console.log(entity.get("ID"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
37
hutool-db/src/test/java/cn/hutool/db/UpdateTest.java
Normal file
37
hutool-db/src/test/java/cn/hutool/db/UpdateTest.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.Entity;
|
||||
|
||||
public class UpdateTest {
|
||||
|
||||
Db db;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
db = Db.use("test");
|
||||
}
|
||||
|
||||
/**
|
||||
* 对更新做单元测试
|
||||
*
|
||||
* @throws SQLException
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void updateTest() throws SQLException {
|
||||
|
||||
// 改
|
||||
int update = db.update(Entity.create("user").set("age", 88), Entity.create().set("name", "unitTestUser"));
|
||||
Assert.assertTrue(update > 0);
|
||||
Entity result2 = db.get("user", "name", "unitTestUser");
|
||||
Assert.assertSame(88, (int) result2.getInt("age"));
|
||||
}
|
||||
}
|
||||
40
hutool-db/src/test/java/cn/hutool/db/meta/MetaUtilTest.java
Normal file
40
hutool-db/src/test/java/cn/hutool/db/meta/MetaUtilTest.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package cn.hutool.db.meta;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.ds.DSFactory;
|
||||
|
||||
/**
|
||||
* 元数据信息单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class MetaUtilTest {
|
||||
DataSource ds = DSFactory.get("test");
|
||||
|
||||
@Test
|
||||
public void getTablesTest() {
|
||||
List<String> tables = MetaUtil.getTables(ds);
|
||||
Assert.assertEquals("user", tables.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTableMetaTest() {
|
||||
Table table = MetaUtil.getTableMeta(ds, "user");
|
||||
Assert.assertEquals(CollectionUtil.newHashSet("id"), table.getPkNames());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getColumnNamesTest() {
|
||||
String[] names = MetaUtil.getColumnNames(ds, "user");
|
||||
Assert.assertArrayEquals(StrUtil.splitToArray("id,name,age,birthday,gender", ','), names);
|
||||
}
|
||||
}
|
||||
60
hutool-db/src/test/java/cn/hutool/db/pojo/User.java
Normal file
60
hutool-db/src/test/java/cn/hutool/db/pojo/User.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package cn.hutool.db.pojo;
|
||||
|
||||
/**
|
||||
* 测试用POJO,与测试数据库中的user表对应
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class User {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private int age;
|
||||
private String birthday;
|
||||
private boolean gender;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer 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;
|
||||
}
|
||||
|
||||
public String getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(String birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public boolean isGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(boolean gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [id=" + id + ", name=" + name + ", age=" + age + ", birthday=" + birthday + ", gender=" + gender + "]";
|
||||
}
|
||||
}
|
||||
55
hutool-db/src/test/java/cn/hutool/db/sql/ConditionTest.java
Normal file
55
hutool-db/src/test/java/cn/hutool/db/sql/ConditionTest.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package cn.hutool.db.sql;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConditionTest {
|
||||
|
||||
@Test
|
||||
public void toStringTest() {
|
||||
Condition conditionNull = new Condition("user", null);
|
||||
Assert.assertEquals("user IS NULL", conditionNull.toString());
|
||||
|
||||
Condition conditionNotNull = new Condition("user", "!= null");
|
||||
Assert.assertEquals("user IS NOT NULL", conditionNotNull.toString());
|
||||
|
||||
Condition condition2 = new Condition("user", "= zhangsan");
|
||||
Assert.assertEquals("user = ?", condition2.toString());
|
||||
|
||||
Condition conditionLike = new Condition("user", "like %aaa");
|
||||
Assert.assertEquals("user LIKE ?", conditionLike.toString());
|
||||
|
||||
Condition conditionIn = new Condition("user", "in 1,2,3");
|
||||
Assert.assertEquals("user IN (?,?,?)", conditionIn.toString());
|
||||
|
||||
Condition conditionBetween = new Condition("user", "between 12 and 13");
|
||||
Assert.assertEquals("user BETWEEN ? AND ?", conditionBetween.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringNoPlaceHolderTest() {
|
||||
Condition conditionNull = new Condition("user", null);
|
||||
conditionNull.setPlaceHolder(false);
|
||||
Assert.assertEquals("user IS NULL", conditionNull.toString());
|
||||
|
||||
Condition conditionNotNull = new Condition("user", "!= null");
|
||||
conditionNotNull.setPlaceHolder(false);
|
||||
Assert.assertEquals("user IS NOT NULL", conditionNotNull.toString());
|
||||
|
||||
Condition conditionEquals = new Condition("user", "= zhangsan");
|
||||
conditionEquals.setPlaceHolder(false);
|
||||
Assert.assertEquals("user = zhangsan", conditionEquals.toString());
|
||||
|
||||
Condition conditionLike = new Condition("user", "like %aaa");
|
||||
conditionLike.setPlaceHolder(false);
|
||||
Assert.assertEquals("user LIKE %aaa", conditionLike.toString());
|
||||
|
||||
Condition conditionIn = new Condition("user", "in 1,2,3");
|
||||
conditionIn.setPlaceHolder(false);
|
||||
Assert.assertEquals("user IN (1,2,3)", conditionIn.toString());
|
||||
|
||||
Condition conditionBetween = new Condition("user", "between 12 and 13");
|
||||
conditionBetween.setPlaceHolder(false);
|
||||
Assert.assertEquals("user BETWEEN 12 AND 13", conditionBetween.toString());
|
||||
}
|
||||
}
|
||||
22
hutool-db/src/test/java/cn/hutool/db/sql/SqlBuilderTest.java
Normal file
22
hutool-db/src/test/java/cn/hutool/db/sql/SqlBuilderTest.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package cn.hutool.db.sql;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SqlBuilderTest {
|
||||
|
||||
@Test
|
||||
public void queryNullTest() {
|
||||
SqlBuilder builder = SqlBuilder.create().select().from("user").where(new Condition("name", "= null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NULL", builder.build());
|
||||
|
||||
SqlBuilder builder2 = SqlBuilder.create().select().from("user").where(new Condition("name", "is null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NULL", builder2.build());
|
||||
|
||||
SqlBuilder builder3 = SqlBuilder.create().select().from("user").where(LogicalOperator.AND, new Condition("name", "!= null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NOT NULL", builder3.build());
|
||||
|
||||
SqlBuilder builder4 = SqlBuilder.create().select().from("user").where(LogicalOperator.AND, new Condition("name", "is not null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NOT NULL", builder4.build());
|
||||
}
|
||||
}
|
||||
51
hutool-db/src/test/resources/config/db.setting
Normal file
51
hutool-db/src/test/resources/config/db.setting
Normal file
@@ -0,0 +1,51 @@
|
||||
#===================================================================
|
||||
# 数据库配置文件样例
|
||||
# DsFactory默认读取的配置文件是config/db.setting
|
||||
# db.setting的配置包括两部分:基本连接信息和连接池配置信息。
|
||||
# 基本连接信息所有连接池都支持,连接池配置信息根据不同的连接池,连接池配置是根据连接池相应的配置项移植而来
|
||||
#===================================================================
|
||||
|
||||
## 打印SQL的配置
|
||||
# 是否在日志中显示执行的SQL,默认false
|
||||
showSql = true
|
||||
# 是否格式化显示的SQL,默认false
|
||||
formatSql = true
|
||||
# 是否显示SQL参数,默认false
|
||||
showParams = true
|
||||
# 打印SQL的日志等级,默认debug
|
||||
sqlLevel = debug
|
||||
|
||||
# 默认数据源
|
||||
url = jdbc:sqlite:test.db
|
||||
|
||||
|
||||
# 测试数据源
|
||||
[test]
|
||||
url = jdbc:sqlite:test.db
|
||||
|
||||
# 测试用HSQLDB数据库
|
||||
[hsqldb]
|
||||
url = jdbc:hsqldb:mem:mem_hutool
|
||||
user = SA
|
||||
pass =
|
||||
|
||||
# 测试用Oracle数据库
|
||||
[orcl]
|
||||
url = jdbc:oracle:thin:@//looly.centos:1521/XE
|
||||
user = looly
|
||||
pass = 123456
|
||||
|
||||
[mysql]
|
||||
url = jdbc:mysql://looly.centos:3306/test_hutool?useSSL=false
|
||||
user = root
|
||||
pass = 123456
|
||||
|
||||
[postgre]
|
||||
url = jdbc:postgresql://looly.centos:5432/test_hutool
|
||||
user = postgres
|
||||
pass = 123456
|
||||
|
||||
[sqlserver]
|
||||
url = jdbc:sqlserver://looly.database.chinacloudapi.cn:1433;database=test;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.chinacloudapi.cn;loginTimeout=30;
|
||||
user = looly@looly
|
||||
pass = 123
|
||||
@@ -0,0 +1,54 @@
|
||||
#===================================================================
|
||||
# 数据库配置文件样例
|
||||
# DsFactory默认读取的配置文件是config/db.setting
|
||||
# db.setting的配置包括两部分:基本连接信息和连接池配置信息。
|
||||
# 基本连接信息所有连接池都支持,连接池配置信息根据不同的连接池,连接池配置是根据连接池相应的配置项移植而来
|
||||
#===================================================================
|
||||
|
||||
#----------------------------------------------------------------------------------------------------------------
|
||||
## 基本配置信息
|
||||
# JDBC URL,根据不同的数据库,使用相应的JDBC连接字符串
|
||||
url = jdbc:mysql://<host>:<port>/<database_name>
|
||||
# 用户名,此处也可以使用 user 代替
|
||||
username = 用户名
|
||||
# 密码,此处也可以使用 pass 代替
|
||||
password = 密码
|
||||
# JDBC驱动名,可选(Hutool会自动识别)
|
||||
driver = com.mysql.jdbc.Driver
|
||||
# 是否在日志中显示执行的SQL
|
||||
showSql = true
|
||||
# 是否格式化显示的SQL
|
||||
formatSql = true
|
||||
|
||||
#----------------------------------------------------------------------------------------------------------------
|
||||
## 连接池配置项
|
||||
|
||||
## ---------------------------------------------------- C3P0
|
||||
# 连接池中保留的最大连接数。默认值: 15
|
||||
maxPoolSize = 15
|
||||
# 连接池中保留的最小连接数,默认为:3
|
||||
minPoolSize = 3
|
||||
# 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3
|
||||
initialPoolSize = 3
|
||||
# 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0
|
||||
maxIdleTime = 0
|
||||
# 当连接池连接耗尽时,客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。默认: 0
|
||||
checkoutTimeout = 0
|
||||
# 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3
|
||||
acquireIncrement = 3
|
||||
# 定义在从数据库获取新连接失败后重复尝试的次数。默认值: 30 ;小于等于0表示无限次
|
||||
acquireRetryAttempts = 0
|
||||
# 重新尝试的时间间隔,默认为:1000毫秒
|
||||
acquireRetryDelay = 1000
|
||||
# 关闭连接时,是否提交未提交的事务,默认为false,即关闭连接,回滚未提交的事务
|
||||
autoCommitOnClose = false
|
||||
# c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试使用。默认值: null
|
||||
automaticTestTable = null
|
||||
# 如果为false,则获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常,但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。默认: false
|
||||
breakAfterAcquireFailure = false
|
||||
# 检查所有连接池中的空闲连接的检查频率。默认值: 0,不检查
|
||||
idleConnectionTestPeriod = 0
|
||||
# c3p0全局的PreparedStatements缓存的大小。如果maxStatements与maxStatementsPerConnection均为0,则缓存不生效,只要有一个不为0,则语句的缓存就能生效。如果默认值: 0
|
||||
maxStatements = 0
|
||||
# maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。默认值: 0
|
||||
maxStatementsPerConnection = 0
|
||||
@@ -0,0 +1,51 @@
|
||||
#===================================================================
|
||||
# 数据库配置文件样例
|
||||
# DsFactory默认读取的配置文件是config/db.setting
|
||||
# db.setting的配置包括两部分:基本连接信息和连接池配置信息。
|
||||
# 基本连接信息所有连接池都支持,连接池配置信息根据不同的连接池,连接池配置是根据连接池相应的配置项移植而来
|
||||
#===================================================================
|
||||
|
||||
#----------------------------------------------------------------------------------------------------------------
|
||||
## 基本配置信息
|
||||
# JDBC URL,根据不同的数据库,使用相应的JDBC连接字符串
|
||||
url = jdbc:mysql://<host>:<port>/<database_name>
|
||||
# 用户名,此处也可以使用 user 代替
|
||||
username = 用户名
|
||||
# 密码,此处也可以使用 pass 代替
|
||||
password = 密码
|
||||
# JDBC驱动名,可选(Hutool会自动识别)
|
||||
driver = com.mysql.jdbc.Driver
|
||||
# 是否在日志中显示执行的SQL
|
||||
showSql = true
|
||||
# 是否格式化显示的SQL
|
||||
formatSql = true
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------------------------------------------
|
||||
## 连接池配置项
|
||||
|
||||
## ---------------------------------------------------- Dbcp
|
||||
# (boolean) 连接池创建的连接的默认的auto-commit 状态
|
||||
defaultAutoCommit = true
|
||||
# (boolean) 连接池创建的连接的默认的read-only 状态。 如果没有设置则setReadOnly 方法将不会被调用。 ( 某些驱动不支持只读模式, 比如:Informix)
|
||||
defaultReadOnly = false
|
||||
# (String) 连接池创建的连接的默认的TransactionIsolation 状态。 下面列表当中的某一个: ( 参考javadoc) NONE READ_COMMITTED EAD_UNCOMMITTED REPEATABLE_READ SERIALIZABLE
|
||||
defaultTransactionIsolation = NONE
|
||||
# (int) 初始化连接: 连接池启动时创建的初始化连接数量,1。2 版本后支持
|
||||
initialSize = 10
|
||||
# (int) 最大活动连接: 连接池在同一时间能够分配的最大活动连接的数量, 如果设置为非正数则表示不限制
|
||||
maxActive = 100
|
||||
# (int) 最大空闲连接: 连接池中容许保持空闲状态的最大连接数量, 超过的空闲连接将被释放, 如果设置为负数表示不限制 如果启用,将定期检查限制连接,如果空闲时间超过minEvictableIdleTimeMillis 则释放连接 ( 参考testWhileIdle )
|
||||
maxIdle = 8
|
||||
# (int) 最小空闲连接: 连接池中容许保持空闲状态的最小连接数量, 低于这个数量将创建新的连接, 如果设置为0 则不创建 如果连接验证失败将缩小这个值( 参考testWhileIdle )
|
||||
minIdle = 0
|
||||
# (int) 最大等待时间: 当没有可用连接时, 连接池等待连接被归还的最大时间( 以毫秒计数), 超过时间则抛出异常, 如果设置为-1 表示无限等待
|
||||
maxWait = 30000
|
||||
# (String) SQL 查询, 用来验证从连接池取出的连接, 在将连接返回给调用者之前。 如果指定, 则查询必须是一个SQL SELECT 并且必须返回至少一行记录 查询不必返回记录,但这样将不能抛出SQL异常
|
||||
validationQuery = SELECT 1
|
||||
# (boolean) 指明是否在从池中取出连接前进行检验, 如果检验失败, 则从池中去除连接并尝试取出另一个。注意: 设置为true 后如果要生效,validationQuery 参数必须设置为非空字符串 参考validationInterval以获得更有效的验证
|
||||
testOnBorrow = false
|
||||
# (boolean) 指明是否在归还到池中前进行检验 注意: 设置为true 后如果要生效,validationQuery 参数必须设置为非空字符串
|
||||
testOnReturn = false
|
||||
# (boolean) 指明连接是否被空闲连接回收器( 如果有) 进行检验。 如果检测失败, 则连接将被从池中去除。注意: 设置为true 后如果要生效,validationQuery 参数必须设置为非空字符串
|
||||
testWhileIdle = false
|
||||
@@ -0,0 +1,55 @@
|
||||
#===================================================================
|
||||
# 数据库配置文件样例
|
||||
# DsFactory默认读取的配置文件是config/db.setting
|
||||
# db.setting的配置包括两部分:基本连接信息和连接池配置信息。
|
||||
# 基本连接信息所有连接池都支持,连接池配置信息根据不同的连接池,连接池配置是根据连接池相应的配置项移植而来
|
||||
#===================================================================
|
||||
|
||||
#----------------------------------------------------------------------------------------------------------------
|
||||
## 基本配置信息
|
||||
# JDBC URL,根据不同的数据库,使用相应的JDBC连接字符串
|
||||
url = jdbc:mysql://<host>:<port>/<database_name>
|
||||
# 用户名,此处也可以使用 user 代替
|
||||
username = 用户名
|
||||
# 密码,此处也可以使用 pass 代替
|
||||
password = 密码
|
||||
# JDBC驱动名,可选(Hutool会自动识别)
|
||||
driver = com.mysql.jdbc.Driver
|
||||
# 是否在日志中显示执行的SQL
|
||||
showSql = true
|
||||
# 是否格式化显示的SQL
|
||||
formatSql = true
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------------------------------------------
|
||||
## 连接池配置项
|
||||
|
||||
## ---------------------------------------------------- Druid
|
||||
# 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
|
||||
initialSize = 0
|
||||
# 最大连接池数量
|
||||
maxActive = 8
|
||||
# 最小连接池数量
|
||||
minIdle = 0
|
||||
# 获取连接时最大等待时间,单位毫秒。配置了maxWait之后, 缺省启用公平锁,并发效率会有所下降, 如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
|
||||
maxWait = 0
|
||||
# 是否缓存preparedStatement,也就是PSCache。 PSCache对支持游标的数据库性能提升巨大,比如说oracle。 在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。作者在5.5版本中使用PSCache,通过监控界面发现PSCache有缓存命中率记录, 该应该是支持PSCache。
|
||||
poolPreparedStatements = false
|
||||
# 要启用PSCache,必须配置大于0,当大于0时, poolPreparedStatements自动触发修改为true。 在Druid中,不会存在Oracle下PSCache占用内存过多的问题, 可以把这个数值配置大一些,比如说100
|
||||
maxOpenPreparedStatements = -1
|
||||
# 用来检测连接是否有效的sql,要求是一个查询语句。 如果validationQuery为null,testOnBorrow、testOnReturn、 testWhileIdle都不会其作用。
|
||||
validationQuery = SELECT 1
|
||||
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
|
||||
testOnBorrow = true
|
||||
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
|
||||
testOnReturn = false
|
||||
# 建议配置为true,不影响性能,并且保证安全性。 申请连接的时候检测,如果空闲时间大于 timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
|
||||
testWhileIdle = false
|
||||
# 有两个含义: 1) Destroy线程会检测连接的间隔时间 2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
|
||||
timeBetweenEvictionRunsMillis = 60000
|
||||
# 物理连接初始化的时候执行的sql
|
||||
connectionInitSqls = SELECT 1
|
||||
# 属性类型是字符串,通过别名的方式配置扩展插件, 常用的插件有: 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall
|
||||
filters = stat
|
||||
# 类型是List<com.alibaba.druid.filter.Filter>, 如果同时配置了filters和proxyFilters, 是组合关系,并非替换关系
|
||||
proxyFilters =
|
||||
@@ -0,0 +1,43 @@
|
||||
#===================================================================
|
||||
# 数据库配置文件样例
|
||||
# DsFactory默认读取的配置文件是config/db.setting
|
||||
# db.setting的配置包括两部分:基本连接信息和连接池配置信息。
|
||||
# 基本连接信息所有连接池都支持,连接池配置信息根据不同的连接池,连接池配置是根据连接池相应的配置项移植而来
|
||||
#===================================================================
|
||||
|
||||
#----------------------------------------------------------------------------------------------------------------
|
||||
## 基本配置信息
|
||||
# JDBC URL,根据不同的数据库,使用相应的JDBC连接字符串
|
||||
url = jdbc:mysql://<host>:<port>/<database_name>
|
||||
# 用户名,此处也可以使用 user 代替
|
||||
username = 用户名
|
||||
# 密码,此处也可以使用 pass 代替
|
||||
password = 密码
|
||||
# JDBC驱动名,可选(Hutool会自动识别)
|
||||
driver = com.mysql.jdbc.Driver
|
||||
# 是否在日志中显示执行的SQL
|
||||
showSql = true
|
||||
# 是否格式化显示的SQL
|
||||
formatSql = true
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------------------------------------------
|
||||
## 连接池配置项
|
||||
|
||||
## ---------------------------------------------------- HikariCP
|
||||
# 自动提交
|
||||
autoCommit = true
|
||||
# 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒
|
||||
connectionTimeout = 30000
|
||||
# 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟
|
||||
idleTimeout = 600000
|
||||
# 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';)
|
||||
maxLifetime = 1800000
|
||||
# 获取连接前的测试SQL
|
||||
connectionTestQuery = SELECT 1
|
||||
# 最小闲置连接数
|
||||
minimumIdle = 10
|
||||
# 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
|
||||
maximumPoolSize = 10
|
||||
# 连接只读数据库时配置为true, 保证安全
|
||||
readOnly = false
|
||||
@@ -0,0 +1,51 @@
|
||||
#===================================================================
|
||||
# 数据库配置文件样例
|
||||
# DsFactory默认读取的配置文件是config/db.setting
|
||||
# db.setting的配置包括两部分:基本连接信息和连接池配置信息。
|
||||
# 基本连接信息所有连接池都支持,连接池配置信息根据不同的连接池,连接池配置是根据连接池相应的配置项移植而来
|
||||
#===================================================================
|
||||
|
||||
#----------------------------------------------------------------------------------------------------------------
|
||||
## 基本配置信息
|
||||
# JDBC URL,根据不同的数据库,使用相应的JDBC连接字符串
|
||||
url = jdbc:mysql://<host>:<port>/<database_name>
|
||||
# 用户名,此处也可以使用 user 代替
|
||||
username = 用户名
|
||||
# 密码,此处也可以使用 pass 代替
|
||||
password = 密码
|
||||
# JDBC驱动名,可选(Hutool会自动识别)
|
||||
driver = com.mysql.jdbc.Driver
|
||||
# 是否在日志中显示执行的SQL
|
||||
showSql = true
|
||||
# 是否格式化显示的SQL
|
||||
formatSql = true
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------------------------------------------
|
||||
## 连接池配置项
|
||||
|
||||
## ---------------------------------------------------- Tomcat-Jdbc-Pool
|
||||
# (boolean) 连接池创建的连接的默认的auto-commit 状态
|
||||
defaultAutoCommit = true
|
||||
# (boolean) 连接池创建的连接的默认的read-only 状态。 如果没有设置则setReadOnly 方法将不会被调用。 ( 某些驱动不支持只读模式, 比如:Informix)
|
||||
defaultReadOnly = false
|
||||
# (String) 连接池创建的连接的默认的TransactionIsolation 状态。 下面列表当中的某一个: ( 参考javadoc) NONE READ_COMMITTED EAD_UNCOMMITTED REPEATABLE_READ SERIALIZABLE
|
||||
defaultTransactionIsolation = NONE
|
||||
# (int) 初始化连接: 连接池启动时创建的初始化连接数量,1。2 版本后支持
|
||||
initialSize = 10
|
||||
# (int) 最大活动连接: 连接池在同一时间能够分配的最大活动连接的数量, 如果设置为非正数则表示不限制
|
||||
maxActive = 100
|
||||
# (int) 最大空闲连接: 连接池中容许保持空闲状态的最大连接数量, 超过的空闲连接将被释放, 如果设置为负数表示不限制 如果启用,将定期检查限制连接,如果空闲时间超过minEvictableIdleTimeMillis 则释放连接 ( 参考testWhileIdle )
|
||||
maxIdle = 8
|
||||
# (int) 最小空闲连接: 连接池中容许保持空闲状态的最小连接数量, 低于这个数量将创建新的连接, 如果设置为0 则不创建 如果连接验证失败将缩小这个值( 参考testWhileIdle )
|
||||
minIdle = 0
|
||||
# (int) 最大等待时间: 当没有可用连接时, 连接池等待连接被归还的最大时间( 以毫秒计数), 超过时间则抛出异常, 如果设置为-1 表示无限等待
|
||||
maxWait = 30000
|
||||
# (String) SQL 查询, 用来验证从连接池取出的连接, 在将连接返回给调用者之前。 如果指定, 则查询必须是一个SQL SELECT 并且必须返回至少一行记录 查询不必返回记录,但这样将不能抛出SQL异常
|
||||
validationQuery = SELECT 1
|
||||
# (boolean) 指明是否在从池中取出连接前进行检验, 如果检验失败, 则从池中去除连接并尝试取出另一个。注意: 设置为true 后如果要生效,validationQuery 参数必须设置为非空字符串 参考validationInterval以获得更有效的验证
|
||||
testOnBorrow = false
|
||||
# (boolean) 指明是否在归还到池中前进行检验 注意: 设置为true 后如果要生效,validationQuery 参数必须设置为非空字符串
|
||||
testOnReturn = false
|
||||
# (boolean) 指明连接是否被空闲连接回收器( 如果有) 进行检验。 如果检测失败, 则连接将被从池中去除。注意: 设置为true 后如果要生效,validationQuery 参数必须设置为非空字符串
|
||||
testWhileIdle = false
|
||||
@@ -0,0 +1,29 @@
|
||||
#--------------------------------------
|
||||
# MongoDB 连接设定
|
||||
# author xiaoleilu
|
||||
#--------------------------------------
|
||||
|
||||
#每个主机答应的连接数(每个主机的连接池大小),当连接池被用光时,会被阻塞住 ,默以为10 --int
|
||||
connectionsPerHost=100
|
||||
#线程队列数,它以connectionsPerHost值相乘的结果就是线程队列最大值。如果连接线程排满了队列就会抛出“Out of semaphores to get db”错误 --int
|
||||
threadsAllowedToBlockForConnectionMultiplier=10
|
||||
#被阻塞线程从连接池获取连接的最长等待时间(ms) --int
|
||||
maxWaitTime = 120000
|
||||
#在建立(打开)套接字连接时的超时时间(ms),默以为0(无穷) --int
|
||||
connectTimeout=0
|
||||
#套接字超时时间;该值会被传递给Socket.setSoTimeout(int)。默以为0(无穷) --int
|
||||
socketTimeout=0
|
||||
#是否打开长连接. defaults to false --boolean
|
||||
socketKeepAlive=false
|
||||
|
||||
user = test1
|
||||
pass = 123456
|
||||
database = test
|
||||
|
||||
#---------------------------------- MongoDB实例连接
|
||||
[master]
|
||||
host = 127.0.0.1:27017
|
||||
|
||||
[slave]
|
||||
host = 127.0.0.1:27017
|
||||
#-----------------------------------------------------
|
||||
59
hutool-db/src/test/resources/config/redis.setting
Normal file
59
hutool-db/src/test/resources/config/redis.setting
Normal file
@@ -0,0 +1,59 @@
|
||||
#----------------------------------------------------------------------------------
|
||||
# Redis客户端配置样例
|
||||
# 每一个分组代表一个Redis实例
|
||||
# 无分组的Pool配置为所有分组的共用配置,如果分组自己定义Pool配置,则覆盖共用配置
|
||||
# 池配置来自于:https://www.cnblogs.com/jklk/p/7095067.html
|
||||
#----------------------------------------------------------------------------------
|
||||
|
||||
#----- 默认(公有)配置
|
||||
# 地址,默认localhost
|
||||
host = localhost
|
||||
# 端口,默认6379
|
||||
port = 6379
|
||||
# 超时,默认2000
|
||||
timeout = 2000
|
||||
# 连接超时,默认timeout
|
||||
connectionTimeout = 2000
|
||||
# 读取超时,默认timeout
|
||||
soTimeout = 2000
|
||||
# 密码,默认无
|
||||
password =
|
||||
# 数据库序号,默认0
|
||||
database = 0
|
||||
# 客户端名,默认"Hutool"
|
||||
clientName = Hutool
|
||||
# SSL连接,默认false
|
||||
ssl = false;
|
||||
|
||||
#----- 自定义分组的连接
|
||||
[custom]
|
||||
# 地址,默认localhost
|
||||
host = localhost
|
||||
# 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
|
||||
BlockWhenExhausted = true;
|
||||
# 设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
|
||||
evictionPolicyClassName = org.apache.commons.pool2.impl.DefaultEvictionPolicy
|
||||
# 是否启用pool的jmx管理功能, 默认true
|
||||
jmxEnabled = true;
|
||||
# 是否启用后进先出, 默认true
|
||||
lifo = true;
|
||||
# 最大空闲连接数, 默认8个
|
||||
maxIdle = 8
|
||||
# 最小空闲连接数, 默认0
|
||||
minIdle = 0
|
||||
# 最大连接数, 默认8个
|
||||
maxTotal = 8
|
||||
# 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
|
||||
maxWaitMillis = -1
|
||||
# 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
|
||||
minEvictableIdleTimeMillis = 1800000
|
||||
# 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
|
||||
numTestsPerEvictionRun = 3;
|
||||
# 对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
|
||||
SoftMinEvictableIdleTimeMillis = 1800000
|
||||
# 在获取连接的时候检查有效性, 默认false
|
||||
testOnBorrow = false
|
||||
# 在空闲时检查有效性, 默认false
|
||||
testWhileIdle = false
|
||||
# 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
|
||||
timeBetweenEvictionRunsMillis = -1
|
||||
17
hutool-db/src/test/resources/logback.xml
Normal file
17
hutool-db/src/test/resources/logback.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<configuration scan="false">
|
||||
<property name="format" value="%d{HH:mm:ss.SSS} [%thread] %-5level %c:%L- %msg%n" />
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!-- encoder 默认配置为PatternLayoutEncoder -->
|
||||
<encoder>
|
||||
<pattern>${format}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="debug">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
2
hutool-db/src/test/resources/simplelogger.properties
Normal file
2
hutool-db/src/test/resources/simplelogger.properties
Normal file
@@ -0,0 +1,2 @@
|
||||
org.slf4j.simpleLogger.defaultLogLevel = debug
|
||||
org.slf4j.simpleLogger.log.com.zaxxer.hikari = info
|
||||
Reference in New Issue
Block a user