在手写mybatis框架过程中需要将
insert into t_car values (#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
语句中的参数提取出来,现在做个测试将其输出并输出是第几个参数,这样就可以使用原生的jdbc代码对数据库进行操作
String sql = "insert into t_car values (#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})";
int fromIndex=0;
int index = 1;
while (true){
//找到井号每次从索引为零开始
int jingIndex = sql.indexOf("#",fromIndex);
if(jingIndex <0){
break;
}
//输出第几个参数
System.out.println(index);
index++;
//找到最后一个字符 } 的索引但不包含 }
int RightKuoHao = sql.indexOf("}",fromIndex);
//将参数打印出来
String propertyName = sql.substring(jingIndex + 2, RightKuoHao);
//输出
System.out.println(propertyName);
fromIndex = RightKuoHao+1;
}