数据查询应该简单易用,因此我编写了这个工具javadb。

JAVADB

数据查询应该简单易用,因此我编写了这个工具。

1. 加载扩展和配置依赖

pom.xml 中添加以下依赖:

<dependency>
 <groupId>io.github.mydtx</groupId> <artifactId>javadb</artifactId> <version>1.0.1</version></dependency>

<dependency>
 <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> <version>1.18.34</version></dependency>

<dependency>
 <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> <version>8.0.33</version></dependency>

<dependency>
 <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>6.2.1</version></dependency>

2. 定义模型

定义一个继承自 Model 的模型类,例如 UnionModel

package com.liuxiaokang.model;

import com.liuxiaokang.config.DbConnection;
import com.liuxiaokang.db.Model;

public class UnionModel extends Model {

 public UnionModel() { super("sg_union"); // 设置表名
 connection(DbConnection.getConnection(0)); // 设置数据库连接池
 }}

3. 查询数据

查询单条数据
  • 返回 Map
UnionModel unionModel = new UnionModel();
Map<String, Object> result = unionModel.select("id, name").find();
  • 映射到实体类:
SgUnion sgUnion = unionModel.find(SgUnion.class);
查询多条数据
  • 返回 List<Map>
List<Map<String, Object>> results = unionModel.where("id", 1).whereOr("id", 2).select("id, name").get();
  • 映射到实体类列表:
List<SgUnion> sgUnions = unionModel.where("id", 1).whereOr("id", 2).get(SgUnion.class);
分页查询
  • 返回 Map
PageResult pageResult = unionModel.where("id", ">", 1).page(1, 5);
  • 映射到实体类列表:
PageResult<SgUnion> sgUnionPageResult = unionModel.where("id", "<=", 100).page(1, 5, SgUnion.class);
联表查询
  • 左连接:
Map<String, Object> leftJoinResult = unionModel.leftJoin("sg_union_staff", "sg_union_staff.union_id", "sg_union.id")
 .where("sg_union.id", 1) .select("sg_union.id, sg_union.name, sg_union_staff.workno") .find();
  • 右连接:
Map<String, Object> rightJoinResult = unionModel.rightJoin("sg_union_staff", "sg_union_staff.union_id", "sg_union.id")
 .where("sg_union.id", 1) .select("sg_union.id, sg_union.name, sg_union_staff.workno") .find();
  • 内连接:
Map<String, Object> innerJoinResult = unionModel.innerJoin("sg_union_staff", "sg_union_staff.union_id", "sg_union.id")
 .where("sg_union.id", 1) .select("sg_union.id, sg_union.name, sg_union_staff.workno") .find();

4. 删除数据

int deleteCount = unionModel.where("id", 52538).delete();

5. 更新数据

使用 Map 更新
int updateCount = unionModel.where("id", 55828).update(Map.of("name", "国家电网有限公司工会11"));
使用实体类更新
SgUnion sgUnion = unionModel.where("id", 55828).find(SgUnion.class);
sgUnion.setName("国家电网有限公司工会222");
int updateCount = unionModel.where("id", 55828).update(sgUnion);

6. 插入数据

使用实体类插入
SgUnion sgUnion = new SgUnion();
sgUnion.setName("新增工会");
sgUnion.setParent_id(1L);
sgUnion.setRoot_id(1L);
sgUnion.setLft(1L);
sgUnion.setRgt(2L);
sgUnion.setListorder(1L);
sgUnion.setHidden(0);
sgUnion.setStatus(true);
sgUnion.setCreated_at(System.currentTimeMillis() / 1000);
sgUnion.setUpdated_at(System.currentTimeMillis() / 1000);
sgUnion.setWeight(1);
sgUnion.setAddress_detail("新增工会地址");

int insertCount = unionModel.insert(sgUnion);
System.out.println(insertCount);
使用 Map 插入
HashMap<String, Object> data = new HashMap<>();
data.put("name", "新增工会map");
data.put("parent_id", 1L);
data.put("root_id", 1L);
data.put("lft", 1L);
data.put("rgt", 2L);
data.put("listorder", 1L);
data.put("hidden", 0);
data.put("status", true);
data.put("created_at", System.currentTimeMillis() / 1000);
data.put("updated_at", System.currentTimeMillis() / 1000);
data.put("weight", 1);
data.put("address_detail", "新增工会地址map");

int insertCount = unionModel.insert(data);
System.out.println(insertCount);
批量插入
ArrayList<HashMap<String, Object>> batchData = new ArrayList<>();
for (int i = 0; i < 10; i++) {
 HashMap<String, Object> data = new HashMap<>(); data.put("name", "新增工会map");
 data.put("parent_id", 1L); data.put("root_id", 1L); data.put("lft", 1L); data.put("rgt", 2L); data.put("listorder", 1L); data.put("hidden", 0); data.put("status", true); data.put("created_at", System.currentTimeMillis() / 1000); data.put("updated_at", System.currentTimeMillis() / 1000); data.put("weight", 1); data.put("address_detail", "新增工会地址map");
 batchData.add(data);}

int[] insertCounts = unionModel.batchInsert(batchData);
System.out.println(Arrays.toString(insertCounts));

通过以上步骤,你可以轻松地使用 JAVADB 进行数据查询、删除、更新和插入操作。希望这个工具能帮助你简化数据操作流程。

讨论数量: 1

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!