添加mybatis-sql执行工具
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;
|
||||
|
||||
/**
|
||||
* 一个基于mybatis简单粗暴的构建MappedStatement的实现
|
||||
*/
|
||||
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,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…
Reference in New Issue