WHCSRL 技术网

Java连接数据库查询数据实例

Java工程结构图

 

 maven工程,可参考: https://gitee.com/automvc/bee

 bee.properties

  1. #bee.db.dbName=MySQL
  2. #bee.db.driverName = com.mysql.jdbc.Driver
  3. bee.db.url =jdbc:mysql://localhost:3306/bee?characterEncoding=UTF-8
  4. bee.db.username =root
  5. bee.db.password =

 BeeTest.java   

  1. /*
  2. * Copyright 2016-2021 the original author.All rights reserved.
  3. * Kingstar(honeysoft@126.com)
  4. * The license,see the LICENSE file.
  5. */
  6. package org.bee.exam;
  7. import java.util.List;
  8. import org.bee.exam.entity.Orders;
  9. import org.teasoft.bee.osql.Suid;
  10. import org.teasoft.honey.osql.core.BeeFactoryHelper;
  11. /**
  12. * @author Kingstar
  13. * @since 1.9
  14. */
  15. public class BeeTest {
  16. public static void main(String[] args) {
  17. Suid suid = BeeFactoryHelper.getSuid();
  18. Orders orders1 = new Orders();
  19. orders1.setName("Bee(ORM Framework)");
  20. List<Orders> list1 = suid.select(orders1); //select
  21. for (int i = 0; i < list1.size(); i++) {
  22. System.out.println(list1.get(i).toString());
  23. }
  24. }
  25. }
  26. //注意: 事务,分页,排序,范围查询,查询结果直接返回json等都支持,这里只是一个入门例子.
  27. //更加应用参考,请查看官网: 更多例子 和 综合应用 .
  28. //https://github.com/automvc/bee
  29. //https://gitee.com/automvc/bee

表orders对应的实体Javabean--Orders.

  1. package org.bee.exam.entity;
  2. import java.io.Serializable;
  3. import java.math.BigDecimal;
  4. /**
  5. *@author Honey
  6. *Create on 2020-10-01 12:41:06
  7. */
  8. public class Orders implements Serializable {
  9. private static final long serialVersionUID = 1593472489996L;
  10. //time-->String
  11. private Long id;
  12. private String userid;
  13. private String name;
  14. private BigDecimal total;
  15. private String createtime;
  16. private String remark;
  17. private String sequence;
  18. private String abc;
  19. private String updatetime;
  20. //...get,set
  21. public String toString(){
  22. StringBuffer str=new StringBuffer();
  23. str.append("Orders[");
  24. str.append("id=").append(id);
  25. str.append(",userid=").append(userid);
  26. str.append(",name=").append(name);
  27. str.append(",total=").append(total);
  28. str.append(",createtime=").append(createtime);
  29. str.append(",remark=").append(remark);
  30. str.append(",sequence=").append(sequence);
  31. str.append(",abc=").append(abc);
  32. str.append(",updatetime=").append(updatetime);
  33. str.append("]");
  34. return str.toString();
  35. }
  36. }

运行结果:

  1. Orders[id=637266389106717,userid=bee,name=Bee(ORM Framework),total=91.990000,createtime=null,remark=null,sequence=null,abc=null,updatetime=null]
  2. Orders[id=637266389106743,userid=bee,name=Bee(ORM Framework),total=91.990000,createtime=2021-10-25 12:29:16,remark=null,sequence=null,abc=null,updatetime=null]
  3. Orders[id=637266389106744,userid=bee,name=Bee(ORM Framework),total=91.990000,createtime=null,remark=null,sequence=null,abc=null,updatetime=null]

查询订单表数据:

关键语句只要两句就搞定.

    Suid suid = BeeFactoryHelper.getSuid();

    List<Orders> list1 = suid.select(orders1);//select 查询订单表数据

配置,只需要将数据的连接, 用户名,密码等信息写在bee.properties即可. 

如此简单, 在于一个ORM框架Bee, 该框架帮我们处理了许多对数据库的固定操作,让我们从操作DB的繁杂步骤解放出来,尽快进入到业务逻辑的开发中.

推荐阅读