添加mybatis-sql执行工具

main
xueqingkun 1 year ago
parent d05091eaaf
commit e4a9621385

@ -0,0 +1,185 @@
package com.supervision.util;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.Configuration;
import java.util.ArrayList;
import java.util.Map;
/**
* mybatisMappedStatement
*/
public class RowMapperStatementBuilder {
private final Configuration configuration;
private final LanguageDriver languageDriver;
public RowMapperStatementBuilder(Configuration configuration) {
this.configuration = configuration;
this.languageDriver = configuration.getDefaultScriptingLanguageInstance();
}
/**
* MSID
*
* @param sql sql
* @param sql sqlCommandType
* @return
*/
private String generateMappedStatementId(String sql, SqlCommandType sqlCommandType) {
return sqlCommandType.toString() + "." + sql.hashCode();
}
/**
* ID
*
* @param msId
* @return
*/
private boolean hasMappedStatement(String msId) {
return configuration.hasStatement(msId, false);
}
/**
* MS
*
* @param msId
* @param sqlSource sqlSource
* @param resultType
*/
private void cacheSelectMappedStatement(String msId, SqlSource sqlSource, final Class<?> resultType) {
if (configuration.hasStatement(msId,false)){
return;
}
MappedStatement ms = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.SELECT)
.resultMaps(new ArrayList<ResultMap>() {
{
add(new ResultMap.Builder(configuration, "defaultResultMap", resultType, new ArrayList<ResultMapping>(0)).build());
}
})
.build();
//缓存MappedStatement
configuration.addMappedStatement(ms);
}
/**
* MS
*
* @param msId
* @param sqlSource sqlSource
* @param sqlCommandType sqlCommandType
*/
private void newUpdateMappedStatement(String msId, SqlSource sqlSource, SqlCommandType sqlCommandType) {
MappedStatement ms = new MappedStatement.Builder(configuration, msId, sqlSource, sqlCommandType)
.resultMaps(new ArrayList<ResultMap>() {
{
add(new ResultMap.Builder(configuration, "defaultResultMap", int.class, new ArrayList<ResultMapping>(0)).build());
}
})
.build();
//缓存
configuration.addMappedStatement(ms);
}
public String select(String sql) {
String msId = generateMappedStatementId(sql, SqlCommandType.SELECT);
if (hasMappedStatement(msId)) {
return msId;
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
cacheSelectMappedStatement(msId, sqlSource, Map.class);
return msId;
}
public String selectDynamic(String sql, Class<?> parameterType) {
String msId = generateMappedStatementId(sql + parameterType, SqlCommandType.SELECT);
if (hasMappedStatement(msId)) {
return msId;
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
cacheSelectMappedStatement(msId, sqlSource, Map.class);
return msId;
}
public String select(String sql, Class<?> resultType) {
String msId = generateMappedStatementId(resultType + sql, SqlCommandType.SELECT);
if (hasMappedStatement(msId)) {
return msId;
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
cacheSelectMappedStatement(msId, sqlSource, resultType);
return msId;
}
public String selectDynamic(String sql, Class<?> parameterType, Class<?> resultType) {
String msId = generateMappedStatementId(resultType + sql + parameterType, SqlCommandType.SELECT);
if (hasMappedStatement(msId)) {
return msId;
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
cacheSelectMappedStatement(msId, sqlSource, resultType);
return msId;
}
public String insert(String sql) {
String msId = generateMappedStatementId(sql, SqlCommandType.INSERT);
if (hasMappedStatement(msId)) {
return msId;
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.INSERT);
return msId;
}
public String insertDynamic(String sql, Class<?> parameterType) {
String msId = generateMappedStatementId(sql + parameterType, SqlCommandType.INSERT);
if (hasMappedStatement(msId)) {
return msId;
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.INSERT);
return msId;
}
public String update(String sql) {
String msId = generateMappedStatementId(sql, SqlCommandType.UPDATE);
if (hasMappedStatement(msId)) {
return msId;
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.UPDATE);
return msId;
}
public String updateDynamic(String sql, Class<?> parameterType) {
String msId = generateMappedStatementId(sql + parameterType, SqlCommandType.UPDATE);
if (hasMappedStatement(msId)) {
return msId;
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.UPDATE);
return msId;
}
public String delete(String sql) {
String msId = generateMappedStatementId(sql, SqlCommandType.DELETE);
if (hasMappedStatement(msId)) {
return msId;
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.DELETE);
return msId;
}
public String deleteDynamic(String sql, Class<?> parameterType) {
String msId = generateMappedStatementId(sql + parameterType, SqlCommandType.DELETE);
if (hasMappedStatement(msId)) {
return msId;
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.DELETE);
return msId;
}
}

@ -0,0 +1,229 @@
package com.supervision.util;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* mybatissql
*/
@Component
public class RowSqlMapper {
private final RowMapperStatementBuilder mapperStatementBuilder;
private final SqlSession sqlSession;
/**
* MappedStatement
*
* @param sqlSession
*/
public RowSqlMapper(SqlSession sqlSession) {
this.sqlSession = sqlSession;
this.mapperStatementBuilder = new RowMapperStatementBuilder(sqlSession.getConfiguration());
}
/**
* List
*
* @param list List
* @param <T>
* @return
*/
private <T> T getOne(List<T> list) {
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}
/**
*
*
* @param sql sql
* @return
*/
public Map<String, Object> selectOne(String sql) {
List<Map<String, Object>> list = selectList(sql);
return getOne(list);
}
/**
*
*
* @param sql sql
* @param value
* @return
*/
public Map<String, Object> selectOne(String sql, Object value) {
List<Map<String, Object>> list = selectList(sql, value);
return getOne(list);
}
/**
*
*
* @param sql sql
* @param resultType
* @param <T>
* @return
*/
public <T> T selectOne(String sql, Class<T> resultType) {
List<T> list = selectList(sql, resultType);
return getOne(list);
}
/**
*
*
* @param sql sql
* @param value
* @param resultType
* @param <T>
* @return
*/
public <T> T selectOne(String sql, Object value, Class<T> resultType) {
List<T> list = selectList(sql, value, resultType);
return getOne(list);
}
/**
* List<Map<String, Object>>
*
* @param sql sql
* @return
*/
public List<Map<String, Object>> selectList(String sql) {
String msId = mapperStatementBuilder.select(sql);
return sqlSession.selectList(msId);
}
/**
* List<Map<String, Object>>
*
* @param sql sql
* @param value
* @return
*/
public List<Map<String, Object>> selectList(String sql, Object value) {
Class<?> parameterType = value != null ? value.getClass() : null;
String msId = mapperStatementBuilder.selectDynamic(sql, parameterType);
return sqlSession.selectList(msId, value);
}
/**
*
*
* @param sql sql
* @param resultType
* @param <T>
* @return
*/
public <T> List<T> selectList(String sql, Class<T> resultType) {
String msId;
if (resultType == null) {
msId = mapperStatementBuilder.select(sql);
} else {
msId = mapperStatementBuilder.select(sql, resultType);
}
return sqlSession.selectList(msId);
}
/**
*
*
* @param sql sql
* @param value
* @param resultType
* @param <T>
* @return
*/
public <T> List<T> selectList(String sql, Object value, Class<T> resultType) {
String msId;
Class<?> parameterType = value != null ? value.getClass() : null;
if (resultType == null) {
msId = mapperStatementBuilder.selectDynamic(sql, parameterType);
} else {
msId = mapperStatementBuilder.selectDynamic(sql, parameterType, resultType);
}
return sqlSession.selectList(msId, value);
}
/**
*
*
* @param sql sql
* @return
*/
public int insert(String sql) {
String msId = mapperStatementBuilder.insert(sql);
return sqlSession.insert(msId);
}
/**
*
*
* @param sql sql
* @param value
* @return
*/
public int insert(String sql, Object value) {
Class<?> parameterType = value != null ? value.getClass() : null;
String msId = mapperStatementBuilder.insertDynamic(sql, parameterType);
return sqlSession.insert(msId, value);
}
/**
*
*
* @param sql sql
* @return
*/
public int update(String sql) {
String msId = mapperStatementBuilder.update(sql);
return sqlSession.update(msId);
}
/**
*
*
* @param sql sql
* @param value
* @return
*/
public int update(String sql, Object value) {
Class<?> parameterType = value != null ? value.getClass() : null;
String msId = mapperStatementBuilder.updateDynamic(sql, parameterType);
return sqlSession.update(msId, value);
}
/**
*
*
* @param sql sql
* @return
*/
public int delete(String sql) {
String msId = mapperStatementBuilder.delete(sql);
return sqlSession.delete(msId);
}
/**
*
*
* @param sql sql
* @param value
* @return
*/
public int delete(String sql, Object value) {
Class<?> parameterType = value != null ? value.getClass() : null;
String msId = mapperStatementBuilder.deleteDynamic(sql, parameterType);
return sqlSession.delete(msId, value);
}
}

@ -0,0 +1,45 @@
package com.supervision;
import com.supervision.service.IrKnowledgeService;
import com.supervision.util.RowSqlMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringJUnit4ClassRunner.class)
@RequiredArgsConstructor
class AskApplicationTests {
@Autowired
private IrKnowledgeService irKnowledgeService;
@Autowired
private RowSqlMapper sqlMapper;
@Test
void RowSqlMapperTest() {
HashMap<String, Object> params = new HashMap<>();
params.put("id", "111");
//params.put("dept_name","沈阳公安");
// and dept_name= #{dept_name}
//<if test="dept_name != null">
// and dept_name= #{dept_name}
// </if>
List<Map<String, Object>> maps = sqlMapper.selectList("<script> select * from interro_robot.ir_robot_config where id = #{id} <if test= \"dept_name != null\">" +
" and dept_name= #{dept_name} </if> </script>",params);
System.out.println(maps);
}
}

@ -1,26 +0,0 @@
package com.supervision;
import com.supervision.domain.IrKnowledge;
import com.supervision.domain.IrKnowledgeSimilar;
import com.supervision.service.IrKnowledgeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringJUnit4ClassRunner.class)
@RequiredArgsConstructor
class AskApplicationTests {
@Autowired
private IrKnowledgeService irKnowledgeService;
}
Loading…
Cancel
Save