合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## @SqlProvider 类似@Sql一样,@SqlProvider指定了提供sql语句的类,BeetlSQL会调用同名方法获取sql语句和参数 ```java @SqlProvider(provider= SelectUserProvider.class) List<UserEntity> queryUserByCondition(String name); ``` SelectUserProvider定义如下 ```java public static class SelectUserProvider{ /*与mapper方法同名,同参数*/ public SQLReady queryUserByCondition(String name){ SQLReady ready = null; if(name==null){ String sql = "select * from sys_user where 1=1"; ready = new SQLReady(sql); }else{ String sql = "select * from sys_user where name=?"; ready = new SQLReady(sql,name); } return ready; } } ``` BeetlSQL将会调用SelectUserProvider实例的同名同参数方法,获得SQLReady,执行查询。BeetlSQL底层代码类似如下代码 ```java SelectUserProvider provider = BeanKit.newSingleInstance(SelectUserProvider.class); SQLReady ready = provider.queryUserByCondition(name); List<UserEntity> list = sqlManager.execute(ready,UserEntity.class) ``` @SqlProvider 注解有利于提供复杂的SQL,或者在原有@Sql不满足的情况,在不改变mapper方法情况下,使用@SqlProvider 提供新的SQL `@SqlProvider`的实现类是`ProviderMapperExtBuilder` ## @SqlTemplateProvider SqlTemplateProvider类似@SqlProvider,不同的是要求返回一个模板SQL语句 ```java @SqlTemplateProvider(provider= SelectUserProvider.class) List<UserEntity> queryUserByTemplateCondition(String name); ``` SelectUserProvider的queryUserByTemplateCondition定义如下 ```java public class SelectUserProvider{ /*与mapper方法同名,同参数*/ public String queryUserByTemplateCondition(String name){ String sql = "select * from sys_user where 1=1"; if(name==null){ return sql; }else{ return sql+" and name=#{name}"; } } } ``` BeetlSQL底层代码类似如下 ```java SelectUserProvider provider = BeanKit.newSingleInstance(SelectUserProvider.class); String sqlTemplate = provider.queryUserByTemplateCondition(name); Map map = new HashMap(); map.put("name",name); List<UserEntity> list = sqlManager.execute(sqlTemplate,UserEntity.class,map); ``` 注解`@SqlTemplateProvider`的实现类是`ProviderMapperExtBuilder`