<a name="imPZx"></a>
Foxnic-SQL (4) —— 增删改查(CRUD)
<a name="iL7RJ"></a>
概述
通过Foxnic-SQL 做 CRUD 是一件非常简单的事情,Foxnic-SQL 分别提供了 Insert、Update、Selelct、Delete 四个类型做语句构建。当然,通过对象化的方式构建语句要比直接写SQL字符串要复杂一些,但是好处也是显而易见的。<br />本文中的示例代码均可在 https://gitee.com/LeeFJ/foxnic-samples 项目中找到。<br />废话不多说,直接上代码:
1import com.github.foxnic.commons.busi.id.IDGenerator; 2import com.github.foxnic.dao.data.Rcd; 3import com.github.foxnic.sql.expr.Delete; 4import com.github.foxnic.sql.expr.Insert; 5import com.github.foxnic.sql.expr.Select; 6import com.github.foxnic.sql.expr.Update; 7import com.leefj.foxnic.sql.demo.config.DBInstance; 8 9import java.util.Date; 10 11public class CRUDBySQLDemo { 12 13 public static void main(String[] args) { 14 // 插入数据 15 String id=insertAddress("137771041252"); 16 System.out.println("addressId(Insert) = "+id); 17 // 按ID查询数据 18 Rcd address=queryAddress(id); 19 if(address!=null) { 20 System.out.println(address.toJSONObject()); 21 } 22 // 更新 23 if(id!=null) { 24 id=updateAddress(id,"13852562523"); 25 System.out.println("addressId(Update) = "+id); 26 } 27 // 删除 28 if(id!=null) { 29 id=deleteAddress(id); 30 System.out.println("addressId(Delete) = "+id); 31 } 32 } 33 34 /** 35 * 插入数据 36 * */ 37 public static String insertAddress(String phone) { 38 // 创建语句对象 39 Insert insert=new Insert("example_address"); 40 String id= IDGenerator.getSnowflakeIdString(); 41 // 设置值 42 insert.set("id",id) 43 .set("name","leefj") 44 // 如果是 null 则不连入SQL语句 45 .setIf("phone_number",phone) 46 .set("address","宁波") 47 .set("region_type","国内") 48 .set("create_time",new Date()) 49 // 设置数据库表达式 50 .setExpr("update_time","now()"); 51 // 输出语句 52 System.out.println(insert.getSQL()); 53 // 执行语句 54 Integer suc=DBInstance.DEFAULT.dao().execute(insert); 55 // 如果执行成功,返回ID,否则返回 null 56 if(suc==1) { 57 return id; 58 } else { 59 return null; 60 } 61 } 62 63 /** 64 * 查询 65 * */ 66 public static Rcd queryAddress(String id) { 67 // 创建语句对象 68 Select select=new Select("example_address"); 69 // 设置值 70 select.where().and("id=?",id); 71 // 输出语句 72 System.out.println(select.getSQL()); 73 // 执行语句 74 Rcd address=DBInstance.DEFAULT.dao().queryRecord(select); 75 // 如果执行成功,返回记录对象,否则返回 null 76 return address; 77 } 78 79 /** 80 * 更新 81 * */ 82 public static String updateAddress(String id,String phone) { 83 // 创建语句对象 84 Update update=new Update("example_address"); 85 // 设置值 86 update.setIf("phone_number",phone) 87 // 设置数据库表达式 88 .setExpr("update_time","now()") 89 .set("update_by","110") 90 .where().and("id=?",id); 91 // 输出语句 92 System.out.println(update.getSQL()); 93 // 执行语句 94 Integer suc=DBInstance.DEFAULT.dao().execute(update); 95 // 如果执行成功,返回ID,否则返回 null 96 if(suc==1) { 97 return id; 98 } else { 99 return null; 100 } 101 } 102 103 /** 104 * 删除 105 * */ 106 public static String deleteAddress(String id) { 107 // 创建语句对象 108 Delete delete=new Delete("example_address"); 109 // 设置条件 110 delete.where().and("id=?",id); 111 // 输出语句 112 System.out.println(delete.getSQL()); 113 // 执行语句 114 Integer suc=DBInstance.DEFAULT.dao().execute(delete); 115 // 如果执行成功,返回ID,否则返回 null 116 if(suc==1) { 117 return id; 118 } else { 119 return null; 120 } 121 } 122}
<a name="S5YrU"></a>
Select 语句
Select 语句是比较常用的语句,Foxnic-SQL 对于 Select 语句的构建也是比较简单的。以下是 Select 语句构建与使用的一个简单的例子,这个例子几乎把 Select 语句的子句对象都用到了。
1/** 2* Select 语句的使用 3* */ 4public static void demo1(String keyword) { 5 // 创建语句对象 6 Select select=new Select(); 7// from 子句 8select.from("sys_role") 9 // 选取字段 10 .select("id").select("name") 11 .selects("code","create_by") 12 // 查询条件 13 .where("id like ?","%"+keyword+"%").andEquals("deleted",0) 14 // 排序 15 .orderBy().ascNL("id") 16 // 从 order by 子句回到顶层 Select 语句 17 .top() 18 // 指定 group by 语句 19 .groupBy().by("id","name","code","create_by") 20 // 指定 having 子句 21 .having().and("count(id)>?",0); 22 23System.out.println(select.getSQL()); 24// 输出: SELECT id , name , code , create_by FROM sys_role WHERE id like '%10%' AND deleted = 0 GROUP BY id HAVING count(id)> 0 ORDER BY ifnull( id ,1) -1 asc, id ASC 25 26// 使用默认 DAO 进行查询 27RcdSet rs=DBInstance.DEFAULT.dao().query(select); 28 29// 遍历与输出 30for (Rcd r : rs) { 31 System.out.println(r.toJSONObject()); 32} 33 34}
<a name="J5D2y"></a>
小结
本节主要是通过代码的方式,让大家快速了解 Foxnic-SQL 是如何通过语句对象完成CRUD基本操作的,因为这些语句对象是基于 Expr 的,所以 Expr 所具备的特性它们否具备,这些特性是直接用字符串拼接SQL语句所不具备的。 <a name="VZBg5"></a>
相关项目
https://gitee.com/LeeFJ/foxnic<br /> https://gitee.com/LeeFJ/foxnic-web<br /> https://gitee.com/lank/eam<br /> https://gitee.com/LeeFJ/foxnic-samples
