package com.powernode.mybatis.mappers;
import com.powernode.mybatis.POJO.Car;
import org.apache.ibatis.annotations.MapKey;
import java.util.List;
import java.util.Map;
public interface CarMapper
{
//查询所有Car,返回一个Map集合
//map集合的Key是主键值,map值是Car对象
@MapKey("id")
Map<Long,Map<String,Object>> selectMap();
List<Map<String,Object>> selectAllCar();
Map<String,Object> selectByIdGetMap(Long id);
List<Car> selectByCarId(Long id);
//模糊查询的对象可能有多个,会出问题吗?
Car selectByBrand(String brand);
List<Car> selectAll();
Car selectById(Long id);
}
package com.powernode.mybatis.mappers;
import com.powernode.mybatis.POJO.Car;
import org.apache.ibatis.annotations.MapKey;
import java.util.List;
import java.util.Map;
public interface CarMapper
{
//查询所有Car,返回一个Map集合
//map集合的Key是主键值,map值是Car对象
@MapKey("id")
Map<Long,Map<String,Object>> selectMap();
List<Map<String,Object>> selectAllCar();
Map<String,Object> selectByIdGetMap(Long id);
List<Car> selectByCarId(Long id);
//模糊查询的对象可能有多个,会出问题吗?
Car selectByBrand(String brand);
List<Car> selectAll();
Car selectById(Long id);
}
<?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.powernode.mybatis.mappers.CarMapper">
<select id="selectById" resultType="Car">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
id = #{id};
</select>
<select id="selectAll" resultType="Car">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
</select>
<select id="selectByBrand" resultType="Car">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
brand like '%${brand}%';
</select>
<select id="selectByCarId" resultType="Car">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
id = #{id};
</select>
<!-- 查询结果没有合适的接收对象,没有合适的POJO类-->
<select id="selectByIdGetMap" resultType="java.util.Map">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
id = #{id};
</select>
<select id="selectAllCar" resultType="Map">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
</select>
<select id="selectMap" resultType="Map">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
</select>
</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.powernode.mybatis.mappers.CarMapper">
<select id="selectById" resultType="Car">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
id = #{id};
</select>
<select id="selectAll" resultType="Car">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
</select>
<select id="selectByBrand" resultType="Car">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
brand like '%${brand}%';
</select>
<select id="selectByCarId" resultType="Car">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
id = #{id};
</select>
<!-- 查询结果没有合适的接收对象,没有合适的POJO类-->
<select id="selectByIdGetMap" resultType="java.util.Map">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
where
id = #{id};
</select>
<select id="selectAllCar" resultType="Map">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
</select>
<select id="selectMap" resultType="Map">
select
id as id,
car_num as carNum,
brand as brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from
t_car
</select>
</mapper>
package com.powernode.mybatis.Test;
import com.powernode.mybatis.POJO.Car;
import com.powernode.mybatis.mappers.CarMapper;
import com.powernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
import java.util.Map;
public class Test
{
@org.junit.Test
public void TestPojoSelect()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Car car = mapper.selectById(5l);
System.out.println(car);
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestSelectAll()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
List<Car> carList = mapper.selectAll();
carList.forEach(car -> {
System.out.println(car);
});
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestBrand()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
//会报错,因为返回了多条记录了
Car car = mapper.selectByBrand("比亚迪");
System.out.println(car);
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestSelectByCarId()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
List<Car> carList = mapper.selectByCarId(5L);
carList.forEach(car -> {
System.out.println(car);
});
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestSelectByIdGetMap()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Map<String, Object> car = mapper.selectByIdGetMap(5l);
System.out.println(car);
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestselectAllCar()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
List<Map<String, Object>> maps = mapper.selectAllCar();
maps.forEach(map ->{
System.out.println(map);
});
}
@org.junit.Test
public void TestMap()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Map<Long, Map<String, Object>> longMapMap = mapper.selectMap();
System.out.println(longMapMap);
Map<String, Object> map = longMapMap.get(3L);
System.out.println(map);
}
}
package com.powernode.mybatis.Test;
import com.powernode.mybatis.POJO.Car;
import com.powernode.mybatis.mappers.CarMapper;
import com.powernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
import java.util.Map;
public class Test
{
@org.junit.Test
public void TestPojoSelect()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Car car = mapper.selectById(5l);
System.out.println(car);
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestSelectAll()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
List<Car> carList = mapper.selectAll();
carList.forEach(car -> {
System.out.println(car);
});
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestBrand()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
//会报错,因为返回了多条记录了
Car car = mapper.selectByBrand("比亚迪");
System.out.println(car);
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestSelectByCarId()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
List<Car> carList = mapper.selectByCarId(5L);
carList.forEach(car -> {
System.out.println(car);
});
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestSelectByIdGetMap()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Map<String, Object> car = mapper.selectByIdGetMap(5l);
System.out.println(car);
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestselectAllCar()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
List<Map<String, Object>> maps = mapper.selectAllCar();
maps.forEach(map ->{
System.out.println(map);
});
}
@org.junit.Test
public void TestMap()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Map<Long, Map<String, Object>> longMapMap = mapper.selectMap();
System.out.println(longMapMap);
Map<String, Object> map = longMapMap.get(3L);
System.out.println(map);
}
}