Mybatis 的映射文件中,前面我们的SQL都是比较简单的,有些时候业务逻辑复杂时,我们的 SQL是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了。
标签我们根据实体类的不同取值,使用不同的SQL语句来进行查询。比如在 id 如果不为空时可以根据 id查询,如果name不同空时还要加入用户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。
Emp findByIdAndName(@Param("id") Integer id, @Param("name") String name);
package com.dfbz.test;import com.dfbz.dao.EmpDao;
import com.dfbz.entity.Condition;
import com.dfbz.entity.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;/*** @author lscl* @version 1.0* @intro:*/public class Demo01 {private SqlSessionFactory factory;private SqlSession session;@Beforepublic void before() throws IOException {InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();factory = builder.build(is);session = factory.openSession(true); }@Afterpublic void after() throws IOException { session.close();}@Testpublic void test1() throws Exception{Emp emp = empDao.findByIdAndName(1, "张三");System.out.println(emp);}
}
标签为了简化上面 where 1=1
的条件拼装,我们可以采用
标签来简化开发。
Tips:where只能帮我们去除前and,不能帮助我们去除后and(where标签除了可以智能的去除and,也可以智能的去除or)
标签我们在进行范围查询时,就要将一个集合中的值,作为参数动态添加进来。这样我们将如何进行参数的递?
MyBatis在传递集合时,将集合的key设置为:collection(List集合、Set集合等),在mapper.xml中,使用#{collection}
就可以获取到集合的值,如果是List类型的集合,还可以使用#{list}
来获取到集合的值,如果是数组类型,那么需要使用#{array}
来获取值
List findByIds(List ids);
@Test
public void test2() {//创建idList ids = Arrays.asList(1, 2, 3);List empList = empDao.findByIds(ids);for (Emp emp : empList) {System.out.println(emp);}
}
List findByIds2(Set ids);List findByIds3(Integer[] ids);
@Test
public void test3() {HashSet ids = new HashSet<>(Arrays.asList(1, 2, 3));List empList2 = empDao.findByIds2(ids);List empList3 = empDao.findByIds3(new Integer[]{1,2,3});System.out.println(empList2);System.out.println(empList2);
}
组合标签choose 相当于 java 里面的 switch 语句。otherwise(其他情况)
List findByEmp(Emp emp);
@Test
public void test4() {Emp emp = new Emp();emp.setId(1);emp.setName("张%");List empList = empDao.findByEmp(emp);System.out.println(empList);
}
@Test
public void test4() {Emp emp = new Emp();emp.setId(1);emp.setName("张%");List empList = empDao.findByEmp(emp);System.out.println(empList);
}
标签set标签和where标签很类似,set标签主要是用在更新操作的时候,如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。
void update(Emp emp);
update empname = #{name}, age = #{age}, addr = #{addr}, salary = #{salary}, where id=#{id}
@Test
public void test5() {empDao.update(new Emp(1,"张三三",null,null,null));
}
Tips:与where标签不同的是,set标签能够去除多余的前后
,
标签trim标签的作用和where标签类似,不过trim标签可以定制化去除的内容和增加的内容,where标签只能增加where,去除and、or等;
属性 | 说明 |
---|---|
prefix | 给SQL语句拼接的前缀 |
suffix | 给SQL语句拼接的后缀 |
prefixOverrides | 去除的前缀 |
suffixOverrides | 去除的后缀 |
List findByIdOrName(@Param("id") Integer id,@Param("name") String name);
@Test
public void test6() {List empList = empDao.findByIdOrName(1, "张三");System.out.println(empList);
}
void update2(Emp emp);
update empname = #{name}, age = #{age}, addr = #{addr}, salary = #{salary}, where id=#{id}
@Test
public void test7() {empDao.update(new Emp(1, "张三三", null, null, null));
}
void save(Emp emp);
insert into empid, name, values#{id}, #{name},
@Test
public void test8() {empDao.save(new Emp(11,"小陈",null,null,null));
}
Sql 中可将重复的 sql提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的。我们先到EmpDao.xml
文件中使用
标签,定义出公共部分,如下:
select * from emp