方式一
@Mapper
public interface DemoMapper extends BaseMapper < TableConfig > { @Update ( "${sql}" ) int createTable ( @Param ( "sql" ) String sql) ;
}
@SpringBootTest
class DemoMapperTest { @Resource private DemoMapper demoMapper; @Test void fun ( ) { String sql = """CREATE TABLE `person_article` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',`author` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '作者',`title` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '文章标题',PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC COMMENT='文章管理表';""" ; tableConfigMapper. createTable ( sql) ; } }
方式二
mybatis- plus: global- config: enable- sql- runner: true # 支持使用SqlRunner 执行原生的SQL 语句
@SpringBootTest
class DemoMapperTest { @Test void fun ( ) { String sql = """CREATE TABLE `person_article` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',`author` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '作者',`title` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '文章标题',PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC COMMENT='文章管理表';""" ; SqlRunner . db ( ) . update ( sql) ; } }