本文共 4800 字,大约阅读时间需要 16 分钟。
前提条件&产生问题原因:
1、由于数据量比较大,又没有使用hadoop等方式处理数据。所以使用分表的形式。分表的看另一篇文章。
2、然后,需求变了,卧槽~~~需求又变了!!!不用分表了,需要把所有的分表数据,插入到一张表中。当然,这所有表的字段要求一致。
///稍等,待会再写,先写一篇,如果不用分布式,处理大量数据的问题。
//回来了写完了,
package com.tujia.ecd.table;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.PreparedStatementCreator;import org.springframework.jdbc.core.RowMapper;import com.tujia.ecd.entity.BabyProjects;public class ExaminationRemind { //private JdbcTemplate localTemplate; private JdbcTemplate onlineTemplate; @Before public void setup(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //localTemplate = (JdbcTemplate) context.getBean("ecdItemRecordSlaverTemplate"); onlineTemplate = (JdbcTemplate) context.getBean("ecdItemRecordSlaverTemplate"); } @Test public void examinationRemind(){ int tableNumByDocid = 512; String tableNameByDocid = "baby_projects_odocid"; int tableNumByProjectid = 512; String tableNameByProjectid = "baby_projects_projectid"; int tableNumByUid = 512; String tableNameByUid = "baby_projects_uid"; //int record_visit = 512; //String record_visit_uid = "record_visit_uid"; List恩,就是上面的代码,很多东西,都跟我写的另一篇,分表,重复了,所以,这里我就不多说了,偷懒一下~~。showList = new ArrayList (); List showAllBabyProjectByDocid = showAllBabyProject(tableNameByDocid,tableNumByDocid); List showAllBabyProjectByProjectid = showAllBabyProject(tableNameByProjectid,tableNumByProjectid); List showAllBabyProjectByUid = showAllBabyProject(tableNameByUid,tableNumByUid); //List showAllrecord_visit = showAllBabyProject(record_visit_uid,record_visit); showList.addAll(showAllBabyProjectByDocid);//35 showList.addAll(showAllBabyProjectByProjectid);//1121 showList.addAll(showAllBabyProjectByUid);//1121 System.out.println("查询出的数量是:"+showAllBabyProjectByUid.size()+"条"); //List showAllRepeat = removeRepeat(showList); //System.out.println("去掉重复后的数量:"+showAllRepeat.size()+"条"); addAll(showAllBabyProjectByProjectid); } public List showAllBabyProject(final String tableName,final int tableNum){ List showList = new ArrayList (); for(int i=0;i oneTable = onlineTemplate.query(sql, new RowMapper (){ @Override public BabyProjects mapRow(ResultSet rs, int num) throws SQLException { BabyProjects babyProject = new BabyProjects(); babyProject.setProjectprice(rs.getString("projectprice")); babyProject.setId(rs.getLong("id")); babyProject.setProjectid(rs.getLong("projectid")); babyProject.setProjectName(rs.getString("projectname")); babyProject.setOdocid(rs.getLong("odocid")); babyProject.setOtime(rs.getLong("otime")); babyProject.setUid(rs.getLong("uid")); babyProject.setRecordid(rs.getLong("recordid")); //babyProject.setDocName(rs.getString("")); babyProject.setTreatid(rs.getLong("treatid")); babyProject.setHid(rs.getInt("hid")); //babyProject.setProjectCode(rs.getString("")); return babyProject; } }); showList.addAll(oneTable); } return showList; } public void addAll(final List listAll){ for(final BabyProjects babyProject : listAll){ final String sql = "insert into baby_projects (projectprice,id,projectid,projectname,odocid,otime,uid,recordid,treatid,hid,status) values(?,?,?,?,?,?,?,?,?,?,1)"; onlineTemplate.update(new PreparedStatementCreator() { @Override public java.sql.PreparedStatement createPreparedStatement( java.sql.Connection con) throws SQLException { java.sql.PreparedStatement ps = con.prepareStatement(sql); int i = 0; ps.setString(++i, babyProject.getProjectprice()); ps.setLong(++i, babyProject.getId()); ps.setLong(++i, babyProject.getProjectid()); ps.setString(++i, babyProject.getProjectName()); ps.setLong(++i, babyProject.getOdocid()); ps.setLong(++i, babyProject.getOtime()); ps.setLong(++i, babyProject.getUid()); ps.setLong(++i, babyProject.getRecordid()); ps.setLong(++i, babyProject.getTreatid()); ps.setLong(++i, babyProject.getHid()); return ps; } }); } } public List removeRepeat(final List listAll){ List showList = new ArrayList (); Set h = new HashSet (listAll); showList.addAll(h); return showList; } /* * public static void removeDuplicate(List list) { HashSet h = new HashSet(list); list.clear(); list.addAll(h); System.out.println(list); } * * public void addAll(List listAll){ for(BabyProjects babyProject : listAll){ templateSqlPrepare tsp = (babyProject, "baby_projects"); localTemplate.update(tsp.getPreparedStatementCreator(tsp.getSqlForInsert())); } }*/}
: p
转载地址:http://joolo.baihongyu.com/