Mybatis查询结果集返回信息使用ResultMap实现封装POJO类-----Mybatis框架
Mybatis查询结果集返回信息使用ResultMap实现封装POJO类-----Mybatis框架
·
<?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 * from t_car where id = #{id}; </select> <select id="selectAllCar" resultType="Map"> select * from t_car; </select> <!-- 专门定义一个结果映射,在这个结果映射中指定这个数据库字段名和java类的属性名的对应关系--> <resultMap id="carResultMap" type="Car"> <!-- 只要数据库表有主键,建议这里配置一个ID标签,非必须--> <!-- 可以提高mybatis的效率--> <!-- 如果非主键的property和columnName相同其实可以不写--> <id property="id" column="id"></id> <result property="carNum" column="car_num" javaType="String" jdbcType="VARCHAR"></result> <result property="brand" column="brand" javaType="String" jdbcType="VARCHAR"></result> <result property="guidePrice" column="guide_price" javaType="Double" jdbcType="DOUBLE"></result> <result property="produceTime" column="produce_time" javaType="String" jdbcType="VARCHAR"></result> <result property="carType" column="car_type" javaType="String" jdbcType="VARCHAR"></result> </resultMap> <!-- type属性用来指定POJO类的类名,ID属性,指定resultMap的唯一标识,这个ID将来要在select标签中使用--> <!-- resultMap后面的值是ResultMap的ID属性,才能实现对应关系--> <select id="selectAllByResultMap" resultMap="carResultMap"> select * from t_car; </select> <select id="selectMap" resultType="Map"> select * 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 * from t_car where id = #{id}; </select> <select id="selectAllCar" resultType="Map"> select * from t_car; </select> <!-- 专门定义一个结果映射,在这个结果映射中指定这个数据库字段名和java类的属性名的对应关系--> <resultMap id="carResultMap" type="Car"> <!-- 只要数据库表有主键,建议这里配置一个ID标签,非必须--> <!-- 可以提高mybatis的效率--> <!-- 如果非主键的property和columnName相同其实可以不写--> <id property="id" column="id"></id> <result property="carNum" column="car_num" javaType="String" jdbcType="VARCHAR"></result> <result property="brand" column="brand" javaType="String" jdbcType="VARCHAR"></result> <result property="guidePrice" column="guide_price" javaType="Double" jdbcType="DOUBLE"></result> <result property="produceTime" column="produce_time" javaType="String" jdbcType="VARCHAR"></result> <result property="carType" column="car_type" javaType="String" jdbcType="VARCHAR"></result> </resultMap> <!-- type属性用来指定POJO类的类名,ID属性,指定resultMap的唯一标识,这个ID将来要在select标签中使用--> <!-- resultMap后面的值是ResultMap的ID属性,才能实现对应关系--> <select id="selectAllByResultMap" resultMap="carResultMap"> select * from t_car; </select> <select id="selectMap" resultType="Map"> select * from t_car </select> </mapper>
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 { List<Car> selectAllByResultMap(); //查询所有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 { List<Car> selectAllByResultMap(); //查询所有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.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); } @org.junit.Test public void TestResultMap() { SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); List<Car> carList = mapper.selectAllByResultMap(); carList.forEach(car -> { System.out.println(car); }); } @org.junit.Test public void TestSelectAllByResultMap() { SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); } }
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); } @org.junit.Test public void TestResultMap() { SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); List<Car> carList = mapper.selectAllByResultMap(); carList.forEach(car -> { System.out.println(car); }); } @org.junit.Test public void TestSelectAllByResultMap() { SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); } }
更多推荐
所有评论(0)