|
|
@@ -0,0 +1,1508 @@
|
|
|
+package com.pub.jdbc;
|
|
|
+
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.DatabaseMetaData;
|
|
|
+import java.sql.Date;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.dao.DataAccessException;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.jdbc.core.PreparedStatementCallback;
|
|
|
+import org.springframework.jdbc.core.RowMapper;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import com.admin.user.UserVO;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.pub.env.Environment;
|
|
|
+import com.pub.exception.BusinessException;
|
|
|
+import com.pub.field.Fields;
|
|
|
+import com.pub.log.Log;
|
|
|
+import com.pub.page.PageRecord;
|
|
|
+import com.pub.system.SystemCache;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class BaseDAO {
|
|
|
+ @Autowired
|
|
|
+ private JdbcTemplate jdbcTemplate = null;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 除了null,有值就更新
|
|
|
+ * @param vo
|
|
|
+ */
|
|
|
+ public void update(SuperVO vo) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (!StringUtils.isEmpty(vo.getPrimaryKey())) {
|
|
|
+ vo.setAttribute(Fields.UPDATE_TIME, new Timestamp(System.currentTimeMillis()));
|
|
|
+ if(Environment.getUser() != null) {
|
|
|
+ vo.setAttribute(Fields.UPDATOR, Environment.getUser().getId());
|
|
|
+ }
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getUpdateValidNames(vo, types, Lists.newArrayList());
|
|
|
+ String updateSQL = getUpdateSQL(tableName, fieldNames, vo);
|
|
|
+ jdbcTemplate.execute(updateSQL, new PreparedStatementCallback<Object>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object doInPreparedStatement(PreparedStatement prepare)
|
|
|
+ throws SQLException, DataAccessException {
|
|
|
+ BaseSQLParam sqlParam = getUpdateSQLParam(vo, fieldNames);
|
|
|
+ commitTransaction(prepare, sqlParam, types, fieldNames);
|
|
|
+ prepare.executeUpdate();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("更新数据异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据字段更新,设置了null也会更新
|
|
|
+ * @param vo
|
|
|
+ * @param updateFieldList
|
|
|
+ */
|
|
|
+ public void update(SuperVO vo, List<String> updateFieldList) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (!StringUtils.isEmpty(vo.getPrimaryKey())) {
|
|
|
+ vo.setAttribute(Fields.UPDATE_TIME, new Timestamp(System.currentTimeMillis()));
|
|
|
+ if(Environment.getUser() != null) {
|
|
|
+ vo.setAttribute(Fields.UPDATOR, Environment.getUser().getId());
|
|
|
+ }
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getUpdateValidNames(vo, types, updateFieldList);
|
|
|
+ String updateSQL = getUpdateSQL(tableName, fieldNames, vo);
|
|
|
+ jdbcTemplate.execute(updateSQL, new PreparedStatementCallback<Object>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object doInPreparedStatement(PreparedStatement prepare)
|
|
|
+ throws SQLException, DataAccessException {
|
|
|
+ BaseSQLParam sqlParam = getUpdateSQLParam(vo, fieldNames);
|
|
|
+ commitTransaction(prepare, sqlParam, types, fieldNames);
|
|
|
+ prepare.executeUpdate();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("更新数据异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据字段更新,设置了null也会更新
|
|
|
+ * @param vo
|
|
|
+ * @param updateFieldList
|
|
|
+ */
|
|
|
+ public void updateAll(SuperVO vo, List<String> updateFieldList) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (true) {
|
|
|
+ vo.setAttribute(Fields.UPDATE_TIME, new Timestamp(System.currentTimeMillis()));
|
|
|
+
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getUpdateValidNames(vo, types, updateFieldList);
|
|
|
+ String updateSQL = getUpdateSQL(tableName, fieldNames, vo);
|
|
|
+ jdbcTemplate.execute(updateSQL, new PreparedStatementCallback<Object>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object doInPreparedStatement(PreparedStatement prepare)
|
|
|
+ throws SQLException, DataAccessException {
|
|
|
+ BaseSQLParam sqlParam = getUpdateSQLParam(vo, fieldNames);
|
|
|
+ commitTransaction(prepare, sqlParam, types, fieldNames);
|
|
|
+ prepare.executeUpdate();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("更新数据异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 除了null,有值就更新
|
|
|
+ * @param vo
|
|
|
+ * @param updateFieldList
|
|
|
+ */
|
|
|
+ public void updateList(List<? extends SuperVO> voList) {
|
|
|
+ try {
|
|
|
+ if (voList != null && voList.size() > 0) {
|
|
|
+ String tableName = voList.get(0).getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getUpdateValidNames(voList.get(0), types, Lists.newArrayList());
|
|
|
+ String updateSQL = getUpdateSQL(tableName, fieldNames, voList.get(0));
|
|
|
+ jdbcTemplate.execute(updateSQL, new PreparedStatementCallback<Object>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object doInPreparedStatement(PreparedStatement prepare)
|
|
|
+ throws SQLException, DataAccessException {
|
|
|
+ for (SuperVO vo : voList) {
|
|
|
+ vo.setAttribute(Fields.UPDATE_TIME, new Timestamp(System.currentTimeMillis()));
|
|
|
+ if(Environment.getUser() != null) {
|
|
|
+ vo.setAttribute(Fields.UPDATOR, Environment.getUser().getId());
|
|
|
+ }
|
|
|
+ if (!StringUtils.isEmpty(vo.getPrimaryKey())) {
|
|
|
+ BaseSQLParam sqlParam = getUpdateSQLParam(vo, fieldNames);
|
|
|
+ commitTransaction(prepare, sqlParam, types, fieldNames);
|
|
|
+ prepare.addBatch();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ prepare.executeBatch();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("更新数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据字段更新List,设置了null也会更新
|
|
|
+ * @param vo
|
|
|
+ * @param updateFieldList
|
|
|
+ */
|
|
|
+ public void updateList(List<? extends SuperVO> voList, List<String> updateFieldList) {
|
|
|
+ try {
|
|
|
+ if (voList != null && voList.size() > 0) {
|
|
|
+ String tableName = voList.get(0).getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ updateFieldList.add(Fields.UPDATE_TIME);
|
|
|
+ updateFieldList.add(Fields.UPDATOR);
|
|
|
+ String[] fieldNames = getUpdateValidNames(voList.get(0), types, updateFieldList);
|
|
|
+ String updateSQL = getUpdateSQL(tableName, fieldNames, voList.get(0));
|
|
|
+ jdbcTemplate.execute(updateSQL, new PreparedStatementCallback<Object>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object doInPreparedStatement(PreparedStatement prepare)
|
|
|
+ throws SQLException, DataAccessException {
|
|
|
+ for (SuperVO vo : voList) {
|
|
|
+ vo.setAttribute(Fields.UPDATE_TIME, new Timestamp(System.currentTimeMillis()));
|
|
|
+ if(Environment.getUser() != null) {
|
|
|
+ vo.setAttribute(Fields.UPDATOR, Environment.getUser().getId());
|
|
|
+ }
|
|
|
+ if (!StringUtils.isEmpty(vo.getPrimaryKey())) {
|
|
|
+ BaseSQLParam sqlParam = getUpdateSQLParam(vo, fieldNames);
|
|
|
+ commitTransaction(prepare, sqlParam, types, fieldNames);
|
|
|
+ prepare.addBatch();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ prepare.executeBatch();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("更新数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 除了null,有值就更新
|
|
|
+ * @param vo
|
|
|
+ * @param updateFieldList
|
|
|
+ */
|
|
|
+ public void updateArray(SuperVO[] vo) {
|
|
|
+ try {
|
|
|
+ updateList(Arrays.asList(vo));
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("更新数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据字段更新Array,设置了null也会更新
|
|
|
+ * @param vo
|
|
|
+ * @param updateFieldList
|
|
|
+ */
|
|
|
+ public void updateArray(SuperVO[] vo, List<String> updateFieldList) {
|
|
|
+ try {
|
|
|
+ updateList(Arrays.asList(vo), updateFieldList);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("更新数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据SQL进行更新
|
|
|
+ * @param sql
|
|
|
+ */
|
|
|
+ public void updateSQL(String sql) {
|
|
|
+ jdbcTemplate.update(sql);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 插入数据
|
|
|
+ * @param vo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String insert(SuperVO vo) {
|
|
|
+ try {
|
|
|
+ String pk = "";
|
|
|
+ if (StringUtils.isEmpty(vo.getPrimaryKey())) {
|
|
|
+ pk = UUID.randomUUID().toString();
|
|
|
+ } else {
|
|
|
+ pk = vo.getPrimaryKey();
|
|
|
+ }
|
|
|
+ vo.setPrimaryKey(pk);
|
|
|
+ if (vo.getAttribute(Fields.CREATE_TIME) == null) {
|
|
|
+ vo.setAttribute(Fields.CREATE_TIME, new Timestamp(System.currentTimeMillis()));
|
|
|
+ }
|
|
|
+ HashMap<String, Object> map = Environment.threadLocal.get();
|
|
|
+ if (map != null) {
|
|
|
+ UserVO person = (UserVO) map.get(Environment.USER);
|
|
|
+ if (StringUtils.isEmpty(vo.getAttribute(Fields.OPERATOR)) && person != null) {
|
|
|
+ vo.setAttribute(Fields.OPERATOR, person.getId());
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(vo.getAttribute(Fields.ORG)) && person != null) {
|
|
|
+ vo.setAttribute(Fields.ORG, person.getSszz_id());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ vo.setAttribute(Fields.DR, 0);
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getValidNames(vo, types);
|
|
|
+ String sql = getInsertSQL(tableName, fieldNames);
|
|
|
+ jdbcTemplate.execute(sql, new PreparedStatementCallback<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object doInPreparedStatement(PreparedStatement prepare)
|
|
|
+ throws SQLException, DataAccessException {
|
|
|
+ BaseSQLParam sqlParam = getSQLParam(vo, fieldNames);
|
|
|
+ commitTransaction(prepare, sqlParam, types, fieldNames);
|
|
|
+ prepare.executeUpdate();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ return pk;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("插入数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 插入数据(wy)
|
|
|
+ * @param vo
|
|
|
+ */
|
|
|
+ public void insertAll(SuperVO vo) {
|
|
|
+ try {
|
|
|
+ // 设置创建时间为当前时间
|
|
|
+ if (vo.getAttribute(Fields.CREATE_TIME) == null) {
|
|
|
+ vo.setAttribute(Fields.CREATE_TIME, new Timestamp(System.currentTimeMillis()));
|
|
|
+
|
|
|
+ }
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ // 主键递增
|
|
|
+ types.remove("ID");
|
|
|
+ String[] fieldNames = getValidNames(vo, types);
|
|
|
+ String sql = getInsertSQL(tableName, fieldNames);
|
|
|
+ jdbcTemplate.execute(sql, new PreparedStatementCallback<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object doInPreparedStatement(PreparedStatement prepare)
|
|
|
+ throws SQLException, DataAccessException {
|
|
|
+ BaseSQLParam sqlParam = getSQLParam(vo, fieldNames);
|
|
|
+ commitTransaction(prepare, sqlParam, types, fieldNames);
|
|
|
+ prepare.executeUpdate();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("插入数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 插入数组
|
|
|
+ * @param vo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String[] insertArray(SuperVO[] vos) {
|
|
|
+ return insertList(Arrays.asList(vos));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量插入List
|
|
|
+ * @param vos
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String[] insertList(List<? extends SuperVO> vos) {
|
|
|
+ List<String> pks = new ArrayList<String>();
|
|
|
+ try {
|
|
|
+ if (vos != null && vos.size() > 0) {
|
|
|
+ String tableName = vos.get(0).getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getValidNames(vos.get(0), types);
|
|
|
+ String sql = getInsertSQL(tableName, fieldNames);
|
|
|
+ jdbcTemplate.execute(sql, new PreparedStatementCallback<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object doInPreparedStatement(PreparedStatement prepare)
|
|
|
+ throws SQLException, DataAccessException {
|
|
|
+ for (SuperVO vo : vos) {
|
|
|
+ String pk = "";
|
|
|
+ if (StringUtils.isEmpty(vo.getPrimaryKey())) {
|
|
|
+ pk = UUID.randomUUID().toString();
|
|
|
+ } else {
|
|
|
+ pk = vo.getPrimaryKey();
|
|
|
+ }
|
|
|
+ vo.setPrimaryKey(pk);
|
|
|
+ if (vo.getAttribute(Fields.CREATE_TIME) == null) {
|
|
|
+ vo.setAttribute(Fields.CREATE_TIME, new Timestamp(System.currentTimeMillis()));
|
|
|
+ }
|
|
|
+ HashMap<String, Object> map = Environment.threadLocal.get();
|
|
|
+ if (map != null) {
|
|
|
+ UserVO person = (UserVO) map.get(Environment.USER);
|
|
|
+ if (StringUtils.isEmpty(vo.getAttribute(Fields.OPERATOR)) && person != null) {
|
|
|
+ vo.setAttribute(Fields.OPERATOR, person.getId());
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(vo.getAttribute(Fields.ORG)) && person != null) {
|
|
|
+ vo.setAttribute(Fields.ORG, person.getSszz_id());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ vo.setAttribute(Fields.DR, 0);
|
|
|
+ BaseSQLParam sqlParam = getSQLParam(vo, fieldNames);
|
|
|
+ commitTransaction(prepare, sqlParam, types, fieldNames);
|
|
|
+ prepare.addBatch();
|
|
|
+ pks.add(pk);
|
|
|
+ }
|
|
|
+ prepare.executeBatch();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("插入数据异常", e);
|
|
|
+ }
|
|
|
+ return pks.toArray(new String[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据条件进行查询所有数据
|
|
|
+ * @param <T>
|
|
|
+ * @param clazz
|
|
|
+ * @param condition
|
|
|
+ * @param sortSQL
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> List<T> queryByCondition(Class<? extends SuperVO> clazz, String condition, String sortSQL) {
|
|
|
+ try {
|
|
|
+ List<T> list = new ArrayList<T>();
|
|
|
+ SuperVO vo = clazz.newInstance();
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getValidNames(vo, types);
|
|
|
+ String sql = getQuerySQL(tableName, fieldNames, condition);
|
|
|
+
|
|
|
+ if (sql == null) {
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ sql = addSortSQL(sql, sortSQL);
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<T>() {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ @Override
|
|
|
+ public T mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ SuperVO rs2vo = null;
|
|
|
+ try {
|
|
|
+ rs2vo = (SuperVO) clazz.newInstance();
|
|
|
+ rs2vo(rs2vo, rs, fieldNames);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return (T) rs2vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ return list;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据条件进行查询所有数据(wy)
|
|
|
+ * @param <T>
|
|
|
+ * @param clazz
|
|
|
+ * @param condition
|
|
|
+ * @param sortSQL
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> List<T> queryAllByCondition(Class<? extends SuperVO> clazz, String condition, String sortSQL) {
|
|
|
+ try {
|
|
|
+ List<T> list = new ArrayList<T>();
|
|
|
+ SuperVO vo = clazz.newInstance();
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getValidNames(vo, types);
|
|
|
+ String sql = getQueryAllSQL(tableName, fieldNames, condition);
|
|
|
+ if (sql == null) {
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ sql = addSortSQL(sql, sortSQL);
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<T>() {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ @Override
|
|
|
+ public T mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ SuperVO rs2vo = null;
|
|
|
+ try {
|
|
|
+ rs2vo = (SuperVO) clazz.newInstance();
|
|
|
+ rs2vo(rs2vo, rs, fieldNames);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return (T) rs2vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ return list;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据条件进行查询所有数据不关心删除标记(wy)
|
|
|
+ * @param <T>
|
|
|
+ * @param clazz
|
|
|
+ * @param condition
|
|
|
+ * @param sortSQL
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> List<T> queryAllByConditionNocare(Class<? extends SuperVO> clazz, String condition, String sortSQL) {
|
|
|
+ try {
|
|
|
+ List<T> list = new ArrayList<T>();
|
|
|
+ SuperVO vo = clazz.newInstance();
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getValidNames(vo, types);
|
|
|
+ String sql = getQueryAllSQLNocare(tableName, fieldNames, condition);
|
|
|
+
|
|
|
+ if (sql == null) {
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ sql = addSortSQL(sql, sortSQL);
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<T>() {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ @Override
|
|
|
+ public T mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ SuperVO rs2vo = null;
|
|
|
+ try {
|
|
|
+ rs2vo = (SuperVO) clazz.newInstance();
|
|
|
+ rs2vo(rs2vo, rs, fieldNames);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return (T) rs2vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ return list;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据条件进行查询分页数据
|
|
|
+ * @param <T>
|
|
|
+ * @param clazz
|
|
|
+ * @param condition
|
|
|
+ * @param pageSize
|
|
|
+ * @param pageNum
|
|
|
+ * @param sortSQL
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> PageRecord<T> queryAllPaginateByCondition(Class<? extends SuperVO> clazz, String condition,
|
|
|
+ Integer pageSize, Integer pageNum, String sortSQL) {
|
|
|
+ PageRecord<T> page = new PageRecord<T>();
|
|
|
+ try {
|
|
|
+
|
|
|
+ List<T> list = new ArrayList<T>();
|
|
|
+ SuperVO vo = clazz.newInstance();
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getValidNames(vo, types);
|
|
|
+ String sql = getQueryAllSQL(tableName, fieldNames, condition);
|
|
|
+
|
|
|
+ if (sql == null || pageSize == null || pageNum == null) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+ String count_sql = sql;
|
|
|
+ sql = addSortSQL(sql, sortSQL);
|
|
|
+ Long total = jdbcTemplate.queryForObject("select count(*) from (" + count_sql + ") tt ", Long.class);
|
|
|
+ page.setTotal(total);
|
|
|
+ Integer totalPageNum;
|
|
|
+ if (total % pageSize == 0) {
|
|
|
+ totalPageNum = total.intValue() / pageSize;
|
|
|
+ } else {
|
|
|
+ totalPageNum = (total.intValue() / pageSize) + 1;
|
|
|
+ }
|
|
|
+ if (pageNum > totalPageNum) {
|
|
|
+ page.setOutTotalPageNum(true);
|
|
|
+ }
|
|
|
+ page.setTotalPageNum(totalPageNum);
|
|
|
+ page.setPageNum(pageNum);
|
|
|
+ page.setPageSize(pageSize);
|
|
|
+ sql = sql + " limit " + (pageNum - 1) * pageSize + "," + pageSize;
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<T>() {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ @Override
|
|
|
+ public T mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ SuperVO rs2vo = null;
|
|
|
+ try {
|
|
|
+ rs2vo = (SuperVO) clazz.newInstance();
|
|
|
+ rs2vo(rs2vo, rs, fieldNames);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return (T) rs2vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ page.setRows(list);
|
|
|
+ return page;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询数据异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据条件进行查询分页数据
|
|
|
+ * @param <T>
|
|
|
+ * @param clazz
|
|
|
+ * @param condition
|
|
|
+ * @param pageSize
|
|
|
+ * @param pageNum
|
|
|
+ * @param sortSQL
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> PageRecord<T> queryPaginateByCondition(Class<? extends SuperVO> clazz, String condition,
|
|
|
+ Integer pageSize, Integer pageNum, String sortSQL) {
|
|
|
+ PageRecord<T> page = new PageRecord<T>();
|
|
|
+ try {
|
|
|
+
|
|
|
+ List<T> list = new ArrayList<T>();
|
|
|
+ SuperVO vo = clazz.newInstance();
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getValidNames(vo, types);
|
|
|
+ String sql = getQuerySQL(tableName, fieldNames, condition);
|
|
|
+
|
|
|
+ if (sql == null || pageSize == null || pageNum == null) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+ String count_sql = sql;
|
|
|
+ sql = addSortSQL(sql, sortSQL);
|
|
|
+ Long total = jdbcTemplate.queryForObject("select count(*) from (" + count_sql + ") tt ", Long.class);
|
|
|
+ page.setTotal(total);
|
|
|
+ Integer totalPageNum;
|
|
|
+ if (total % pageSize == 0) {
|
|
|
+ totalPageNum = total.intValue() / pageSize;
|
|
|
+ } else {
|
|
|
+ totalPageNum = (total.intValue() / pageSize) + 1;
|
|
|
+ }
|
|
|
+ if (pageNum > totalPageNum) {
|
|
|
+ page.setOutTotalPageNum(true);
|
|
|
+ }
|
|
|
+ page.setTotalPageNum(totalPageNum);
|
|
|
+ page.setPageNum(pageNum);
|
|
|
+ page.setPageSize(pageSize);
|
|
|
+ sql = sql + " limit " + (pageNum - 1) * pageSize + "," + pageSize;
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<T>() {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ @Override
|
|
|
+ public T mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ SuperVO rs2vo = null;
|
|
|
+ try {
|
|
|
+ rs2vo = (SuperVO) clazz.newInstance();
|
|
|
+ rs2vo(rs2vo, rs, fieldNames);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return (T) rs2vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ page.setRows(list);
|
|
|
+ return page;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询数据异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据主键查询
|
|
|
+ * @param <T>
|
|
|
+ * @param clazz
|
|
|
+ * @param pk
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> T queryByPK(Class<? extends SuperVO> clazz, String pk) {
|
|
|
+ try {
|
|
|
+ List<SuperVO> list = new ArrayList<SuperVO>();
|
|
|
+ SuperVO vo = (SuperVO) clazz.newInstance();
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getValidNames(vo, types);
|
|
|
+ String sql = getQueryByPK(vo, tableName, fieldNames, pk);
|
|
|
+
|
|
|
+ if (sql == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<SuperVO>() {
|
|
|
+ @Override
|
|
|
+ public SuperVO mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ rs2vo(vo, rs, fieldNames);
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ if ((list != null) && (list.size() != 0)) {
|
|
|
+ return (T) list.get(0);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据键名查询
|
|
|
+ * @author wy
|
|
|
+ * @param <T>
|
|
|
+ * @param clazz
|
|
|
+ * @param pk
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> T queryByKey(Class<? extends SuperVO> clazz,String keyName, String k) {
|
|
|
+ try {
|
|
|
+ List<SuperVO> list = new ArrayList<SuperVO>();
|
|
|
+ SuperVO vo = (SuperVO) clazz.newInstance();
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getValidNames(vo, types);
|
|
|
+ String sql = getQueryByKey(vo, tableName, fieldNames, keyName, k);
|
|
|
+ if (sql == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<SuperVO>() {
|
|
|
+ @Override
|
|
|
+ public SuperVO mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ rs2vo(vo, rs, fieldNames);
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ if ((list != null) && (list.size() != 0)) {
|
|
|
+ return (T) list.get(0);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据SQL查询数据
|
|
|
+ * @param sql
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<Map<String, Object>> queryBySQL(String sql) {
|
|
|
+ List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<Map<String, Object>>() {
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ int columncount = rs.getMetaData().getColumnCount();
|
|
|
+ for (int i = 0; i < columncount; ++i) {
|
|
|
+ String name = rs.getMetaData().getColumnLabel(i + 1);
|
|
|
+ Object obj = rs.getObject(i + 1);
|
|
|
+ if (obj instanceof oracle.sql.TIMESTAMP) {
|
|
|
+ oracle.sql.TIMESTAMP timestamp = (oracle.sql.TIMESTAMP) obj;
|
|
|
+ obj = timestamp.timestampValue();
|
|
|
+ } else if (obj instanceof Date) {
|
|
|
+ Date date = (Date) obj;
|
|
|
+ obj = new java.util.Date(date.getTime());
|
|
|
+ }
|
|
|
+ map.put(name.toLowerCase(), obj);
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据SQL查询分页数据
|
|
|
+ * @param sql
|
|
|
+ * @param pageSize
|
|
|
+ * @param pageNum
|
|
|
+ * @param sortSQL
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public PageRecord<Map<String, Object>> queryPaginateBySQL(String sql, Integer pageSize, Integer pageNum,
|
|
|
+ String sortSQL) {
|
|
|
+ String count_sql = sql;
|
|
|
+ sql = addSortSQL(sql, sortSQL);
|
|
|
+ PageRecord<Map<String, Object>> page = new PageRecord<Map<String, Object>>();
|
|
|
+ Long total = jdbcTemplate.queryForObject("select count(*) from (" + count_sql + ") t1", Long.class);
|
|
|
+ page.setTotal(total);
|
|
|
+ Integer totalPageNum;
|
|
|
+ if (total % pageSize == 0) {
|
|
|
+ totalPageNum = total.intValue() / pageSize;
|
|
|
+ } else {
|
|
|
+ totalPageNum = (total.intValue() / pageSize) + 1;
|
|
|
+ }
|
|
|
+ if (pageNum > totalPageNum) {
|
|
|
+ page.setOutTotalPageNum(true);
|
|
|
+ }
|
|
|
+ page.setTotalPageNum(totalPageNum);
|
|
|
+ page.setPageNum(pageNum);
|
|
|
+ page.setPageSize(pageSize);
|
|
|
+ sql = sql + " limit " + (pageNum - 1) * pageSize + "," + pageSize;
|
|
|
+ List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<Map<String, Object>>() {
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ int columncount = rs.getMetaData().getColumnCount();
|
|
|
+ for (int i = 0; i < columncount; ++i) {
|
|
|
+ String name = rs.getMetaData().getColumnLabel(i + 1);
|
|
|
+ Object obj = rs.getObject(i + 1);
|
|
|
+ if (obj instanceof oracle.sql.TIMESTAMP) {
|
|
|
+ oracle.sql.TIMESTAMP timestamp = (oracle.sql.TIMESTAMP) obj;
|
|
|
+ obj = timestamp.timestampValue();
|
|
|
+ } else if (obj instanceof Date) {
|
|
|
+ Date date = (Date) obj;
|
|
|
+ obj = new java.util.Date(date.getTime());
|
|
|
+ }
|
|
|
+ map.put(name.toLowerCase(), obj);
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ page.setRows(list);
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据SQL查询所有数据
|
|
|
+ * @param <T>
|
|
|
+ * @param clazz
|
|
|
+ * @param sql
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> List<T> queryBySQL(Class<? extends SuperVO> clazz, String sql) {
|
|
|
+ List<T> list = new ArrayList<T>();
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<T>() {
|
|
|
+ @Override
|
|
|
+ public T mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ try {
|
|
|
+ SuperVO superVO = clazz.newInstance();
|
|
|
+ int columncount = rs.getMetaData().getColumnCount();
|
|
|
+ for (int i = 0; i < columncount; ++i) {
|
|
|
+ String name = rs.getMetaData().getColumnLabel(i + 1);
|
|
|
+ Object obj = rs.getObject(i + 1);
|
|
|
+ if (obj instanceof oracle.sql.TIMESTAMP) {
|
|
|
+ oracle.sql.TIMESTAMP timestamp = (oracle.sql.TIMESTAMP) obj;
|
|
|
+ obj = timestamp.timestampValue();
|
|
|
+ } else if (obj instanceof Date) {
|
|
|
+ Date date = (Date) obj;
|
|
|
+ obj = new java.util.Date(date.getTime());
|
|
|
+ }
|
|
|
+ superVO.setAttribute(name.toLowerCase(), obj);
|
|
|
+ }
|
|
|
+ return (T) superVO;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据SQL查询分页数据
|
|
|
+ * @param <T>
|
|
|
+ * @param clazz
|
|
|
+ * @param sql
|
|
|
+ * @param pageSize
|
|
|
+ * @param pageNum
|
|
|
+ * @param sortSQL
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> PageRecord<T> queryPaginateBySQL(Class<? extends SuperVO> clazz, String sql, Integer pageSize,
|
|
|
+ Integer pageNum, String sortSQL) {
|
|
|
+ String count_sql = sql;
|
|
|
+ sql = addSortSQL(sql, sortSQL);
|
|
|
+ PageRecord<T> page = new PageRecord<T>();
|
|
|
+ List<T> list = new ArrayList<T>();
|
|
|
+ if (StringUtils.isEmpty(sql) || StringUtils.isEmpty(pageSize) || StringUtils.isEmpty(pageNum)) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+ Long total = jdbcTemplate.queryForObject("select count(*) from (" + count_sql + ") t1", Long.class);
|
|
|
+ page.setTotal(total);
|
|
|
+ Integer totalPageNum;
|
|
|
+ if (total % pageSize == 0) {
|
|
|
+ totalPageNum = total.intValue() / pageSize;
|
|
|
+ } else {
|
|
|
+ totalPageNum = (total.intValue() / pageSize) + 1;
|
|
|
+ }
|
|
|
+ if (pageNum > totalPageNum) {
|
|
|
+ page.setOutTotalPageNum(true);
|
|
|
+ }
|
|
|
+ page.setTotalPageNum(totalPageNum);
|
|
|
+ page.setPageNum(pageNum);
|
|
|
+ page.setPageSize(pageSize);
|
|
|
+ sql = sql + " limit " + (pageNum - 1) * pageSize + "," + pageSize;
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<T>() {
|
|
|
+ @Override
|
|
|
+ public T mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ try {
|
|
|
+ SuperVO superVO = clazz.newInstance();
|
|
|
+ int columncount = rs.getMetaData().getColumnCount();
|
|
|
+ for (int i = 0; i < columncount; ++i) {
|
|
|
+ String name = rs.getMetaData().getColumnLabel(i + 1);
|
|
|
+ Object obj = rs.getObject(i + 1);
|
|
|
+ if (obj instanceof oracle.sql.TIMESTAMP) {
|
|
|
+ oracle.sql.TIMESTAMP timestamp = (oracle.sql.TIMESTAMP) obj;
|
|
|
+ obj = timestamp.timestampValue();
|
|
|
+ } else if (obj instanceof Date) {
|
|
|
+ Date date = (Date) obj;
|
|
|
+ obj = new java.util.Date(date.getTime());
|
|
|
+ }
|
|
|
+ superVO.setAttribute(name.toLowerCase(), obj);
|
|
|
+ }
|
|
|
+ return (T) superVO;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ page.setRows(list);
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据主键删除数据
|
|
|
+ * @param clazz
|
|
|
+ * @param pk
|
|
|
+ */
|
|
|
+ public void deleteByPK(Class<? extends SuperVO> clazz, String pk) {
|
|
|
+ try {
|
|
|
+ SuperVO vo = clazz.newInstance();
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ String pkname = vo.getPKFieldName();
|
|
|
+ HashMap<String, Object> map = Environment.threadLocal.get();
|
|
|
+ String del_sql = "";
|
|
|
+ if (map != null) {
|
|
|
+ UserVO person = (UserVO) map.get(Environment.USER);
|
|
|
+ if (person != null) {
|
|
|
+ del_sql = ",del_opr='" + person.getId() + "' ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String sql = "update " + tableName + " set dr=1,del_time=now()" + del_sql + " where " + pkname + "='" + pk
|
|
|
+ + "'";
|
|
|
+ jdbcTemplate.update(sql);
|
|
|
+ deleteRecursion(clazz, pk);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("删除数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据键名删除数据
|
|
|
+ * @param clazz
|
|
|
+ * @param keyName
|
|
|
+ * @param k
|
|
|
+ */
|
|
|
+ public void deleteByKey(Class<? extends SuperVO> clazz,String keyName,String k) {
|
|
|
+ try {
|
|
|
+ SuperVO vo = clazz.newInstance();
|
|
|
+ String tableName =vo.getTableName();
|
|
|
+ String sql = "update " + tableName + " set delete_tag=1,delete_time=now()" + " where " + keyName + "='" + k+ "'";
|
|
|
+ jdbcTemplate.update(sql);
|
|
|
+// deleteRecursion(clazz, k);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("删除数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归删除级联数据
|
|
|
+ * @param clazz
|
|
|
+ * @param pk
|
|
|
+ * @throws InstantiationException
|
|
|
+ * @throws IllegalAccessException
|
|
|
+ */
|
|
|
+ private void deleteRecursion(Class<? extends SuperVO> clazz, String pk)
|
|
|
+ throws InstantiationException, IllegalAccessException {
|
|
|
+ CascadeMetadata[] childs = clazz.newInstance().getChildCascadeMetadata();
|
|
|
+ if (childs != null) {
|
|
|
+ for (CascadeMetadata md : childs) {
|
|
|
+ List<? extends SuperVO> vos = queryByCondition(md.getClazz(),
|
|
|
+ " and " + md.getParentFieldName() + "='" + pk + "'", null);
|
|
|
+ if (vos.size() != 0) {
|
|
|
+ deleteByCondition(md.getClazz(), " and " + md.getParentFieldName() + "='" + pk + "'");
|
|
|
+ for (SuperVO vo : vos) {
|
|
|
+ deleteRecursion(md.getClazz(), vo.getPrimaryKey());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据条件删除数据
|
|
|
+ * @param clazz
|
|
|
+ * @param condition
|
|
|
+ */
|
|
|
+ public void deleteByCondition(Class<? extends SuperVO> clazz, String condition) {
|
|
|
+ try {
|
|
|
+ List<? extends SuperVO> vos = queryByCondition(clazz, condition, null);
|
|
|
+ deleteList(vos);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("删除数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除一个List
|
|
|
+ * @param delList
|
|
|
+ */
|
|
|
+ public void deleteList(List<? extends SuperVO> delList) {
|
|
|
+ try {
|
|
|
+ if (delList != null && delList.size() > 0) {
|
|
|
+ for (SuperVO vo : delList) {
|
|
|
+ deleteByPK(vo.getClass(), vo.getPrimaryKey());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("删除数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除一个数组
|
|
|
+ * @param supers
|
|
|
+ */
|
|
|
+ public void deleteArray(SuperVO[] supers) {
|
|
|
+ try {
|
|
|
+ deleteList(Arrays.asList(supers));
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("删除数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将结果集转换成对象
|
|
|
+ * @param rs2vo
|
|
|
+ * @param rs
|
|
|
+ * @param fieldNames
|
|
|
+ */
|
|
|
+ private void rs2vo(SuperVO rs2vo, ResultSet rs, String[] fieldNames) {
|
|
|
+ try {
|
|
|
+ for (String field : fieldNames) {
|
|
|
+ field = field.toUpperCase();
|
|
|
+ Object obj = rs.getObject(field);
|
|
|
+ if (obj != null) {
|
|
|
+ if (obj instanceof oracle.sql.TIMESTAMP) {
|
|
|
+ oracle.sql.TIMESTAMP timestamp = (oracle.sql.TIMESTAMP) obj;
|
|
|
+ obj = timestamp.timestampValue();
|
|
|
+ } else if (obj instanceof Date) {
|
|
|
+ Date date = (Date) obj;
|
|
|
+ obj = new java.util.Date(date.getTime());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rs2vo.setAttribute(field.toLowerCase(), obj);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("删除数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得查询SQL
|
|
|
+ * @param tableName
|
|
|
+ * @param fieldNames
|
|
|
+ * @param condition
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getQuerySQL(String tableName, String[] fieldNames, String condition) {
|
|
|
+ if (condition == null) {
|
|
|
+ condition = "";
|
|
|
+ }
|
|
|
+ condition = condition + " and IFNULL(dr,0)=0";
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ if (fieldNames.length != 0) {
|
|
|
+ sb.append("select ");
|
|
|
+ for (String field : fieldNames) {
|
|
|
+ sb.append(field + ",");
|
|
|
+ }
|
|
|
+ sb.setLength(sb.length() - 1);
|
|
|
+ sb.append(" from " + tableName + " where 1=1 ");
|
|
|
+ if (condition != null && !condition.trim().equals("")) {
|
|
|
+ sb.append(condition);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得查询SQL(wy)
|
|
|
+ * @param tableName
|
|
|
+ * @param fieldNames
|
|
|
+ * @param condition
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getQueryAllSQL(String tableName, String[] fieldNames, String condition) {
|
|
|
+ if (condition == null) {
|
|
|
+ condition = "";
|
|
|
+ }
|
|
|
+ condition = condition + " and IFNULL(delete_tag,0)=0";
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ if (fieldNames.length != 0) {
|
|
|
+ sb.append("select ");
|
|
|
+ for (String field : fieldNames) {
|
|
|
+ sb.append(field + ",");
|
|
|
+ }
|
|
|
+ sb.setLength(sb.length() - 1);
|
|
|
+ sb.append(" from " + tableName + " where 1=1 ");
|
|
|
+ if (condition != null && !condition.trim().equals("")) {
|
|
|
+ sb.append(condition);
|
|
|
+ }
|
|
|
+// Log.info(sb.toString());
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得查询SQL不关心删除标记(wy)
|
|
|
+ * @param tableName
|
|
|
+ * @param fieldNames
|
|
|
+ * @param condition
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getQueryAllSQLNocare(String tableName, String[] fieldNames, String condition) {
|
|
|
+ if (condition == null) {
|
|
|
+ condition = "";
|
|
|
+ }
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ if (fieldNames.length != 0) {
|
|
|
+ sb.append("select ");
|
|
|
+ for (String field : fieldNames) {
|
|
|
+ sb.append(field + ",");
|
|
|
+ }
|
|
|
+ sb.setLength(sb.length() - 1);
|
|
|
+ sb.append(" from " + tableName + " where 1=1 ");
|
|
|
+ if (condition != null && !condition.trim().equals("")) {
|
|
|
+ sb.append(condition);
|
|
|
+ }
|
|
|
+ Log.info(sb.toString());
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据主键获得查询语句
|
|
|
+ * @param vo
|
|
|
+ * @param tableName
|
|
|
+ * @param fieldNames
|
|
|
+ * @param pk
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getQueryByPK(SuperVO vo, String tableName, String[] fieldNames, String pk) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ if (fieldNames.length != 0) {
|
|
|
+ sb.append("select ");
|
|
|
+ for (String field : fieldNames) {
|
|
|
+ sb.append(field + ",");
|
|
|
+ }
|
|
|
+ sb.setLength(sb.length() - 1);
|
|
|
+ sb.append(" from " + tableName);
|
|
|
+ sb.append(" where " + vo.getPKFieldName() + "='" + pk + "'");
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据键名获得查询语句
|
|
|
+ * @author wy
|
|
|
+ * @param vo
|
|
|
+ * @param tableName
|
|
|
+ * @param fieldNames
|
|
|
+ * @param pk
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getQueryByKey(SuperVO vo, String tableName, String[] fieldNames, String keyName,String k) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ if (fieldNames.length != 0) {
|
|
|
+ sb.append("select ");
|
|
|
+ for (String field : fieldNames) {
|
|
|
+ sb.append(field + ",");
|
|
|
+ }
|
|
|
+ sb.setLength(sb.length() - 1);
|
|
|
+ sb.append(" from " + tableName);
|
|
|
+ sb.append(" where " + keyName+ "='" + k + "'");
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得数据库表的列名信息
|
|
|
+ * @param table
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<String, Integer> getColumnTypes(String table) {
|
|
|
+ try {
|
|
|
+ HashMap<String, Integer> typeMap = new HashMap<String, Integer>();
|
|
|
+ if (SystemCache.table_cache.containsKey(table)) {
|
|
|
+ return SystemCache.table_cache.get(table);
|
|
|
+ }
|
|
|
+ Connection conn = jdbcTemplate.getDataSource().getConnection();
|
|
|
+ DatabaseMetaData dbMetaData = conn.getMetaData();
|
|
|
+ ResultSet rsColumns = null;
|
|
|
+ rsColumns = dbMetaData.getColumns(null, null, table, "%");
|
|
|
+ while (rsColumns.next()) {
|
|
|
+ String columnName = rsColumns.getString("COLUMN_NAME").toUpperCase();
|
|
|
+ int columnType = rsColumns.getShort("DATA_TYPE");
|
|
|
+ typeMap.put(columnName, new Integer(columnType));
|
|
|
+ }
|
|
|
+ rsColumns.close();
|
|
|
+ conn.close();
|
|
|
+ SystemCache.table_cache.put(table, typeMap);
|
|
|
+ return typeMap;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("获得表的元数据产生异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置PreparedStatement
|
|
|
+ * @param prepareStatement
|
|
|
+ * @param sqlParam
|
|
|
+ * @param types
|
|
|
+ * @param fieldName
|
|
|
+ */
|
|
|
+ private void commitTransaction(PreparedStatement prepareStatement, BaseSQLParam sqlParam,
|
|
|
+ Map<String, Integer> types, String[] fieldName) {
|
|
|
+ try {
|
|
|
+ List<?> list = sqlParam.getList();
|
|
|
+ for (int i = 0; i < list.size(); ++i) {
|
|
|
+ Object obj = list.get(i);
|
|
|
+ if (obj instanceof Timestamp) {
|
|
|
+ prepareStatement.setTimestamp(i + 1, (Timestamp) obj);
|
|
|
+ } else if (obj instanceof java.util.Date) {
|
|
|
+ prepareStatement.setDate(i + 1, new Date(((java.util.Date) obj).getTime()));
|
|
|
+ } else {
|
|
|
+ prepareStatement.setObject(i + 1, obj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("设置数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得插入的SQL参数
|
|
|
+ * @param vo
|
|
|
+ * @param fieldNames
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private BaseSQLParam getSQLParam(SuperVO vo, String[] fieldNames) {
|
|
|
+ BaseSQLParam sqlParam = new BaseSQLParam();
|
|
|
+ for (String fieldName : fieldNames) {
|
|
|
+ sqlParam.addParam(vo.getAttribute(fieldName));
|
|
|
+ }
|
|
|
+ return sqlParam;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得更新的SQL参数
|
|
|
+ * @param vo
|
|
|
+ * @param fieldNames
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private BaseSQLParam getUpdateSQLParam(SuperVO vo, String[] fieldNames) {
|
|
|
+ BaseSQLParam sqlParam = new BaseSQLParam();
|
|
|
+ String pkValue = "";
|
|
|
+ for (String fieldName : fieldNames) {
|
|
|
+ if (!fieldName.equals(vo.getPKFieldName()))
|
|
|
+ sqlParam.addParam(vo.getAttribute(fieldName));
|
|
|
+ else {
|
|
|
+ pkValue = (String) vo.getAttribute(fieldName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sqlParam.addParam(pkValue);
|
|
|
+ return sqlParam;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得插入SQL
|
|
|
+ * @param table
|
|
|
+ * @param names
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getInsertSQL(String table, String[] names) {
|
|
|
+ StringBuffer buffer = new StringBuffer("INSERT INTO " + table + " (");
|
|
|
+ for (int i = 0; i < names.length; ++i) {
|
|
|
+ buffer.append(names[i] + ",");
|
|
|
+ }
|
|
|
+ buffer.setLength(buffer.length() - 1);
|
|
|
+ buffer.append(") VALUES (");
|
|
|
+ for (int i = 0; i < names.length; ++i) {
|
|
|
+ buffer.append("?,");
|
|
|
+ }
|
|
|
+ buffer.setLength(buffer.length() - 1);
|
|
|
+ buffer.append(")");
|
|
|
+ return buffer.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得更新SQL
|
|
|
+ * @param table
|
|
|
+ * @param names
|
|
|
+ * @param vo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getUpdateSQL(String table, String[] names, SuperVO vo) {
|
|
|
+ StringBuffer buffer = new StringBuffer("UPDATE " + table + " SET ");
|
|
|
+ for (String name : names) {
|
|
|
+ if (!name.equals(vo.getPKFieldName())) {
|
|
|
+ buffer.append(name + "=?,");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ buffer.setLength(buffer.length() - 1);
|
|
|
+ buffer.append(" where " + vo.getPKFieldName() + "=?");
|
|
|
+ return buffer.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得更新SQL
|
|
|
+ * @author wy
|
|
|
+ * @param table
|
|
|
+ * @param names
|
|
|
+ * @param vo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getUpdateAllSQL(String table, String[] names, SuperVO vo) {
|
|
|
+ StringBuffer buffer = new StringBuffer("UPDATE " + table + " SET ");
|
|
|
+ for (String name : names) {
|
|
|
+ if (!name.equals(vo.getPKFieldName())) {
|
|
|
+ buffer.append(name + "=?,");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ buffer.setLength(buffer.length() - 1);
|
|
|
+ buffer.append(" where dev_id =?");
|
|
|
+ return buffer.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得插入和查询数据时有效的列名
|
|
|
+ * @param vo
|
|
|
+ * @param types
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String[] getValidNames(SuperVO vo, Map<String, Integer> types) {
|
|
|
+ String[] names = vo.getAttributeNames();
|
|
|
+ List<String> nameList = new ArrayList<String>();
|
|
|
+ for (int i = 0; i < names.length; ++i) {
|
|
|
+ if (types.get(names[i].toUpperCase()) != null) {
|
|
|
+ nameList.add(names[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return (String[]) nameList.toArray(new String[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得更新时有效的列名
|
|
|
+ * @param vo
|
|
|
+ * @param types
|
|
|
+ * @param paramNameList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String[] getUpdateValidNames(SuperVO vo, Map<String, Integer> types, List<String> paramNameList) {
|
|
|
+ String[] names = vo.getAttributeNames();
|
|
|
+ List<String> nameList = new ArrayList<String>();
|
|
|
+ for (int i = 0; i < names.length; ++i) {
|
|
|
+ if (!names[i].equals("operator") && !names[i].equals("create_time") && !names[i].equals("dr")
|
|
|
+ && !names[i].equals("org_id") && !names[i].equals(vo.getPKFieldName())
|
|
|
+ && types.get(names[i].toUpperCase()) != null) {
|
|
|
+ if (paramNameList.size() > 0 && paramNameList.contains(names[i])) {
|
|
|
+ nameList.add(names[i]);
|
|
|
+ } else {
|
|
|
+ if (vo.getAttribute(names[i]) != null) {
|
|
|
+ nameList.add(names[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ nameList.add(vo.getPKFieldName());
|
|
|
+ return (String[]) nameList.toArray(new String[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加入排序SQL
|
|
|
+ * @param sql
|
|
|
+ * @param sortSQL
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String addSortSQL(String sql, String sortSQL) {
|
|
|
+ if (!StringUtils.isEmpty(sortSQL)) {
|
|
|
+ sql = sql + " order by " + sortSQL;
|
|
|
+ }
|
|
|
+ return sql;
|
|
|
+ }
|
|
|
+
|
|
|
+ public JdbcTemplate getJdbcTemplate() {
|
|
|
+ return jdbcTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据主键数组查询数据
|
|
|
+ * @param <T>
|
|
|
+ * @param clazz
|
|
|
+ * @param pks
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> List<T> queryByPks(Class<? extends SuperVO> clazz, String[] pks) {
|
|
|
+ try {
|
|
|
+ List<T> list = new ArrayList<T>();
|
|
|
+ if (pks != null && pks.length > 0) {
|
|
|
+ SuperVO vo = clazz.newInstance();
|
|
|
+ String tableName = vo.getTableName();
|
|
|
+ Map<String, Integer> types = getColumnTypes(tableName);
|
|
|
+ String[] fieldNames = getValidNames(vo, types);
|
|
|
+ StringBuffer buf = new StringBuffer(" and id in (");
|
|
|
+ for (String pk : pks) {
|
|
|
+ buf.append("'").append(pk).append("',");
|
|
|
+ }
|
|
|
+ String inpk = buf.substring(0, buf.length() - 1) + ")";
|
|
|
+ String sql = getQuerySQL(tableName, fieldNames, inpk);
|
|
|
+
|
|
|
+ if (sql == null) {
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ list = jdbcTemplate.query(sql, new RowMapper<T>() {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ @Override
|
|
|
+ public T mapRow(ResultSet rs, int arg1) throws SQLException {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ SuperVO rs2vo = null;
|
|
|
+ try {
|
|
|
+ rs2vo = (SuperVO) clazz.newInstance();
|
|
|
+ rs2vo(rs2vo, rs, fieldNames);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return (T) rs2vo;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.error(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询数据异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|