数据查询应该简单易用,因此我编写了这个工具javadb。 | java | java 技术论坛-380玩彩网官网入口
javadb
数据查询应该简单易用,因此我编写了这个工具。
1. 加载扩展和配置依赖
在 pom.xml
中添加以下依赖:
<dependency>
<groupid>io.github.mydtxgroupid> <artifactid>javadbartifactid> <version>1.0.1version>dependency>
<dependency>
<groupid>org.projectlombokgroupid> <artifactid>lombokartifactid> <optional>trueoptional> <version>1.18.34version>dependency>
<dependency>
<groupid>com.mysqlgroupid> <artifactid>mysql-connector-jartifactid> <scope>runtimescope> <version>8.0.33version>dependency>
<dependency>
<groupid>com.zaxxergroupid> <artifactid>hikaricpartifactid> <version>6.2.1version>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
:
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 进行数据查询、删除、更新和插入操作。希望这个工具能帮助你简化数据操作流程。
哇,php开发转java