mybatis执行原生sql
package com.lynch.asap.config.mapper;
import org.apache.ibatis.annotations.Param;
public interface SqlMapper {
void dynamicsInsert(@Param("paramSQL") String sql);
void dynamicsUpdate(@Param("paramSQL") String sql);
void dynamicsDelete(@Param("paramSQL") String sql);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lynch.asap.config.mapper.SqlMapper">
<insert id="dynamicsInsert">
${paramSQL}
</insert>
<insert id="dynamicsUpdate">
${paramSQL}
</insert>
<delete id="dynamicsDelete">
${paramSQL}
</delete>
</mapper>
import org.junit.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.SpringRunner;
import com.yunping.asap.WebApplication;
import com.yunping.asap.config.DataSourceContext;
import com.yunping.asap.config.model.CfSerialClass;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WebApplication.class)
public class SqlMapperTest {
@Autowired
private SqlMapper sqlMapper;
@Test
public void dynamicsInsert() {
DataSourceContext.setRouterKey("asap-test1");
String sql = "INSERT INTO `cf_constant` (`cc_id`, `cc_code`, `cc_name`, `ccc_id`, `ccc_code`, `ap_stage`, `cc_key`, `cc_value`, `cc_sort`, `begin_time`, `end_time`, `cc_createtime`, `cc_creator`, `update_time`, `modifier`, `so_org`, `so_group`) VALUES ('aaaa', NULL, NULL, '4bf5d5ae2fa6450b916c854c73ad5171', 'FJLX', NULL, 'ht', '合同', NULL, NULL, NULL, '2019-06-06 14:08:22', NULL, '2019-06-06 14:08:23', NULL, '', NULL)";
sqlMapper.dynamicsInsert(sql);
}
@Test
public void dynamicsUpdate() {
DataSourceContext.setRouterKey("asap-test1");
String sql = "UPDATE `cf_constant` SET `cc_id`='aaaa', `cc_code`=NULL, `cc_name`=NULL, `ccc_id`='4bf5d5ae2fa6450b916c854c73ad5171', `ccc_code`='FJLX', `ap_stage`=NULL, `cc_key`='ht', `cc_value`='合同1111111111111111', `cc_sort`=NULL, `begin_time`=NULL, `end_time`=NULL, `cc_createtime`='2019-06-06 14:08:22', `cc_creator`=NULL, `update_time`='2019-06-06 14:08:23', `modifier`=NULL, `so_org`='', `so_group`=NULL WHERE (`cc_id`='aaaa')";
sqlMapper.dynamicsUpdate(sql);
}
@Test
public void dynamicsDelete() {
DataSourceContext.setRouterKey("asap-test1");
String sql = "delete from `cf_constant` where cc_id='aaaa'";
sqlMapper.dynamicsDelete(sql);
}
}