1.动态查询(动态生成Where语句):
这篇文章中叙述的功能,统一使用下表测试:
CREATE TABLE `test_order_detail_mm` ( `id` int(20) NOT NULL AUTO_INCREMENT, `order_id` int(20) NOT NULL, `goods_name` varchar(50) DEFAULT NULL, `single_price` decimal(19,2) DEFAULT NULL, `num` int(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
(1)我们在写SELECT语句时,有时候会遇到有不定数量个where条件,一般我们在处理时,需要动态的拼写sql语句,在Mybatis中使用动态sql,where标签来处理这个问题。
(2)配置mapper文件:
<select id="dynamicSelect" parameterType="com.vip.model.TestOrderDetailMm" resultMap="orderDetailResultMap"> select * from test_order_detail_mm <where> <if test="id!=0">id = #{id}</if> <if test="orderId!=0">AND order_id = #{orderId}</if> <if test="goodsName!=null">AND goods_name like ‘%${goodsName}%‘ </if> <if test="singlePrice!=0">AND single_price = #{singlePrice}</if> <if test="num!=0">AND num = #{num}</if> </where> </select>
where元素的作用是会在写入where元素的地方输出一个where,并且mybatis会智能地根据if条件来输出where条件。如上例,假如orderId=0那么“AND order_id = #{orderId}”这条语句将不会输出。另外假如第一个标签id不符合,第一个where条件就变成了“AND order_id = #{orderId}”,即使这样,前面也不会加and。
(3)测试类:
@Test public void dynamicSelect() { SqlSession session = sqlSessionFactory.openSession(); //创建查询对象 TestOrderDetailMm tst = new TestOrderDetailMm(); tst.setSinglePrice(198.5); tst.setNum(2); //执行动态查询 List<TestOrderDetailMm> lst = session.selectList("com.vip.mapping.TestOrderDetailMm.dynamicSelect", tst); System.out.println(lst); }
如上例中,我们只设置了2个查询条件,SinglePrice和num 两个条件,其余属性未设置。这块需要注意,对于String类型的属性,如果未set则为null,如果是int或double,未set则为0。
(4)运行结果:
DEBUG - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@3d74bf60] DEBUG - ==> Preparing: select * from test_order_detail_mm WHERE single_price = ? AND num = ? DEBUG - ==> Parameters: 198.5(Double), 2(Integer) [TestOrderDetailMm [id=2, orderId=1, goodsName=衣服, singlePrice=198.5, num=2], TestOrderDetailMm [id=3, orderId=1, goodsName=衣服, singlePrice=198.5, num=2]]
如上边的运行日志,在where条件中,只有2个条件single_price和num,其余我们未设置的查询条件(属性)没有放在Where条件中。
2.动态更新(动态生成update set语句):
动态update,与动态查询类似。我们对某个属性设置值后,只update这个属性,而不会将其他属性设置成null或者0。
(1)Mapper文件:
<update id="dynamicUpdate" parameterType="com.vip.model.TestOrderDetailMm" > update test_order_detail_mm <set> <if test="id!=0">id = #{id},</if> <if test="orderId!=0">order_id = #{orderId},</if> <if test="goodsName!=null">goods_name = #{goodsName}, </if> <if test="singlePrice!=0">single_price = #{singlePrice},</if> <if test="num!=0">num = #{num}</if> </set> where id = #{id} </update>
(2)测试类:
@Test public void dynamicUpdate() { SqlSession session = sqlSessionFactory.openSession(); //创建查询对象 TestOrderDetailMm tst = new TestOrderDetailMm(); tst.setId(2); tst.setGoodsName("遥控车"); //执行动态查询 session.update("com.vip.mapping.TestOrderDetailMm.dynamicUpdate", tst); session.close(); }
(3)运行结果:
DEBUG - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@31368b99] DEBUG - ==> Preparing: update test_order_detail_mm SET id = ?, goods_name = ? where id = ? DEBUG - ==> Parameters: 2(Integer), 遥控车(String), 2(Integer)
如上运行结果,我们只update了goods_name字段,没有修改其他字段。
3.实现in操作:
4.实现in操作:
5.批量插入:
6.批量删除:
本文出自 “雪花” 博客,请务必保留此出处http://6216083.blog.51cto.com/6206083/1842794
原文地址:http://6216083.blog.51cto.com/6206083/1842794