mybtais 动态sql注解
所属分类 mybatis
浏览量 106
MyBatis使用 @SelectProvider、@InsertProvider、@UpdateProvider、@DeleteProvider注解 建动态SQL语句。
type属性,该属性指定一个类;method属性指定该类的方法,其用来提供需要执行的SQL语句
PetMapper.java
@SelectProvider(type= SqlProvider.class,method="queryPetsByStartId")
List< PetDO> queryPetsByStartIdIfIdNotNull2(PetDO pet);
public class SqlProvider {
public String queryPetsByStartId(PetDO pet){
String sql = "SELECT id,name,age FROM pet ";
Long id = null;
if(pet!=null) {
id = pet.getId();
}
if(id!=null && id>0){
sql = sql + " where id=#{id} ";
}
sql = sql + " order by id asc";
return sql;
}
}
@RequestMapping("/queryPetsByStartIdIfIdNotNull2")
public Object queryPetsByStartIdIfIdNotNull2(@RequestParam long id){
PetDO pet = new PetDO();
if(id>0){
pet.setId(id);
}
return petMapper.queryPetsByStartIdIfIdNotNull2(pet);
}
查看 sql 信息的工具
@RequestMapping("/getMappedStatementNames")
public Object getMappedStatementNames(){
return sqlSessionFactory.getConfiguration().getMappedStatementNames();
}
@RequestMapping("/getSqlSource")
public Object getSqlSource(String id)throws Exception{
Configuration config = sqlSessionFactory.getConfiguration();
MappedStatement mappedStatement = config.getMappedStatement(id);
if(mappedStatement==null){
return "mappedStatement_null";
}
SqlSource sqlSource = mappedStatement.getSqlSource();
Map map = new HashMap<>();
map.put("type",sqlSource.getClass().toString());
map.put("info",sqlSource.toString());
return map;
}
@RequestMapping("/getBoundSql")
public Object getBoundSql(String id)throws Exception{
Configuration config = sqlSessionFactory.getConfiguration();
MappedStatement mappedStatement = config.getMappedStatement(id);
if(mappedStatement==null){
return "mappedStatement_null";
}
SqlSource sqlSource = mappedStatement.getSqlSource();
return sqlSource.getBoundSql(null);
}
@RequestMapping("/getBoundSql2")
public Object getBoundSql2(String id,long petId)throws Exception{
Configuration config = sqlSessionFactory.getConfiguration();
MappedStatement mappedStatement = config.getMappedStatement(id);
if(mappedStatement==null){
return "mappedStatement_null";
}
SqlSource sqlSource = mappedStatement.getSqlSource();
PetDO pet = null;new PetDO();
if(petId>0) {
pet = new PetDO();
pet.setId(petId);
}
return sqlSource.getBoundSql(pet);
}
}
http://127.0.0.1:8080/getSqlSource?id=org.demo.dao.mapper.PetMapper.queryPetsByStartIdIfIdNotNull2
{"type":"class org.apache.ibatis.builder.annotation.ProviderSqlSource","info":"org.apache.ibatis.builder.annotation.ProviderSqlSource@152789ae"}
http://127.0.0.1:8080/getBoundSql2?id=org.demo.dao.mapper.PetMapper.queryPetsByStartIdIfIdNotNull2&petId=0
{"sql":"SELECT id,name,age FROM pet order by id asc","parameterMappings":[],"parameterObject":null}
http://127.0.0.1:8080/getBoundSql2?id=org.demo.dao.mapper.PetMapper.queryPetsByStartIdIfIdNotNull2&petId=1
{
"sql": "SELECT id,name,age FROM pet where id=? order by id asc",
"parameterMappings": [
{
"property": "id",
"mode": "IN",
"javaType": "java.lang.Long",
"jdbcType": null,
"numericScale": null,
"typeHandler": {
"rawType": "java.lang.Long"
},
"resultMapId": null,
"jdbcTypeName": null,
"expression": null
}
],
"parameterObject": {
"id": 1,
"name": null,
"age": null
}
}
https://gitee.com/dyyx/work2024/blob/master/demo/mybatis-paging-demo/src/main/java/org/demo/controller/HelloController.java
上一篇
下一篇
Mybatis SqlSource 与 BoundSql
小红书视频下载
羽毛球新手学习顺序
mybtais mybatisplus TkMybatis 原理
java final 字段 反射修改说明
煎荷包蛋小技巧