Mybatis_动态SQL(2)

1、内置参数
_parameter:代表sql语句的整个参数,若参数为封装的Map,则此时该参数代表整个Map

_databaseId:若是全局配置文件中制定了dataBaseId,则可以在sql语句中根据判断 _databaseId的取值,从而操作不同的数据库,而不需要写两条sql语句来操作不同的数据库。

2、bind标签
将OGNL表达式的值绑定到一个变量中,方便后续引用。例如,实现姓名的模糊匹配:

最基本方法是将模糊匹配表达式作为参数传入:
select * from stu where name like #{name}<参数中name的值为"%e%">
上述方法可以修改为在sql中拼接,写成$拼接动态字符串:
select * from stu where name like '%${name}%'
使用bind拼接,则可以修改为:
<bind name="_name" value="'%'+name+'%'">
select * from stu where name like #{_name}

3、sql片段抽取-include引用
sql标签:
抽取常用的列表(查询、插入等),方便后续引用
include标签:
引用外部已抽取的sql片段,指定refid。includ标签中可自定义属性property,在sql判断中使用${property}引用自定义属性。

4、针对前面这里写链接内容的动态SQL问题代码的解释
(1)

<!--  如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
-->

/*sql映射文件中使用array类型,测试类中报错如下:
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'array' not found. Available parameters are [collection, list]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'array' not found. Available parameters are [collection, list]*/

<!--据此,将collection类型修改为collection,成功了-->
 <select id="getStusByCondForeach" resultType="com.taozi.mybatis.bean.Student" >
  select * from stu where id in
 <foreach collection="array" item="item_id"  open="(" close=")" separator=","> 
      #{item_id}
  </foreach>
  </select> 

ArrayList<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(3);
List<Student> stus = mapper.getStusByCondForeach(ids);
for (Student student : stus) {
    System.out.println(student);
}

(2)

<!-- 当查询的参数有多个时,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称-->

/*此前的sql映射文件中写错了select语句id号,导致未找到对应的sql映射语句,正确修改后就成功了*/

(3)

<!--使用foreach实现批量插入-->
<!-- public void insertStuForeach(List<Student> stus); -->
<insert id="insertStuForeach" parameterType="list">
  <foreach collection="list" item="item_stu" >
     insert into stu values (#{item_stu});
  </foreach>
  </insert> 


List<Student> params = null;
Student stu1 = new Student(10, "test_name1", 'f', new Classes(1,"test"));
params.add(stu1);
mapper.insertStuForeach(params);

<!--此时报空指针异常,经检查,增加注解参数stus,修改参数创建方法,成功增加数据-->
/*sql映射接口*/
public void insertStuForeach(@Param("stus")List<Student> stus);

/*sql映射文件*/
<insert id="insertStuForeach" parameterType="list">
   insert into stu (id,name,gender,class_id) values 
  <foreach collection="stus" item="stu" separator="," >
    (#{stu.id},#{stu.name},#{stu.Sex},#{stu.banji.id})     
  </foreach>
  </insert>  
/*测试方法*/
List<Student> params = new ArrayList();
Student stu1 = new Student(15, "test_name5", 'f', new Classes(1,"test"));
Student stu2 = new Student(16, "test_name6", 'f', new Classes(1,"test"));
params.add(stu1);
params.add(stu2);

mapper.insertStuForeach(params);
session.commit();

补充:
mysql批量更新需要开启allowMultiQueries=true属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值