Mybatis查询结果集用驼峰处理以及查询结果集输出-----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>
    <select id="selectAllByMapUnderCamelCase" resultType="Car">
        select
            *
        from
            t_car
    </select>
<!--    不要用count(某字段),因为字段为空,结果就少了一条-->
    <select id="selectTotal" resultType="java.lang.Long">
        select count(*) 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>
    <select id="selectAllByMapUnderCamelCase" resultType="Car">
        select
            *
        from
            t_car
    </select>
<!--    不要用count(某字段),因为字段为空,结果就少了一条-->
    <select id="selectTotal" resultType="java.lang.Long">
        select count(*) from t_car;
    </select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="jdbc.properties"/>
    <settings>
        <setting name="logImpl" value="SLF4J"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.powernode.mybatis.POJO"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.powernode.mybatis.mappers"/>
    </mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="jdbc.properties"/>
    <settings>
        <setting name="logImpl" value="SLF4J"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.powernode.mybatis.POJO"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.powernode.mybatis.mappers"/>
    </mappers>
</configuration>
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
{
    Long selectTotal();
    List<Car> selectAllByMapUnderCamelCase();
    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
{
    Long selectTotal();
    List<Car> selectAllByMapUnderCamelCase();
    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);
        });
        SqlSessionUtil.close(sqlSession);
    }
    @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);
        SqlSessionUtil.close(sqlSession);
    }
    @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);
        });
        SqlSessionUtil.close(sqlSession);
    }
    @org.junit.Test
    public void TestSelectAllByResultMap()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectAllByMapUnderCamelCase();
        carList.forEach(car -> {
            System.out.println(car);
        });
        SqlSessionUtil.close(sqlSession);
    }
    @org.junit.Test
    public void TestCount()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Long aLong = mapper.selectTotal();
        System.out.println(aLong);
        SqlSessionUtil.close(sqlSession);
    }
}

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);
        });
        SqlSessionUtil.close(sqlSession);
    }
    @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);
        SqlSessionUtil.close(sqlSession);
    }
    @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);
        });
        SqlSessionUtil.close(sqlSession);
    }
    @org.junit.Test
    public void TestSelectAllByResultMap()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List<Car> carList = mapper.selectAllByMapUnderCamelCase();
        carList.forEach(car -> {
            System.out.println(car);
        });
        SqlSessionUtil.close(sqlSession);
    }
    @org.junit.Test
    public void TestCount()
    {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Long aLong = mapper.selectTotal();
        System.out.println(aLong);
        SqlSessionUtil.close(sqlSession);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧约Alatus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值