如何正確使用Hutool和Guava這兩個豐富的工具集,自己學習測試,樣例

一、Hutool 關於數據庫的操作示例

1.1 連接數據庫:加載 config/db.setting

在這裏插入圖片sssss

#===================================================================
# 數據庫配置文件樣例
# DsFactory默認讀取的配置文件是config/db.setting
# db.setting的配置包括兩部分:基本連接信息和連接池配置信息。
# 基本連接信息所有連接池都支持,連接池配置信息根據不同的連接池,連接池配置是根據連接池相應的配置項移植而來
#===================================================================

## 打印SQL的配置
# 是否在日誌中顯示執行的SQL,默認false
showSql = true
# 是否格式化顯示的SQL,默認false
formatSql = true
# 是否顯示SQL參數,默認false
showParams = true
# 打印SQL的日誌等級,默認debug
sqlLevel = debug

[mysql]
url = jdbc:mysql://localhost:3306/proce?useSSL=false # 這裏指定數據庫
user = root
pass = root

這裏是測試model的建表語句和對應Model類

CREATE TABLE `user` (
  `id` int(11) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  `gender` varchar(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
@Data
public class User {

	private Integer id;

	private String name;

	private int age;

	private String birthday;

	private boolean gender;
}

1.1 測試 MetaUtil

public class MetaUtilTest {
	DataSource ds = DSFactory.get("mysql"); // 加載 Mysql 數據庫
	@Test
	public void getTablesTest() {
		List<String> tables = MetaUtil.getTables(ds);
	}
	@Test
	public void getTableMetaTest() {
		Table table = MetaUtil.getTableMeta(ds, "user");
	}
}

返回數據

tables = [Atest, author, book, city, state, user]
table  = {
"pkNames": [],
"columns": [
	{
		"size": 10,
		"isNullable": true,
		"name": "id",
		"typeName": "INT",
		"comment": "",
		"type": 4,
		"tableName": "user"
	},
	{
		"size": 20,
		"isNullable": true,
		"name": "name",
		"typeName": "VARCHAR",
		"comment": "",
		"type": 12,
		"tableName": "user"
	},
	{
		"size": 10,
		"isNullable": true,
		"name": "birthday",
		"typeName": "DATE",
		"comment": "",
		"type": 91,
		"tableName": "user"
	},
	{
		"size": 2,
		"isNullable": true,
		"name": "gender",
		"typeName": "VARCHAR",
		"comment": "",
		"type": 12,
		"tableName": "user"
	}
		],
"comment": "",
"tableName": "user"
}

1.2 conditionTest

@Test
	public void toStringTest() {
		Condition c1 = new Condition("name", null);
		System.out.println(c1);
		Condition c2 = new Condition("birthday", "!= null");
		System.out.println(c2);
		Condition c3 = new Condition("name", "= zhangsan");
		System.out.println(c3);
		Condition c4 = new Condition("name", "like %aaa");
		System.out.println(c4);
		Condition c5 = new Condition("id", "in 1,2,3");
		System.out.println(c5);
		Condition c6 = new Condition("age", "between 12 and 13");
		System.out.println(c6);
	}

控制檯輸出爲,默認條件佔位符爲 True

name IS NULL
birthday IS NOT NULL
name = ?
name LIKE ?
id IN (?,?,?)
age BETWEEN ? AND ?

還有關於條件佔位符的開啓與關閉,這裏指定爲關閉

@Test
	public void toStringNoPlaceHolderTest() {
		Condition conditionNull = new Condition("user", null);
		conditionNull.setPlaceHolder(false);
		System.out.println(conditionNull);

		Condition conditionNotNull = new Condition("user", "!= null");
		conditionNotNull.setPlaceHolder(false);
		System.out.println(conditionNotNull);

		Condition conditionEquals = new Condition("user", "= zhangsan");
		conditionEquals.setPlaceHolder(false);
		System.out.println(conditionEquals);

		Condition conditionLike = new Condition("user", "like %aaa");
		conditionLike.setPlaceHolder(false);
		System.out.println(conditionLike);

		Condition conditionIn = new Condition("user", "in 1,2,3");
		conditionIn.setPlaceHolder(false);
		System.out.println(conditionIn);

		Condition conditionBetween = new Condition("user", "between 12 and 13");
		conditionBetween.setPlaceHolder(false);
		System.out.println(conditionBetween);
	}

控制檯輸出爲,和上面的輸出比較一下區別

user IS NULL
user IS NOT NULL
user = zhangsan
user LIKE %aaa
user IN (1,2,3)
user BETWEEN 12 AND 13

1.3 SqlBuilder測試

@Test
	public void queryNullTest() {
		SqlBuilder builder = SqlBuilder.create().select().from("user").where(new Condition("name", "= null"));
		System.out.println(builder);
		
		SqlBuilder builder2 = SqlBuilder.create().select().from("user").where(new Condition("name", "is null"));
		System.out.println(builder2);

		SqlBuilder builder3 = SqlBuilder.create().select().from("user").where(LogicalOperator.OR, new Condition("name", "!= null"),new Condition("name","like%as"));
		System.out.println(builder3);

		SqlBuilder builder4 = SqlBuilder.create().select().from("user").where(LogicalOperator.AND, new Condition("name", "is not null"),new Condition("id", "in 12,13,14"));
		System.out.println(builder4);
	}

控制檯輸出爲

SELECT * FROM user WHERE name IS NULL
SELECT * FROM user WHERE name IS NULL
SELECT * FROM user WHERE name IS NOT NULL OR name = ?
SELECT * FROM user WHERE name IS NOT NULL AND id IN (?,?,?)

下一篇,基於Hutool包的的增刪改查操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章