Commit cd51b868 authored by chenzai's avatar chenzai

优化分页查询

parent eb797018
package com.yeejoin.amos.boot.module.cas.api.service; package com.yeejoin.amos.boot.module.cas.api.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.yeejoin.amos.boot.module.cas.api.entity.IdxBizXnzs;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -11,56 +13,13 @@ import java.util.List; ...@@ -11,56 +13,13 @@ import java.util.List;
import java.sql.*; import java.sql.*;
import java.util.*; import java.util.*;
@Service public interface IdxBizSyncService extends IService<IdxBizXnzs> {
public class IdxBizSyncService {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String passwd;
// 多个查询(分页) // 多个查询(分页)
public List selectAllPageQuery(String tableName, Map params, int current, int size) throws SQLException { List selectAllPageQuery(String tableName, Map params, int current, int size);
Connection connection = DriverManager.getConnection(url, user, passwd);
System.out.println(getSql(tableName, params, current, size));
PreparedStatement preparedStatement = connection.prepareStatement(getSql(tableName, params, current, size));
ResultSet set = preparedStatement.executeQuery();
List list = convertList(set);
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
return list;
}
public String getSql(String tableName, Map params, int current, int size) { String getSql(String tableName, Map params, int current, int size);
String sql = "select * from " + tableName;
sql += " where ";
Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
sql = sql + entry.getKey() + " = " + "'" + entry.getValue() + "'" + " AND ";
}
sql += "1 = 1 ";
sql += " limit " + (current - 1) * size + "," + size;
// Page
return sql;
}
public List convertList(ResultSet rs) throws SQLException {
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount(); //Map rowData;
while (rs.next()) { //rowData = new HashMap(columnCount);
Map rowData = new HashMap();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
}
return list;
}
} }
\ No newline at end of file
package com.yeejoin.amos.boot.module.cas.service.impl;
import com.yeejoin.amos.boot.module.cas.api.dto.IdxBizXnzsDto;
import com.yeejoin.amos.boot.module.cas.api.entity.IdxBizXnzs;
import com.yeejoin.amos.boot.module.cas.api.mapper.IdxBizXnzsMapper;
import com.yeejoin.amos.boot.module.cas.api.service.IIdxBizXnzsService;
import com.yeejoin.amos.boot.module.cas.api.service.IdxBizSyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.typroject.tyboot.core.rdbms.service.BaseService;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@Service
public class IdxBizSyncServiceImpl extends BaseService<IdxBizXnzsDto, IdxBizXnzs, IdxBizXnzsMapper> implements IdxBizSyncService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List selectAllPageQuery(String tableName, Map params, int current, int size) {
List<Map<String, Object>> list = jdbcTemplate.queryForList(getSql(tableName, params, current, size));
return list;
}
@Override
public String getSql(String tableName, Map params, int current, int size) {
String sql = "select * from " + tableName;
sql += " where ";
Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
sql = sql + entry.getKey() + " = " + "'" + entry.getValue() + "'" + " AND ";
}
sql += "1 = 1 ";
sql += " limit " + (current - 1) * size + "," + size;
return sql;
}
}
#DB properties: #DB properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://36.46.151.113:5432/project_test?allowMultiQueries=true&serverTimezone=GMT%2B8\ spring.datasource.url=jdbc:mysql://39.98.45.134:3306/amos_idx?allowMultiQueries=true&serverTimezone=GMT%2B8\
&characterEncoding=utf8 &characterEncoding=utf8&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=CONVERT_TO_NULL
spring.datasource.username=admin spring.datasource.username=root
spring.datasource.password=Yeejoin@2023 spring.datasource.password=Yeejoin@2020
##eureka properties: ##eureka properties:
eureka.client.service-url.defaultZone =http://172.16.10.210:10001/eureka/ eureka.client.service-url.defaultZone =http://172.16.10.210:10001/eureka/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment