Java 数据库数据查询

在Java中对数据库数据进行查询

1,使用commonApiDao

当要对两个或者多个表进行联查的时候,此方法比较繁杂,不建议使用

//将数组转换成list集合
List<String> cliniids = Arrays.asList(paramMap.get("ids").split(","));

//提取unitmapEntityList集合中的部分数据
List<String> unitCodeList = unitmapEntityList.stream().collect(Collectors.mapping(TableEntity::getUnitcode, Collectors.toList()));
                
List<TabletEntity> eresultlist = commonApiDao.getScrollData(TableEntity.class,
                " itemCode=?0 and clinicItemCode=?1 and unitcode in (?2) and cfgcomplete=0 ", 
                new Object[]{paraMap.get("itemCode"), paraMap.get("clinicItemCode"), unitCodeList},
                new LinkedHashMap<>()).getResultlist();
//常用查询
TabletEntity tabletEntity= commonApiDao.find(TabletEntity.class,param.get("id"));
//注意:条件参数从0开始【?0】,否则报错

commonApiDao.batchInsert(eresultlist);
commonApiDao.batchUpdate(eresultlist); 
//注意:将eresultlist中的集合数据批量操作

2,调用dao层函数**

//controller
 List<String> pkgIdList = clinicallist.stream().collect(Collectors.mapping(TableEntity::getId, Collectors.toList()));
 List<TableEntity>  pkgmaplist=tableEntityDao.findByPkgIds(pkgIdList);
 
//dao
 @Query("select s from TableEntity s where s.pkgId in (?1)")
 List<TableEntity> findByPkgIds(List<String> pkgId);
 注意:条件参数从1开始【?1

3,使用jdbcTemplate

3.1 拼接sql


 String sql = "select ID,pkgId,pkgName,pkgCode from table where 1=1 ";
            if(!StringUtil.isEmptyOrLength0(paramMap.get("unitid"))){
                sql+=" and unitid='"+paramMap.get("unitid")+"'";
            }
            if(!StringUtil.isEmptyOrLength0(paramMap.get("clinicalname"))){
                sql+=" and pkgName like '%"+paramMap.get("clinicalname")+"%'";
            }

            sql+=" order by pkgCode asc";
            //简单的查询
            List<Map<String, Object>> list=jdbcTemplate.queryForList(sql);
            

3.2 jdbcTemplate 设置参数的查询


String tmpsqlks = " select code from table where 1=1 and levelcode2 = ? ";

//返回List<String>类型
List<String> kscodeList = jdbcTemplate.queryForList(tmpsqlks, new Object[]{paraMap.get("unitcode")}, String.class);


//返回List<YgZixunEntity>类型
List<YgZixunEntity> ygZixunOrderinfoEntityListTmp1 = 
			jdbcTemplate.query( "select *  from table where (publishtime>=? and publishtime<=? and zfstatus in(?))",
            new Object[]{timestampSt,timestampEnd,code},
            new BeanPropertyRowMapper<>(YgZixunEntity.class));

3.3 NamedParameterJdbcTemplate 设置参数的查询

方式一:

//需要先载入数据库的连接驱动,如果第一次启动项目的时候npJdbcTemplate=null,则重启项目即可
 @Autowired
 NamedParameterJdbcTemplate npJdbcTemplate;

//编写sql,配置参数,执行
 String deleteSql=" delete from table where itemCode=:itemCode and unitcode in (:unitcode)";
 HashMap<String, Object> paramMap = new HashMap<>();
 paramMap.put("unitcode",queryForObject);
 paramMap.put("itemCode",entity.getItemCode());
 npJdbcTemplate.update(deleteSql, paramMap);
 

方式二:

//编写sql,配置参数,执行
  String[] ids =paramMap.get("id").split(",");
  String sql="delete from table WHERE  id in (:id)";
  NamedParameterJdbcTemplate namedParameterJdbcTemplate=new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());//连接数据库
  MapSqlParameterSource argsMap=new MapSqlParameterSource();
  argsMap.addValue("id", Arrays.asList(ids)); //参数
  namedParameterJdbcTemplate.update(sql, argsMap);
  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值