使用手册
基础示例
增
M userModel = new M("user");
userModel.put("name","刚刚");
Boolean insertSuc = userModel.insert();
Integer insertId = userModel.getId();
删
M userModel = new M("user");
userModel.where("id",10);
Boolean delSuc = userModel.delete();
查
M userModel = new M("user");
userModel.where("id",10);
ArrayList<Map<String,Object>> userArr = userModel.select();
改
M userModel = new M("user");
userModel.where("id",10);
userModel.put("id",10);
Integer updateCount = userModel.update();
api
新建模型 new M("tableName")
M tableModel = new M("tableName");
表名前缀 tableNamePrefix
class M{
...
private String tableNamePrefix="hsy_";
...
}
tableModel.setTableNamePrefix("hsy_");
tableModel.getTableNamePrefix();
查询条件 where
tableModel.where("id",10);
tableModel.where("age",">=",20);
设置键值对 put
tableModel.put("age",20);
tableModel.put("name","gg");
添加 insert
M tableModel = new M("tableName");
tableModel.put("age",20);
tableModel.put("name","gg");
tableModel.insert();
获取id getId
M tableModel = new M("tableName");
tableModel.put("age",20);
tableModel.put("name","gg");
tableModel.insert();
tableModel.getId();
修改 update
M tableModel = new M("tableName");
tableModel.where("name","gg");
tableModel.put("age",20);
tableModel.update();
返回的字段 field
M tableModel = new M("tableName");
tableModel.where("name","gg");
tableModel.field("age");
class M{
...
private String fieldString="*";
...
}
返回的行 limit
M tableModel = new M("tableName");
tableModel.where("name","gg");
tableModel.limit(20);
tableModel.limit(1,20);
查询所有行 select
M tableModel = new M("tableName");
tableModel.where("name","gg");
ArrayList<Map<String,Object>> dataArr = tableModel.select();
查询1行 find
M tableModel = new M("tableName");
tableModel.where("name","gg");
Map<String,Object> data = tableModel.find();
删除 delete
M tableModel = new M("tableName");
tableModel.where("name","gg");
tableModel.delete();
源码
package com.damei.hsydata.util;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.damei.common.util.PropertyUtil;
import org.apache.commons.dbcp.BasicDataSource;
public class M {
private static BasicDataSource dataSource = new BasicDataSource();
static{
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(
"jdbc:mysql://"
+ PropertyUtil.getProperty("jdbc.host") + "/"
+ PropertyUtil.getProperty("jdbc.database") + "?useSSL=false&autoReconnect=true&failOverReadOnly=false&autoReconnectForPools=true"
);
dataSource.setUsername(PropertyUtil.getProperty("jdbc.username"));
dataSource.setPassword(PropertyUtil.getProperty("jdbc.password"));
dataSource.setMaxActive(3);
dataSource.setMaxActive(5);
dataSource.setInitialSize(10);
}
private String tableNamePrefix="hsy_";
private String fieldString="*";
private Boolean isSetLimit=false;
private int limitNum;
private int skipNum;
private int id;
private String tableName;
private PreparedStatement lastSql;
private ArrayList<Map<String,String>> whereList;
private ArrayList<Map<String,String>> putList;
public M(String tableName){
this.tableName = tableName;
this.whereList = new ArrayList<Map<String,String>>();
this.putList = new ArrayList<Map<String,String>>();
}
public String getTableNamePrefix(){
return this.tableNamePrefix;
}
public String setTableNamePrefix(String tableNamePrefix){
return this.tableNamePrefix = tableNamePrefix;
}
public PreparedStatement getLastSql(){
return this.lastSql;
}
public M where(String key,Object value){
return this.where(key,"=",value);
}
public M where(String key,String type,Object value){
Map<String,String> obj = new HashMap<String, String>();
obj.put("key",key);
obj.put("value",value.toString());
obj.put("type",type);
this.whereList.add(obj);
return this;
}
public M field(String field){
this.fieldString = field;
return this;
}
public M limit(int limit){
this.limit(0, limit);
this.isSetLimit = true;
return this;
}
public M limit(int skip,int limit){
this.limitNum = limit;
this.skipNum = skip;
this.isSetLimit = true;
return this;
}
public M put(String key,Object value){
return this.put(key,"=",value);
}
public M put(String key,String type,Object value){
Map<String,String> obj = new HashMap<String, String>();
obj.put("key",key);
obj.put("value",value.toString());
obj.put("type",type);
this.putList.add(obj);
return this;
}
public Map<String,Object> find(){
this.limit(this.skipNum,1);
ArrayList<Map<String,Object>> reArr = this.select();
if (reArr!=null && reArr.size() > 0) {
return reArr.get(0);
}
return null;
}
private String whereToSql(){
String sql = "";
if (this.whereList.size() > 0) {
sql += " WHERE ";
for (Map<String,String> e : this.whereList){
String type = e.get("type");
switch (type) {
case "=":
case ">=":
case "<=":
case ">":
case "<":
sql += " "+e.get("key") +" "+ type +" ? and";
break;
case "in":
ArrayList<String> inArr = new ArrayList();
for (String retval: e.get("value").split(",")){
inArr.add("?");
}
sql += " "+e.get("key") +" "+ type +" ("+String.join(",",inArr)+") and";
break;
default:
throw new IllegalArgumentException("where 不支持"+type+"类型");
}
}
sql = sql.substring(0,sql.length() - 3);
}
return sql;
}
public ArrayList<Map<String,Object>> select(){
String sql = "SELECT "+ this.fieldString + " FROM "+this.tableNamePrefix+this.tableName ;
sql += whereToSql();
if (isSetLimit && this.limitNum >-1) {
sql += " limit "+this.skipNum +","+this.limitNum +" ";
}
Connection con = null;
PreparedStatement pStemt = null;
try {
con = dataSource.getConnection();
pStemt = con.prepareStatement(sql);
Integer parameterIndex = 0;
if (this.whereList.size() > 0) {
for (Map<String,String> e : this.whereList){
String type = e.get("type");
switch (type) {
case "in":
for (String retval: e.get("value").split(",")){
System.out.println(retval);
pStemt.setString(++parameterIndex, retval);
}
break;
default:
pStemt.setString(++parameterIndex, e.get("value"));
break;
}
}
}
ResultSet res = pStemt.executeQuery();
ArrayList<Map<String,Object>> reArr = new ArrayList<Map<String,Object>>();
while (res.next()){
ResultSetMetaData metaData = res.getMetaData();
int colCount = metaData.getColumnCount();
Map<String,Object> item = new HashMap<String,Object>();
for (int i = 1; i < colCount+1; i++) {
item.put(metaData.getColumnLabel(i), res.getObject(i));
}
reArr.add(item);
}
res.close();
this.lastSql = pStemt;
return reArr;
} catch (Exception throwables) {
this.lastSql = pStemt;
System.out.println("error "+ pStemt);
throwables.printStackTrace();
} finally {
try {
if (con != null)
con.close();
if (pStemt != null)
pStemt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return null;
}
public Integer update(){
String sql = "UPDATE "+this.tableNamePrefix+this.tableName +" SET ";
for (Map<String,String> e : this.putList){
String type = e.get("type");
switch (type) {
case "=":
sql += " "+e.get("key") +" "+ type +" ? ,";
break;
default:
break;
}
}
sql = sql.substring(0,sql.length() - 1);
sql += whereToSql();
if (isSetLimit && this.limitNum >-1) {
sql += " limit "+this.skipNum +","+this.limitNum +" ";
}
Connection con = null;
PreparedStatement pStemt = null;
try {
con = dataSource.getConnection();
pStemt = con.prepareStatement(sql);
Integer parameterIndex = 0;
for (Map<String,String> e : this.putList){
pStemt.setString(++parameterIndex, e.get("value"));
}
for (Map<String,String> e : this.whereList){
String type = e.get("type");
switch (type) {
case "in":
for (String retval: e.get("value").split(",")){
System.out.println(retval);
pStemt.setString(++parameterIndex, retval);
}
break;
default:
pStemt.setString(++parameterIndex, e.get("value"));
break;
}
}
Integer updateCount = pStemt.executeUpdate();
this.lastSql = pStemt;
return updateCount;
} catch (Exception throwables) {
this.lastSql = pStemt;
System.out.println("error "+ pStemt);
throwables.printStackTrace();
} finally {
try {
if (con != null)
con.close();
if (pStemt != null)
pStemt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return 0;
}
public Integer getId(){
return this.id;
}
public Boolean insert(){
String sql = "INSERT INTO "+this.tableNamePrefix+this.tableName +" ";
String field = "";
String value = "";
for (Map<String,String> e : this.putList){
String type = e.get("type");
switch (type) {
case "=":
field += " "+e.get("key") +" ,";
value += " ? ,";
break;
default:
break;
}
}
sql = sql.substring(0,sql.length() - 1);
sql += " ( "+field.substring(0,field.length() - 1)+" ) ";
sql += " VALUES ( "+value.substring(0,value.length() - 1)+" ) ";
sql += whereToSql();
Connection con = null;
PreparedStatement pStemt = null;
try {
con = dataSource.getConnection();
pStemt = con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
Integer parameterIndex = 0;
for (Map<String,String> e : this.putList){
pStemt.setString(++parameterIndex, e.get("value"));
}
Integer updateCount = pStemt.executeUpdate();
System.out.println(pStemt.getGeneratedKeys());
this.lastSql = pStemt;
ResultSet rs = pStemt.getGeneratedKeys();
if(rs.next()) {
this.id = rs.getInt(1);
}
return updateCount > 0;
} catch (Exception throwables) {
this.lastSql = pStemt;
System.out.println("error "+ pStemt);
throwables.printStackTrace();
} finally {
try {
if (con != null)
con.close();
if (pStemt != null)
pStemt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return false;
}
public Boolean delete(){
String sql = "DELETE FROM "+this.tableNamePrefix+this.tableName ;
sql += whereToSql();
if (isSetLimit && this.limitNum >-1) {
sql += " limit "+this.skipNum +","+this.limitNum +" ";
}
Connection con = null;
PreparedStatement pStemt = null;
try {
con = dataSource.getConnection();
pStemt = con.prepareStatement(sql);
Integer parameterIndex = 0;
for (Map<String,String> e : this.putList){
pStemt.setString(++parameterIndex, e.get("value"));
}
for (Map<String,String> e : this.whereList){
String type = e.get("type");
switch (type) {
case "in":
for (String retval: e.get("value").split(",")){
System.out.println(retval);
pStemt.setString(++parameterIndex, retval);
}
break;
default:
pStemt.setString(++parameterIndex, e.get("value"));
break;
}
}
Integer updateCount = pStemt.executeUpdate();
this.lastSql = pStemt;
return true;
} catch (Exception throwables) {
this.lastSql = pStemt;
System.out.println("error "+ pStemt);
throwables.printStackTrace();
} finally {
try {
if (con != null)
con.close();
if (pStemt != null)
pStemt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return false;
}
}