只需用ConnectionUtil.java
和DBReflectionUtil.java
搞定簡單并且多屬性眯亦,數(shù)據(jù)量大的數(shù)據(jù)庫CURD般码。
點擊查看源文件
約束條件
- 目前僅支持int,float,long,double,String數(shù)據(jù)類型
- 目前僅支持 "=", "like", "and", "or"操作
- 實體類中的字段與表中字段一一對應(yīng)
- 實體類中需存在無參構(gòu)造函數(shù)以及getter&setter方法
功能介紹
用于小型的項目板祝,簡化數(shù)據(jù)庫訪問操作
- 添加(含id添加忽略),表中若存在id字段券时,請設(shè)為自增
- 刪除:清空所有記錄,多條件刪除,單條件刪除
- 更新:無條件更新記錄舵鳞,多條件更新記錄蜓堕,單條件更新記錄
- 查詢:無條件全部查詢,多條件查詢多條記錄迂猴,多條件查詢單條記錄背伴,
單條件查詢多條記錄,單條件查詢單條記錄
使用說明
文件說明
- ConnectionUtil.java : 用于連接數(shù)據(jù)庫息尺,在里面替換數(shù)據(jù)庫名
- DBReflectionUtil.java:通用的增刪改查操作疾掰。
假設(shè)我們有個Student實體類,并存在students表
//實體類
package com.sprint.entity;
public class Student {
private int id;
private String name;
private int age;
private String sex;
private String school;
private String major;
private String clazz;
//省略構(gòu)造 && gettter && setter方法
}
//對應(yīng)表結(jié)構(gòu)
create table students (
id int auto_increment,
name varchar(128),
age int,
sex varchar(5),
school varchar(128),
major varchar(128),
clazz varchar(128)
)engine=innodb default charset = utf8;
一般需要再封裝一層dao炭懊,看個人編程規(guī)范吧侮腹。這里直接使用DBReflectionUtil類操作
添加數(shù)據(jù): public static int insert(String tableName, T object)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
object |
存儲的對象 |
Student stu = new Student("name", 18, "male", "CodeUniversity", "SoftWare", "class01");
DBReflectionUtil.insert("students", stu))
清空所有記錄:public static int deleteAll(String tableName)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
DBReflectionUtil.deleteAll("students");
單條件刪除記錄:public static int deleteByKey(String tableName, Map<String, Object>map, String operator1)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
map |
條件字段和字段值組成的key-value |
operator1 |
操作類型广恢,目前只支持"="或"like" |
//例如:刪除students中 name前綴為'邢',需要自己進行字符匹配
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "邢%");
DBReflectionUtil.deleteByKeys("students", map, "like");
多條件刪除記錄:public static int deleteByKeys(String tableName, Map<String, Object>map, String operator1, String operator2)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
map |
條件字段和字段值組成的key-value |
operator1 |
操作類型,目前只支持"="或"like" |
operator2 |
邏輯操作至非,目前只支持"and"或"or" |
//例如:刪除students中 age=18并且 sex='女'
Map<String, Object> map = new HashMap<String, Object>();
map.put("sex", "女");
map.put("age", 18);
DBReflectionUtil.deleteByKeys("students", map, "=", "and");
無條件更新:public static int updateAll(String tableName, Map<String, Object> map)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
map |
需要更新的字段和字段值組成的key-value |
//假設(shè)將students中的age更新為1000, major更新為"水下工作者"
Map<String, Object> map = new HashMap<String, Object>();
map.put("age", 1000);
map.put("major", "水下工作者");
DBReflectionUtil.updateAll("students", map);
單條件更新:public static int updateByKey(String tableName, Map<String, Object> updateMap, Map<String, Object> optionMap, String operator1)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
updateMap |
需要更新的字段和字段值組成的key-value |
optionMap |
條件字段和字段值組成的key-value |
operator1 |
操作類型荒椭,目前只支持"="或"like" |
//假設(shè)將name中姓“駱”的age更改為20
Map<String, Object> updateMap = new HashMap<String, Object>();
map.put("age", 20);
Map<String, Object> optionMap = new HashMap<String, Object>();
map.put("name", "駱%");
DBReflectionUtil.updateByKeys("students", updateMap, optionMap, "like");
多條件更新:public static int updateByKeys(String tableName, Map<String, Object> updateMap, Map<String, Object> optionMap, String operator1, String operator2)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
updateMap |
需要更新的字段和字段值組成的key-value |
optionMap |
條件字段和字段值組成的key-value |
operator1 |
操作類型趣惠,目前只支持"="或"like" |
operator2 |
邏輯操作,目前只支持"and" 或 "or" |
//假設(shè)將age=18并且major='SoftWare'的記錄中clazz更改為"1403"
Map<String, Object> updateMap = new HashMap<String, Object>();
map.put("clazz", "1403");
Map<String, Object> optionMap = new HashMap<String, Object>();
map.put("age", 18);
map.put("major", "SoftWare");
DBReflectionUtil.updateByKeys("students", updateMap, optionMap, "=", "and");
無條件查詢操作接口:public static <T> List<T> findAll(String table, T object)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
object |
獲取的對象草戈,不用設(shè)置數(shù)據(jù)侍瑟,僅 new Object() |
//例如:獲取所以的學(xué)生
List<Student> list = DBReflectionUtil.findAll("students", new Student());
單條件查詢單條:public static <T> T findOnlyByKey(String tableName, T object, Map<String, Object> map)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
object |
獲取的對象,不用設(shè)置數(shù)據(jù)费韭,僅 new Object() |
map |
條件字段和字段值組成的key-value |
// 例如:取出表中id為2的記錄
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 2);
Student stu = DBReflectionUtil.findOnlyByKey("students", new Student(),map);
單條件查詢多條:pulic static <T> List<T> findMoreByKey(String tableName, T object, , Map<String, Object> map, String operator1)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
object |
獲取的對象星持,不用設(shè)置數(shù)據(jù)弹灭,僅 new Object() |
map |
條件字段和字段值組成的key-value |
operator1 |
操作類型,目前只支持"="或"like" |
//例如:查找name中含"駱"的學(xué)生
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "駱%");
List<Student> list = DBReflectionUtil.findMoreByKey("students", new Student(), map, "like");
多條件查詢多條記錄:public static <T> List<T> findMoreByKeys(String tableName, T object, Map<String, Object> map, String operator1, String operator2)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
object |
獲取的對象损痰,不用設(shè)置數(shù)據(jù)酒来,僅 new Object() |
map |
條件字段和字段值組成的key-value |
operator1 |
操作類型堰汉,目前只支持"="或"like" |
operator2 |
邏輯操作,目前只支持"and" 或 "or" |
//例如:查詢name中姓"駱"翘鸭,并且school為"中國"為前綴的學(xué)生
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "駱%");
map.put("sex", "中國%");
List<Student> list = DBReflectionUtil.findMoreByKeys("students", new Student(), map, "like", "and");
多條件查詢單條記錄:public static <T> T findOnlyByKeys(String tableName, T object, Map<String, Object> map, String operator1, String operator2)
參數(shù)名 |
參數(shù)名描述 |
tableName |
表名 |
object |
獲取的對象就乓,不用設(shè)置數(shù)據(jù)拱烁,僅 new Object() |
map |
條件字段和字段值組成的key-value |
operator1 |
操作類型噩翠,目前只支持"="或"like" |
operator2 |
邏輯操作伤锚,目前只支持"and" 或 "or" |
//例如:查詢sex為"男",并且id為4為前綴的學(xué)生
Map<String, Object> map = new HashMap<String, Object>();
map.put("sex", "男");
map.put("id", 4);
Student stu = DBReflectionUtil.findOnlyByKeys("students", new Student(), map, "=", "and");
下一版需要改進的地方(當(dāng)前版的缺點)
- 合理猛们,全面的數(shù)據(jù)校驗
- 查詢狞洋,更新,刪除的sql語句改成 IN類型
- 支持時間類型