1. selectOne() selectOne()方法只能返回1条或者0条记录
StudentDaoImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.util.List;import org.apache.ibatis.session.SqlSession;public class StudentDaoImpl implements StudentDao { @Override public Student selectStudentById (Long id) { SqlSession session=SqlSessionUtil.getSession(); Student student=session.selectOne("selectStudentById" , id); SqlSessionUtil.close(session); return student; } }
StudentDao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?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 ="test" > <select id ="selectStudentById" resultType ="student" > select * from student where id=#{xxx} </select > </mapper >
2. selectList() selectList(): 查询多条记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.util.List;import org.apache.ibatis.session.SqlSession;public class StudentDaoImpl implements StudentDao { @Override public List<Student> selectStudentAll () { SqlSession session=SqlSessionUtil.getSession(); List<Student> list=session.selectList("selectStudentAll" ); SqlSessionUtil.close(session); return list; } }
StudentDao.xml
1 2 3 4 5 6 7 8 9 10 11 12 <?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 ="test" > <select id ="selectStudentAll" resultType ="student" > select * from student </select > </mapper >
3. 查询多条记录返回Map 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSession;public class StudentDaoImpl implements StudentDao { @Override public Map<String, Student> selectStudentAll () { SqlSession session=SqlSessionUtil.getSession(); Map<String, Student> map=session.selectMap("selectStudentAll" , "name" ); SqlSessionUtil.close(session); return map; } }
StudentDao.xml
1 2 3 4 5 6 7 8 9 10 11 12 <?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 ="test" > <select id ="selectStudentAll" resultType ="student" > select * from student </select > </mapper >
返回值
{ , , ,}
4. 模糊查询 StudentDaoImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 import java.util.List;import org.apache.ibatis.session.SqlSession;public class StudentDaoImpl implements StudentDao { public List<Student> selectStudentLikeName1 (String name) { SqlSession session=SqlSessionUtil.getSession(); List<Student> list=session.selectList("selectStudentLikeName" , name); SqlSessionUtil.close(session); return list; } }
StudentDao.xml
1.使用数据库提供的字符串拼接函数,使用#{}预编译形式实现模糊查询
1 2 3 4 5 6 7 8 9 <?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 ="test" > <select id ="selectStudentLikeName" resultType ="student" > select * from student where name like concat('%',#{xxx},'%') </select > </mapper >
2.直接使用预编译实现模糊查询(推荐使用)
注意:#{}的前后必须拥有空格,否则查询失败
StudentDao.xml
1 2 3 4 5 6 7 8 9 <?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 ="test" > <select id ="selectStudentLikeName" resultType ="student" > select * from student where name like '%' #{name} '%' </select > </mapper >
3.${}:单纯的字符串拼接
如果参数对象为基本数据格式,那么只能填写value,如果参数对象为实体类,则需要填写属性名
注: 由于${}是字符串拼接,没有使用预编译,因此会导致SQL注入攻击,并且执行效率低,因此我们不推荐使用
StudentDao.xml
1 2 3 4 5 6 7 8 9 10 <?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 ="test" > <select id ="selectStudentLikeName" resultType ="student" > select * from student where name like '%${value}%' </select > </mapper >
#{}:
预编译
基本数据类型#{xxx},任意名称
${}
字符串拼接
基本数据类型${value}
执行效率低,容易导致SQL注入攻击
5. 使用Map集合作为查询条件 StudentDaoImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSession;public class StudentDaoImpl implements StudentDao { @Override public List<Student> selectStudentByMap (Map map) { SqlSession session=SqlSessionUtil.getSession(); List<Student> list=session.selectList("selectStudentByMap" , map); SqlSessionUtil.close(session); return list; } public static void main (String[] args) { StudentDao dao=new StudentDaoImpl (); Map map=new HashMap (); map.put("name" , "a" ); System.out.println(dao.selectStudentByMap(map)); } }
StudentDao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 <?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 ="test" > <select id ="selectStudentByMap" resultType ="student" > select * from student where name like '%' #{name} '%' </select > </mapper >
#{param}
属性名
xxx,任意
map的key