用了FastChar框架以后,我觉得这个框架真的很好很方便,国内的优秀框架之一!比较突出的就是这个框架对数据的常规操作做了更好的封装,我们使用起来确实很方便!
1、save(String...checks)方法
此方法与save方法功能一致,当传入checks参数时,方法的作用就是:不存在则添加,存在则不添加!
这个功能就牛逼了,对于我们寻常的系统需求来说,经常会遇到先判断数据是否存在,不存在就添加,存在就不添加,然后开发写代码,至少两条sql语句处理,甚至创建两个方法来处理这一个需求,那么一个表如此,如果几十上百个表呢?可想而知工作的重复量就多么的大!
那么有了save(String...checks)方法,开发者只要传入checks参数就能非常方便的实现这个功能了!!以数据库为mysql,例子如下:
FcUserIntegralEntity integralEntity = FcUserIntegralEntity.newInstance();
integralEntity.set("userId", 5);
integralEntity.set("integralValue", 3);
integralEntity.set("integralType", FcUserIntegralEntity.IntegralTypeEnum.每日社交回复_进账.ordinal());
integralEntity.set("integralDetails", "每日评论一条动态!系统奖励积分!");
integralEntity.set("integralData", FastDateUtils.getDateString("yyyy-MM-dd"));
if (integralEntity.save("userId", "integralType", "integralData")) {//根据传入的userId、integralType和integralData属性值,组合条件判断是否存在
FcUserNoticeEntity userNoticeEntity = FcUserNoticeEntity.newInstance();
userNoticeEntity.set("noticeTitle", "系统奖励");
userNoticeEntity.set("noticeContent", "每日评论一条动态!系统奖励3个积分!");
userNoticeEntity.set("noticeType", 2);
userNoticeEntity.set("userId", 5);
userNoticeEntity.save();
}
如上代码,调用save方法时传入了userId、integralType和integralData属性,那么FastChar会根据此属性自动生成insert语句并把传入的属性条件值组合成where条件!注意:传入的checks属性值必须在当前对象实体类存在,否则会报错,最终生成的sql语句如下:
Sql :insert into fc_user_integral (integralData,integralDateTime,integralDetails,integralType,integralValue,userId) select ?,?,?,?,?,? from dual where not exists (select userId,integralType,integralData from fc_user_integral where 1=1 and userId = ? and integralType = ? and integralData = ? )
Params :[2019-11-12, 2019-11-12 09:21:54, 每日评论一条动态!系统奖励积分!, 4, 3, 5, 5, 4, 2019-11-12]
Total :0.589 seconds
Time :2019-11-12 09:21:55:064
Sql :insert into fc_user_notice (noticeContent,noticeDateTime,noticeState,noticeTitle,noticeType,userId) values (?,?,?,?,?,?)
Params :[每日评论一条动态!系统奖励3个积分!, 2019-11-12 09:21:55, 0, 系统奖励, 2, 5]
Total :0.007 seconds
Time :2019-11-12 09:21:55:072
2、delete(String...checks)方法
此方法与delete方法功能一致,当传入checks参数时,则以传入的checks属性为where条件进行删除数据!
具体使用与上述的save(String...checks)方法类似,此处就不在赘述!
3、update(String...checks)方法
此方法与update方法功能一致,当传入checks参数时,则以传入的checks属性为where条件进行修改数据!
具体使用与上述的save(String...checks)方法类似,此处就不在赘述!
4、push(String...checks)方法
此方法的是save(String...checks)方法和update(String...checks)方法的组合方法,
作用是:不存在则添加,存在则修改!
这个功能也是FastChar封装的比较牛逼方便的方法之一!在数据操作上也是比较频繁使用的方法!!
具体使用与上述的save(String...checks)方法类似,此处就不在赘述!