|
| 1 | +package com.codingapi.springboot.fast.mapping; |
| 2 | + |
| 3 | +import com.codingapi.springboot.fast.dynamic.DynamicQuery; |
| 4 | +import lombok.AllArgsConstructor; |
| 5 | +import lombok.SneakyThrows; |
| 6 | +import org.apache.commons.text.CaseUtils; |
| 7 | +import org.springframework.jdbc.core.BeanPropertyRowMapper; |
| 8 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 9 | +import org.springframework.jdbc.core.RowMapper; |
| 10 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 11 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 12 | + |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.sql.ResultSet; |
| 15 | +import java.sql.ResultSetMetaData; |
| 16 | +import java.sql.SQLException; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +@AllArgsConstructor |
| 22 | +public class DynamicMappingRegister { |
| 23 | + |
| 24 | + private final MvcEndpointMapping mvcEndpointMapping; |
| 25 | + |
| 26 | + private final DynamicQuery dynamicQuery; |
| 27 | + |
| 28 | + private final JdbcTemplate jdbcTemplate; |
| 29 | + |
| 30 | + |
| 31 | + private void addJdbcMapping(String mapping, RequestMethod requestMethod,String sql,Class<?> clazz, Object ... params) { |
| 32 | + Object handler = new JdbcMapping( jdbcTemplate,sql,clazz,params); |
| 33 | + Method method = getJdbcMethod(); |
| 34 | + mvcEndpointMapping.addMapping(mapping, requestMethod, handler, method); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + private void addJdbcMapping(String mapping, RequestMethod requestMethod,String sql, Object ... params) { |
| 39 | + Object handler = new JdbcMapMapping( jdbcTemplate,sql,params); |
| 40 | + Method method = getJdbcMapMethod(); |
| 41 | + mvcEndpointMapping.addMapping(mapping, requestMethod, handler, method); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + private void addHqlMapping(String mapping, RequestMethod requestMethod,String hql,Class<?> clazz, Object ... params) { |
| 46 | + Object handler = new HqlMapping(dynamicQuery,hql,clazz,params); |
| 47 | + Method method = getHqlMethod(); |
| 48 | + mvcEndpointMapping.addMapping(mapping, requestMethod, handler, method); |
| 49 | + } |
| 50 | + |
| 51 | + public void addMapping(DynamicMapping dynamicMapping) { |
| 52 | + switch (dynamicMapping.getType()) { |
| 53 | + case JDBC: |
| 54 | + addJdbcMapping(dynamicMapping.getMapping(), dynamicMapping.getRequestMethod(), dynamicMapping.getSql(), dynamicMapping.getClazz(), dynamicMapping.getParams()); |
| 55 | + break; |
| 56 | + case HQL: |
| 57 | + addHqlMapping(dynamicMapping.getMapping(), dynamicMapping.getRequestMethod(), dynamicMapping.getSql(), dynamicMapping.getClazz(), dynamicMapping.getParams()); |
| 58 | + break; |
| 59 | + case JDBC_MAP: |
| 60 | + addJdbcMapping(dynamicMapping.getMapping(), dynamicMapping.getRequestMethod(), dynamicMapping.getSql(), dynamicMapping.getParams()); |
| 61 | + break; |
| 62 | + default: |
| 63 | + throw new RuntimeException("not support type"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + @SneakyThrows |
| 70 | + private Method getJdbcMethod(){ |
| 71 | + return JdbcMapping.class.getDeclaredMethod("execute"); |
| 72 | + } |
| 73 | + |
| 74 | + @SneakyThrows |
| 75 | + private Method getJdbcMapMethod(){ |
| 76 | + return JdbcMapMapping.class.getDeclaredMethod("execute"); |
| 77 | + } |
| 78 | + |
| 79 | + @SneakyThrows |
| 80 | + private Method getHqlMethod(){ |
| 81 | + return HqlMapping.class.getDeclaredMethod("execute"); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + @AllArgsConstructor |
| 86 | + public static class JdbcMapMapping{ |
| 87 | + |
| 88 | + private final JdbcTemplate jdbcTemplate; |
| 89 | + private final String sql; |
| 90 | + private final Object[] params; |
| 91 | + |
| 92 | + @ResponseBody |
| 93 | + public List<Map<String,Object>> execute() { |
| 94 | + return jdbcTemplate.query(sql, params, new CamelCaseRowMapper()); |
| 95 | + } |
| 96 | + |
| 97 | + private static class CamelCaseRowMapper implements RowMapper<Map<String, Object>> { |
| 98 | + |
| 99 | + @Override |
| 100 | + public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException { |
| 101 | + ResultSetMetaData metaData = rs.getMetaData(); |
| 102 | + int columnCount = metaData.getColumnCount(); |
| 103 | + Map<String, Object> map = new HashMap<>(columnCount); |
| 104 | + for (int i = 1; i <= columnCount; i++) { |
| 105 | + String columnName = metaData.getColumnLabel(i); |
| 106 | + map.put(CaseUtils.toCamelCase(columnName,false), rs.getObject(i)); |
| 107 | + } |
| 108 | + return map; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + @AllArgsConstructor |
| 115 | + public static class JdbcMapping{ |
| 116 | + |
| 117 | + private final JdbcTemplate jdbcTemplate; |
| 118 | + private final String sql; |
| 119 | + private final Class<?> clazz; |
| 120 | + private final Object[] params; |
| 121 | + |
| 122 | + @ResponseBody |
| 123 | + public List<?> execute() { |
| 124 | + return jdbcTemplate.query(sql, params, new BeanPropertyRowMapper<>(clazz)); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + @AllArgsConstructor |
| 133 | + public static class HqlMapping{ |
| 134 | + |
| 135 | + private final DynamicQuery dynamicQuery; |
| 136 | + private final String hql; |
| 137 | + private final Class<?> clazz; |
| 138 | + private final Object[] params; |
| 139 | + |
| 140 | + @ResponseBody |
| 141 | + public List<?> execute() { |
| 142 | + return dynamicQuery.listQuery(clazz,hql, params); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments