mybatis-plus执行原生sql
mybaits版本信息pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
使用SqlRunner的方式执行原生SQL
演示查询和更新代码、绑定参数。
String tableName = "具体表名称";
String sql = "select top 10 * from " + tableName + " where gonghao={0} and dd={1} ";
mapList = SqlRunner.db().selectList(sql, user.getUserid(), attendanceDate);
if (mapList != null && mapList.size() > 0) {
for (Map<String, Object> ttDayKQ : mapList) {
Object ccqj = ttDayKQ.get("CCQJ");
if((int)ccqj==0){
int[] 参数列表= calcKqFactorVal(signinList,attendanceDate,signinRulesList);
String updateSql = "update " + tableName + " set kqs={0},cd={1},zt={2} where gonghao={3} and dd={4} and zzpid={5}";
SqlRunner.db().update(updateSql, 参数列表[0], 参数列表[1], 参数列表[2], user.getUserid(), attendanceDate, String.valueOf(user.getZzpid()));
}
}
}
使用SqlRunner的前提是打开SqlRunner,编辑application.yaml增加配置如下:
mybatis-plus:
global-config:
enable-sql-runner: true
否则会报错
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.baomidou.mybatisplus.core.mapper.SqlRunner.SelectList
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.baomidou.mybatisplus.core.mapper.SqlRunner.SelectList