说明
数据库操作,数据库操作助码支持两种,一种是使用实体,第二种是使用原生sql查询
使用实体方式
只要软件中建立了表实体,那么就自动会有实体的三层结构,主要使用 service,和 entity
需要导入相关包:
import com.zhumadev.startzhuma.iservice.IDemoSingleTableService;
import com.zhumadev.startzhuma.entity.DemoSingleTable;
注入service对象
@IServiceAutowired
private IDemoSingleTableService demoSingleTableService;
示例中试用到的方法
createData 插入实体对象
modifyData 更新实体对象
selectByIdWithFill 根据主键查询实体对象 查询外会填充实体关联信息
selectListKeyEqValue 根据字段名称和字段值查询数据列表
deleteData 根据主键删除实体对象,添加删除消息
更多方法参考 ZmServiceBase 服务(Service)的基类的说明
示例
//实体操作测试public ZmHash TestEntity(){ZmHash log=new ZmHash();ZmReturnMsg msg=new ZmReturnMsg();DemoSingleTable data=new DemoSingleTable();data.TableName = "实体测试";data.TableNum = 5;//实体添加数据this.demoSingleTableService.createData(data,msg);int newAddId=data.TableId;log.Add("添加了一条数据,数据id为:",newAddId);//查询刚刚添加的数据DemoSingleTable selectdata=this.demoSingleTableService.selectByIdWithFill(newAddId,true,true);log.Add("查询刚刚添加的数据TableName",selectdata.TableName );//修改数据名称为 实体测试修改selectdata.TableName= "实体测试修改";this.demoSingleTableService.modifyData(selectdata,msg);//重新查询selectdata=this.demoSingleTableService.selectByIdWithFill(newAddId,true,true);log.Add("查询修改后的数据的数据TableName",selectdata.TableName );Listlist = this.demoSingleTableService.selectListKeyEqValue("TableId",newAddId); log.Add("删除前数据条数",list.GetCount());//删除刚刚添加的数据this.demoSingleTableService.deleteData(newAddId,msg);list = this.demoSingleTableService.selectListKeyEqValue("TableId",newAddId);log.Add("删除后数据条数",list.GetCount());return log;}
使用原生sql查询 弱类型方式
需要先找到数据库查询对象,在控制器、服务层、作业等等地方都可以通过下面语句找到数据库查询对象
BaseProvider curDb = base.GetCurDb();
示例中试用到的方法
InsertIntIdentityByParamList 根据sql参数列表插入数据并返回自增列id整型,sql参数名称就是列(col)名称
UpdateByConditions 根据参数列表更新符合条件列表的数据
SelectHashWhere 根据传入表数据和where条件生成并执行sql语句,并返回hash结构
SelectHashListWhere 根据传入表数据和where条件生成并执行sql语句,并返回hashlist结构
Delete 删除符合条件列表的数据
更多方法参考 BaseProvider说明
本方式可以通过可视化码流/微流的方式生成
示例
//数据库弱类型操作测试public ZmHash TestDbProvide(){ZmHash log=new ZmHash();BaseProvider curDb = base.GetCurDb();ZmReturnMsg msg=new ZmReturnMsg();ZmHash data=new ZmHash();ZmSqlParamList editParamList=new ZmSqlParamList();editParamList.AddStringParam("table_name", "弱类型测试",50);editParamList.AddIntParam("table_num", 5);//弱类型添加数据int newAddId=curDb.InsertIntIdentityByParamList("demo_single_table",editParamList);log.Add("添加了一条数据,数据id为:",newAddId);//查询刚刚添加的数据ZmHash selectdata= curDb.SelectHashWhere("demo_single_table", " table_id="+newAddId, "*");log.Add("查询刚刚添加的数据TableName",selectdata.GetStringValue("table_name") );//修改数据名称为 实体测试修改ZmQueryConditionList queryConditions = new ZmQueryConditionList();queryConditions.Add("table_id", "Equal", newAddId, false);editParamList=new ZmSqlParamList();editParamList.AddStringParam("table_name", "弱类型测试修改",50);curDb.UpdateByConditions("demo_single_table", editParamList, queryConditions);//重新查询selectdata= curDb.SelectHashWhere("demo_single_table", " table_id="+newAddId, "*");log.Add("查询修改后的数据的数据TableName",selectdata.GetStringValue("table_name") );ZmHashList list = curDb.SelectHashListWhere("demo_single_table", " table_id="+newAddId, "*");log.Add("删除前数据条数",list.GetCount());//删除刚刚添加的数据curDb.Delete("demo_single_table",queryConditions);list = curDb.SelectHashListWhere("demo_single_table", " table_id="+newAddId, "*");log.Add("删除后数据条数",list.GetCount());return log;}
使用存储过程示例
BaseProvider curDb = base.GetCurDb();if(curDb.isSupportProcedure()){//判断当前数据库提供者是否支持存储过程ZmSqlParamList paramlist = new ZmSqlParamList();paramlist.AddIntParam("id", 0);ZmSqlParam countParam = paramlist.AddIntParam("count", 2);//inout 参数的用法countParam.setInOutTypeInOut();ZmSqlParam ocParam = paramlist.AddIntParam("oc", 0);// out参数用法ocParam.setInOutTypeOut();//调用存储过程并返回ZmHashListZmHashList hashlist = curDb.ExecProcHashListParams("my_test_proc", paramlist);//参数返回以后的类型转换int newCount = ZmUtil.toInt(countParam.getParamValue());int newoc =ZmUtil.toInt(ocParam.getParamValue());}else{//对于不支持存储过程的数据处理}
示例视频包含的内容
使用实体方式的操作数据库
使用数据库原生sql方式的操作
