Mybatis学习笔记7 - select查询的相关属性使用
1、当接口的返回类型是集合List时,resultType要写集合中元素的类型
示例如下:
接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; import java.util.*; public interface EmployeeMapper { public List<Employee> getEmpByLastName(String lastName); } mapper定义: <?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.mybatis.dao.EmployeeMapper"> <select id="getEmpByLastName" resultType="com.mybatis.bean.Employee"> select * from tbl_employee where last_name like #{lastName} </select> </mapper> 测试代码: package com.mybatis.demo; import java.io.*; import java.util.*; import com.mybatis.bean.Employee; import com.mybatis.dao.EmployeeMapper; import org.apache.ibatis.io.*; import org.apache.ibatis.session.*; import org.junit.Test; public class MyTest { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testSelect() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(true); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); List<Employee> employees = mapper.getEmpByLastName("%e%"); for (Employee emp : employees) { System.out.println(emp); } } finally { openSession.close(); } } }
2、@MapKey注解:指定使用哪个实体的属性作为key
示例如下:
接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; import org.apache.ibatis.annotations.MapKey; import java.util.*; public interface EmployeeMapper { //返回一条记录的map;key就是列名,值就是对应的值 public Map<String, Object> getEmpByIdReturnMap(Integer id); //多条记录封装一个map:Map<Integer,Employee>:键是这条记录的主键,值是记录封装后的javaBean //@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key @MapKey("id") public Map<Integer, Employee> getEmpReturnMap(String lastName); } mapper定义: <?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.mybatis.dao.EmployeeMapper"> <!--public Map<String, Object> getEmpByIdReturnMap(Integer id);--> <select id="getEmpByIdReturnMap" resultType="map"> select * from tbl_employee where id=#{id} </select> <!--public Map<Integer, Employee> getEmpReturnMap(String lastName); --> <select id="getEmpReturnMap" resultType="com.mybatis.bean.Employee"> select * from tbl_employee where last_name like #{lastName} </select> </mapper> 测试代码: package com.mybatis.demo; import java.io.*; import java.util.*; import com.mybatis.bean.Employee; import com.mybatis.dao.EmployeeMapper; import org.apache.ibatis.io.*; import org.apache.ibatis.session.*; import org.junit.Test; public class MyTest { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testSelectOne() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(true); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Map<String, Object> employee = mapper.getEmpByIdReturnMap(1); System.out.println(employee); } finally { openSession.close(); } } @Test public void testSelectMany() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(true); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Map<Integer, Employee> employee = mapper.getEmpReturnMap("%e%"); System.out.println(employee); } finally { openSession.close(); } } }