上一篇:(七)Mybatis传值中#{}和${}的区别,及别名机制
下一篇:(十)Mybatis之动态SQL
数据库:汽车表t_car
引⼊依赖:mysql驱动依赖、mybatis依赖、logback依赖、junit依赖。
引入配置文件:jdbc.properties、mybatis-config.xml、logback.xml
pojo类:Car
SqlSession工具类:SqlSessionUtil
都可以复制之前的
当查询的结果,有对应的实体类,并且查询结果只有⼀条时:
创建CarMapper接口,并添加方法
public interface CarMapper {
/**
* 根据id查询
* @param id
* @return
*/
Car selectById(Long id);
}
创建CarMapper.xml映射文件进行配置
<select id="selectById" resultType="car">
select
id,car_num as carNum,brand,guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from t_car where id = #{id}
</select>
测试程序
@Test
public void testSelectById(){
SqlSession session = SqlSessionUtil.getSession();
CarMapper mapper = session.getMapper(CarMapper.class);
Car car = mapper.selectById(13L);
System.out.println(car);
SqlSessionUtil.close(session);
}
结果
当查询的记录条数是多条的时候,必须使⽤集合接收。如果使用单个实体类接收会出现异常。
测试返回多条返回单个实体类
接口添加方法
Car selectAll();
映射文件配置
<select id="selectAll" resultType="car">
select
id,car_num as carNum,brand,guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from t_car
</select>
测试程序
@Test
public void testSelectAll(){
SqlSession session = SqlSessionUtil.getSession();
CarMapper mapper = session.getMapper(CarMapper.class);
Car car1 = mapper.selectAll();
System.out.println(car1);
SqlSessionUtil.close(session);
}
结果,发生异常,原因是Mybatis进行自动类型推断,发现你设置的返回值是单个的实体类就执行selectOne方法,然后发现最终返回结果是多条,就引发了异常
接口中的方法应改为
List<Car> selectAll();
测试程序改为
@Test
public void testSelectAll(){
SqlSession session = SqlSessionUtil.getSession();
CarMapper mapper = session.getMapper(CarMapper.class);
List<Car> cars = mapper.selectAll();
//Car car1 = mapper.selectAll();
//System.out.println(car1);
cars.forEach(car -> System.out.println(car));
SqlSessionUtil.close(session);
}
执行成功
当返回的数据,没有合适的实体类对应的话,可以采用Map集合接收。Mtbais底层会自动把查询结果的字段名做key,字段值做value进行存储
查询如果可以保证只有⼀条数据,则返回⼀个Map集合即可。
接口添加方法:
Map<String ,Object> slectByIdRetMap(Long id);
映射文件配置:
resultMap=“map”,这是因为mybatis内置了很多别名。
<select id="slectByIdRetMap" resultType="map">
select
id,car_num as carNum,brand,guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from t_car where id = #{id}
</select>
测试程序:
@Test
public void testSlectByIdRetMap(){
SqlSession session = SqlSessionUtil.getSession();
CarMapper mapper = session.getMapper(CarMapper.class);
Map<String, Object> map = mapper.slectByIdRetMap(13L);
System.out.println(map);
SqlSessionUtil.close(session);
}
查询结果条数大于等于1条数据,则可以返回⼀个存储Map集合的List集合。List
等同于List
接口添加方法:
List<Map<String ,Object>> selectAllRetListMap();
映射文件配置:
<select id="selectAllRetListMap" resultType="map">
select *
from t_car
</select>
测试程序:
@Test
public void testSelectAllRetListMap(){
SqlSession session = SqlSessionUtil.getSession();
CarMapper mapper = session.getMapper(CarMapper.class);
List<Map<String, Object>> listMaps = mapper.selectAllRetListMap();
listMaps.forEach(listMap -> System.out.println(listMap));
SqlSessionUtil.close(session);
}
拿Car的id做key,以后取出对应的Map集合时更方便。
Map集合的key一般是主键值,需要通过@MapKey注解标明
接口添加方法:
@MapKey("id")
Map<Long ,Map<String ,Object>> selectAllRetMap();
映射文件配置:
<select id="selectAllRetMap" resultType="map">
select *
from t_car
</select>
测试程序:
@Test
public void testSelectAllRetMap(){
SqlSession session = SqlSessionUtil.getSession();
CarMapper mapper = session.getMapper(CarMapper.class);
Map<Long, Map<String, Object>> longMapMap = mapper.selectAllRetMap();
System.out.println(longMapMap);
SqlSessionUtil.close(session);
}