9 changed files with 134 additions and 9 deletions
@ -0,0 +1,61 @@ |
|||||
|
package com.qs.serve.modules.exl.common; |
||||
|
|
||||
|
import org.apache.ibatis.session.SqlSession; |
||||
|
import org.mybatis.spring.SqlSessionTemplate; |
||||
|
import org.mybatis.spring.SqlSessionUtils; |
||||
|
|
||||
|
import java.sql.PreparedStatement; |
||||
|
import java.sql.SQLException; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2023/8/14 |
||||
|
*/ |
||||
|
public class QsSqlSessionUtil { |
||||
|
|
||||
|
/** |
||||
|
* 获取sqlSession |
||||
|
* @return |
||||
|
*/ |
||||
|
public static SqlSession getSqlSession(SqlSessionTemplate sqlSessionTemplate){ |
||||
|
return SqlSessionUtils.getSqlSession(sqlSessionTemplate.getSqlSessionFactory(), |
||||
|
sqlSessionTemplate.getExecutorType(), sqlSessionTemplate.getPersistenceExceptionTranslator()); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 关闭sqlSession |
||||
|
* @param session |
||||
|
*/ |
||||
|
public static void closeSqlSession(SqlSession session,SqlSessionTemplate sqlSessionTemplate) { |
||||
|
SqlSessionUtils.closeSqlSession(session, sqlSessionTemplate.getSqlSessionFactory()); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 执行sql |
||||
|
* @param sql |
||||
|
* @param sqlSessionTemplate |
||||
|
* @return |
||||
|
*/ |
||||
|
public static boolean executeSql(String sql,SqlSessionTemplate sqlSessionTemplate){ |
||||
|
PreparedStatement pst = null; |
||||
|
SqlSession session = getSqlSession(sqlSessionTemplate); |
||||
|
try { |
||||
|
pst = session.getConnection().prepareStatement(sql); |
||||
|
pst.execute(); |
||||
|
return true; |
||||
|
} catch (SQLException e) { |
||||
|
e.printStackTrace(); |
||||
|
}finally { |
||||
|
if(pst!=null){ |
||||
|
try { |
||||
|
pst.close(); |
||||
|
} catch (SQLException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
closeSqlSession(session,sqlSessionTemplate); |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,39 @@ |
|||||
|
package com.qs.serve.modules.exl.common; |
||||
|
|
||||
|
import com.qs.serve.modules.exl.entity.ExlColumnConf; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2023/8/14 |
||||
|
*/ |
||||
|
public class TableCreateSqlUtil { |
||||
|
|
||||
|
public static final Map<String,String> MYSQL_DATA_MAP = new HashMap<>(); |
||||
|
|
||||
|
public static final String COLUMN_NULL = " null "; |
||||
|
public static final String COLUMN_NOT_NULL = " not null "; |
||||
|
public static final String COLUMN_COMMENT = "COMMENT"; |
||||
|
|
||||
|
public static String createMysqlTableSql(String tableName, List<ExlColumnConf> columnList){ |
||||
|
StringBuffer result = new StringBuffer("CREATE TABLE `"+tableName+"` ("); |
||||
|
result.append(" `union_id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',"); |
||||
|
for (ExlColumnConf column : columnList) { |
||||
|
String columnType = column.getColumnType(); |
||||
|
String nullValue = column.getNotNullFlag().equals(1) ? COLUMN_NOT_NULL:COLUMN_NULL; |
||||
|
if(columnType.equals("int")){ |
||||
|
result.append("`"+column.getColumnName()+"` "+ |
||||
|
MYSQL_DATA_MAP.get(columnType) + |
||||
|
nullValue + |
||||
|
COLUMN_COMMENT + " '"+ |
||||
|
column.getColumnHeader()+"',"); |
||||
|
} |
||||
|
} |
||||
|
result.append(" PRIMARY KEY (`union_id`) USING BTREE "); |
||||
|
return result.append(")").toString(); |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue