mybatis学习之路----bind的简单使用
来源:互联网 发布:三剑客软件下载 编辑:程序博客网 时间:2024/06/16 07:06
点滴记载,点滴进步,愿自己更上一层楼。
mybatis的bind元素可以执行一个表达式,创建一个变量。
可以用于模糊查询。
可以给特定参数加些特殊的业务前缀等等。
其实就是执行传入对象中的某个方法,进行特殊的业务处理。当然get set方法也是方法。
<select id="selectUseIf" parameterType="com.soft.test.model.DynamicTestModel" resultMap="userMap"> <if test="username != null and username neq ''"> <bind name="pattern" value="'%' + _parameter.getUsername() + '%'" /> </if> select * from t_user <where> <if test="username != null and username neq ''"> username like #{pattern} </if> </where> </select>
两个属性 name 创建的变量名称,
value 创建变量的值,包含可执行的表达式,执行一些特殊的操作。
其中的_parameter固定,代表的是传入对象,.getUsername() 传入对象中方法,
如果调用的是对象中的对象的某个方法,_parameter.getDynamicTest1Model().getString()
getDynamicTest1Model 对象中的get方法 获取对象,getString()对象中的对象中的方法。
需要注意:确保被调用的属性已经实例化了,否则会空指针异常。
加一层if判断最适合。
测试方法
private DynamicSqlDao dynamicSqlDao; @Test public void dynamic() throws Exception { dynamicSqlDao = new DynamicSqlDaoImpl(); DynamicTestModel user = new DynamicTestModel(); user.setUsername("xiao"); List<User> userList = dynamicSqlDao.dynamic(user); System.out.println(userList); }测试结果:
DEBUG [main] - ==> Preparing: select * from t_user WHERE username like ?
DEBUG [main] - ==> Parameters: %xiao%(String)
上面说过bind里面的vlaue属性,可以执行的是一个对象的方法,对象必须实例化。下面演示一个自己的随便写的方法,通过bind调用
在model里面定义下面的方法,并在xml的bind里面使用。
public String handleUsername(){ String username = this.username; return "%" + username + "%"; }
<select id="selectUseIf" parameterType="com.soft.test.model.DynamicTestModel" resultMap="userMap"> <if test="username != null and username neq ''"> <bind name="pattern" value=" _parameter.handleUsername() " /> </if> select * from t_user <where> <if test="username != null and username neq ''"> username like #{pattern} </if> </where> </select>测试结果:
DEBUG [main] - ==> Preparing: select * from t_user WHERE username like ?
DEBUG [main] - ==> Parameters: %xiaohon%(String)
也是可以执行的。
注意点就是可执行的方法的对象必须实例化,否则空指针。
阅读全文
0 0
- mybatis学习之路----bind的简单使用
- boost之bind的使用
- std::bind,std::cref的简单使用
- mybatis 的简单使用
- mybatis简单的使用
- mybatis的简单使用
- MyBatis的简单使用
- MyBatis的简单使用
- 学习Mybatis:Mybatis的使用
- Javascript学习之bind的模拟实现
- Boost库之bind的使用
- Mybatis使用之简单的增删改查
- jQuery学习之bind
- boost学习之bind
- 安卓学习之路之ViewPager的简单使用
- swift学习之路之UITabBrarController的简单使用
- 简单的bind实现
- mybatis的foreach简单使用
- Bootstrap中点击后禁用按钮的最佳方法
- 常见注册
- 从零开始学习rocketmq(1)
- @Autowired写在成员变量上和set方法上的区别?
- ES6——Proxy(代理)、Reflect(反射)
- mybatis学习之路----bind的简单使用
- 3.web.xml配置文件
- 数据结构实验之栈与队列八:栈的基本操作
- 五分钟学GIS | 空间数据可视化渲染技术
- 5.leetCode:657. Judge Route Circle
- jdbctemplate方法在调用时 的相关配置以及注意事项
- 计算鼠标偏移量
- 算法学习-排序算法
- macOS系统下的文件夹加密