20200623

  • Post author:
  • Post category:其他


一、再次学习baseDao的使用;

1.get

  • public abstract Object get(Class<?> cls, Number id);

  • 获取指定id的数据
  • Hibernate会确认一下该id对应的数据是否存在,首先在session缓存中查找,然后在二级缓存中查找,还没有就查询数据库,数据库中没有就返回null
ApproveOnline approveOnline = (ApproveOnline) baseDao.get(ApproveOnline.class, approveOnlineId);
IMaterial oMaterial = (IMaterial) baseDao.get(Material.class, mid);

2.sql

  • public abstract List<?> sql(Class<?> cls, String sql);

  • 根据sql,对数据进行操作(增删改查)

    DynamicSqlConfig config = DynamicSqlConfig.getConfigInfo("dynamicsql",
                "orup.xml", "getBureauUnits");
        List<IOrganization> organizations = new ArrayList<IOrganization>();
        List<?> list = baseDao.sql(Organization.class, config.getSqlStr());
  • public List<?> sql(String dbName, String sql, Map<String, Object> args);

  • 根据传入的参数拼接到sql,对数据进行操作(增删改查)
DynamicSqlConfig config = DynamicSqlConfig.getConfigInfo("dynamicsql","process.xml", "getProcessButtonPrivilege");
Map<String, Object> args = new HashMap<String, Object>();
        args.put("businessId", businessId);
		args.put("activityName", activityName);
List<?> list = baseDao.sql(config.getDbName(), config.getMixSqlStr(),args);
<config name="getProcessButtonPrivilege" dbname="model">
		<sql><![CDATA[
select t.propertyvalue  from TS_EXTENDPROPERTY t where tba.activityname = :activityName and tbp.ref_business_id = :businessId)) 
		]]></sql>
		<params>
			<param name="businessId" />
			<param name="activityName" />
		</params>
	</config>
	
  • public abstract List<?> sql(Class<?> cls, String sql, Map<String, Object> args);

  • 根据传入的参数拼接到sql,对数据进行操作(增删改查)
 DynamicSqlConfig config = DynamicSqlConfig.getConfigInfo("database.xml", "getProjectEndYKDEntity");
        Map<String, Object> args = new HashMap<String, Object>();
        List<ProjectEndInstance> projectEndInstances = (List<ProjectEndInstance>) baseDao.sql(ProjectEndInstance.class,
                config.getMixSqlStr(args.keySet(), args.keySet()) + "" + config.getEndsqlStr(), args);

3.SQLPageToType

  • public abstract <T> List<T> SQLPageToType(Class<T> cls, String sql, int pageNum, int pageSize, Map<String, Object> args);
  • 根据参数进行分页,操作数据
	DynamicSqlConfig config = DynamicSqlConfig.getConfigInfo("dynamicsql", "orup.xml","getSuspendAuthorityUser");
			Map<String, Object> argsMap = new HashMap<String, Object>();	
			argsMap.put("id", organization.getParentid());
			List<User> users = baseDao.SQLPageToType(User.class, config.getMixSqlStr(), 0, Integer.MAX_VALUE,argsMap );

4.sqlAsMap

  • public List<Map<String, Object>> sqlAsMap(String string, String sql, Integer page, Integer pageSize);

DynamicSqlConfig config = DynamicSqlConfig.getConfigInfo("dynamicsql/project.xml", "getArchiveProjectList");
String sql = config.getMixSqlStr() + sqlFilter + config.getEndsqlStr();
baseDao.sqlAsMap(config.getDbName(), sql, 1, 500);
  • public List<Map<String, Object>> sqlAsMap(String dbName, String sql, Integer pageNum, Integer pageSize, Map<String, Object> args);

 String tableName = "ld".equals(index) ? BDC_LD : BDC_ZH;
        String querySql = "select * from " + tableName + " where pid = " + projectId + " and bdcdyh like '%" + bdcdyh + "%' ";
        List<Map<String, Object>> resList = baseDao.sqlAsMap(DataBase.Instance, querySql, pageIndex, pageSize, null);

5.save

  • public abstract Object save(Object ser);

     IMaterial materialFile = new Material();
        materialFile.setAdddate(new Date());
        materialFile.setDescription("YKD_FILE");
        materialFile.setExtension(extension);
        materialFile.setMaterialSize(String.valueOf(fileSize));
        baseDao.save(materialFile);
  • public abstract <T> void saveAll(Class<T> cls, List entities);

  • saveAlll和for循环一起使用
   for (Map<String, Object> map : resList) {
                TysbhEntity tysbhEntity = new TysbhEntity();
                tysbhEntity.setGmtCreate(new Date());
                tysbhEntity.setPid(MapUtils.getInteger(map, "PID"));
                tysbhEntity.setSource(MapUtils.getInteger(map, "APPLYSOURCE"));
                tysbhEntity.setTysbh(MapUtils.getString(map, "TYSBH"));
                tysbhEntities.add(tysbhEntity);
            }
            baseDao.saveAll(TysbhEntity.class, tysbhEntities);
  • public abstract void saveOrUpdate(Object ser);

    IProjectRelation projectRelation = projectRelationSupport
                    .getProjectRelation(projectId);
            projectRelation.setHeadprojectid(headProjectId);
            baseDao.saveOrUpdate(projectRelation);

6.criteria

  • 按条件进行查询

  • public abstract List<?> criteria(Class<?> cls, List<Criterion> criterions, int pageNum, int pageSize);

   List<Criterion> criterion = new ArrayList<Criterion>();
        if (StringUtils.isNotEmpty(queryFilter)) {
            criterion.add(Restrictions.like("businessName", queryFilter, MatchMode.ANYWHERE));
            criterion.add(Restrictions.like("reportType", queryFilter, MatchMode.ANYWHERE));
        }
        List<WordReportConfig> configList = (List<WordReportConfig>) baseDao.criteria(WordReportConfig.class, criterion, pageIndex, pageSize);
  • public abstract List<?> criteria(Class<?> cls, List<Criterion> criterions, Order order, int pageNum, int pageSize);

        List<Criterion> cs = new ArrayList<Criterion>();
        cs.add(Restrictions.in("businessId", bizIds));
        cs.add(Restrictions.ne("syncGt", 1));
        List<ProjectEndInstance> resList = (List<ProjectEndInstance>)
                baseDao.criteria(ProjectEndInstance.class, cs,
                        Order.desc("updateTime"),
                        1,
                        15);
  • public abstract List<?> criteria(Class<?> cls, List<Criterion> criterions, Order order);

List<Criterion> criterions = new ArrayList<Criterion>();
		criterions.add(Restrictions.eq("id", id));
List<?> sysMessageLst = baseDao.criteria(MessageItem.class,criterions,Order.desc("recodeDate"));

7.count

  • 根据条件查询结果总数

  • public abstract int count(Class<?> cls, List<Criterion> criterions);

        List<Criterion> criterions = new ArrayList<Criterion>();
        criterions.add(Restrictions.ne("state", 0));
        int count=baseDao.count(HzArchiveProject.class, criterions);

二、DataEngine(数据引擎)对项目数据的存取、修改

数据库引擎是用于

存储、处理和保护数据

的核心服务。利用数据库引擎可控制访问权限并快速处理事务,从而满足企业内大多数需要处理大量数据的应用程序的要求。

1.保存数据

  • public void saveData() throws Exception;



版权声明:本文为qq_41629684原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。