1. if标签 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import java.util.HashMap;import java.util.Map;import org.apache.ibatis.session.SqlSession;public class Main { public static void main (String[] args) { SqlSession session=SqlSessionUtil.getSession(); StudentDao dao=session.getMapper(StudentDao.class); Map map=new HashMap (); map.put("name" , "a" ); map.put("startAge" , 21 ); map.put("endAge" , 30 ); System.out.println(dao.searchStudent(map)); SqlSessionUtil.close(SqlSessionUtil.getSession()); } }
StudentDao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace ="com.node.dao.StudentDao" > <select id ="searchStudent" resultType ="student" > select * from student where 1=1 <if test ="name != null and name.trim != ''" > and name like '%' #{name} '%' </if > <if test ="startAge != null and startAge > 20" > and age > #{startAge} </if > <if test ="endAge != null and 40 > endAge" > and #{endAge} > age </if > </select > </mapper >
2. where标签 StudentDao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace ="com.node.dao.StudentDao" > <select id ="searchStudent" resultType ="student" > select * from student where 1=1 <where > <if test ="name != null and name.trim!=''" > and name like '%' #{name} '%' </if > <if test ="startAge!=null and startAge>20" > and age > #{startAge} </if > <if test ="endAge!=null and 40>endAge" > and #{endAge} > age </if > </where > </select > </mapper >
3. choose标签 StudentDao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace ="com.node.dao.StudentDao" > <select id ="searchStudent" resultType ="student" > select * from student <where > <choose > <when test ="name!=null and name.trim!=''" > name like '%' #{name} '%' </when > <when test ="startAge!=null and startAge>20" > age>#{startAge} </when > <when test ="endAge!=null and 40>endAge" > #{endAge}>age </when > <otherwise > 1=1 </otherwise > </choose > </where > </select > </mapper >
4. foreach标签 collection是这里面比较难得下面我们详细介绍一下实际中的运用:
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可
如果传入的参数是多个的时候,我们也可以放在实体类中(这种实际用到也是非常多的)
(1) foreach实现in查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSession;public class Main { public static void main (String[] args) { SqlSession session=SqlSessionUtil.getSession(); StudentDao dao=session.getMapper(StudentDao.class); List<Integer> list=new ArrayList <Integer>(); list.add(21 ); list.add(23 ); list.add(24 ); list.add(26 ); System.out.println(dao.searchStudentInAge(list)); SqlSessionUtil.close(SqlSessionUtil.getSession()); } }
StudentDao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace ="com.node.dao.StudentDao" > <select id ="searchStudentInAge" resultType ="student" > select * from student <where > <if test ="list!=null and list.size>0" > age in <foreach collection ="list" item ="obj" open ="(" close =")" separator ="," > #{obj} </foreach > </if > </where > </select > </mapper >
(2) foreach实现批量插入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSession;public class Main { public static void main (String[] args) { SqlSession session=SqlSessionUtil.getSession(); StudentDao dao=session.getMapper(StudentDao.class); List<Student> list=new ArrayList <Student>(); Student s1=new Student (); s1.setName("An" ); s1.setAge(36 ); Student s2=new Student (); s2.setName("Bn" ); s2.setAge(37 ); list.add(s1); list.add(s2); dao.batchInsertStudents(list); SqlSessionUtil.getSession().commit();; SqlSessionUtil.close(SqlSessionUtil.getSession()); } }
StudentDao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace ="com.node.dao.StudentDao" > <insert id ="batchInsertStudents" > insert into student(name,age) values <foreach collection ="list" item ="obj" separator ="," > (#{obj.name},#{obj.age}) </foreach > </insert > </mapper >
5. sql标签 StudentDao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace ="com.node.dao.StudentDao" > <sql id ="selectStudent" > select name,age from student </sql > <sql id ="selectStudentByIdWhere" > where id=#{xxx} </sql > <select id ="selectStudentAll" resultType ="Student" > <include refid ="selectStudent" /> </select > <select id ="selectStudentById" resultType ="Student" > <include refid ="selectStudent" /> <include refid ="selectStudentByIdWhere" /> </select > </mapper >