Java 杂谈首页投稿(暂停使用,暂停投稿)程序员

5.平凡之路-查询结果集简单处理

2017-08-26  本文已影响0人  胖先森

说在前面的话 : 命名空间暂时约定-持久化类(实体Bean)的类名的全路径 com.shxt.model.Skill

一 简单查询结果集处理

1.查询结果集处理为Map类型

(1) 字段作为Map中的KEY值

<?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="命名空间,唯一的不能重复"> -->
<mapper namespace="com.shxt.model.Skill">
  <!-- 1.结果集处理后变成Map类型 -->
  <!-- 当你传递的是 "一个" 简单的数据类型的形参的时候,那么你的KEY值可以任意取名 -->
  <select id="getMap01" parameterType="_int" resultType="map">
    SELECT
        skill_id,skill_name,num
    FROM
        skill
    WHERE
        skill_id=#{asdfasdfasdf}
  </select>
</mapper>
public class 处理结果集为Map类型 {
    public static void main( String[] args ) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSqlSession();

            Map<String,Object> map =  sqlSession.selectOne(Skill.class.getName()+".getMap01", 2);

            System.out.println(map);
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
    }
}
映射文件或者核心配置文件错误

(2) 通过设置字段别名修改Map中的KEY值

   <select id="getMap02" parameterType="_int" resultType="map">
    SELECT
        skill_id skillId,skill_name as skillName,num as number
    FROM
        skill
    WHERE
        skill_id=#{id}
  </select>
public class 处理结果集为Map类型_通过给字段取别名方式 {
    public static void main( String[] args ) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSqlSession();

            Map<String,Object> map =  sqlSession.selectOne(Skill.class.getName()+".getMap02", 2);

            System.out.println(map);
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
    }

}

(3) 通过resultMap属性和resultMap标签组合,设置不同的KEY

  <!-- 只能对结果集(查询)操作进行处理 -->
  <resultMap type="map" id="MapResultMapper" autoMapping="true">
    <result column="skill_id" property="id"/>
    <result column="skill_name" property="name"/>
    <result column="num" property="num"/>
  </resultMap>
  <select id="getMap03" parameterType="_int" resultMap="MapResultMapper">
    SELECT
        skill_id,skill_name,num
    FROM
        skill
    WHERE
        skill_id=#{asdfasdfasdf}
  </select>

autoMapping="true" 默认值 按照字段进行映射,如果修改了后者覆盖前者
autoMapping="false" 设置哪些就映射哪些

public class 处理结果集为Map类型_通过resultMap标签处理结果集 {
    public static void main( String[] args ) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSqlSession();

            Map<String,Object> map =  sqlSession.selectOne(Skill.class.getName()+".getMap03", 2);

            System.out.println(map);
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
    }

}

2.返回单一值

  <select id="getCountSkill" resultType="int">
    SELECT count(*) FROM skill 
  </select>

返回一个字符串也是类似的 resultType="string"

public class 处理结果集返回一个整型 {
    public static void main( String[] args ) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSqlSession();

            Integer count =  sqlSession.selectOne(Skill.class.getName()+".getCountSkill");

            System.out.println(count);
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
    }

}

3.返回自定义类型对象

(1) 通过设置别名方式

public class Skill {
    private Integer skillId;
    private String skillName;
    private Integer num;
        //省略getter和setter方法
}
  <!-- 1.别名方式,属性和字段名一致,可以完成自动映射 -->
  <select id="getSkill01" 
    parameterType="int" 
    resultType="com.shxt.model.Skill">
     SELECT
        skill_id skillId,skill_name skillName,num
    FROM
        skill
    WHERE
        skill_id=#{id}
  </select>

字段别名和类中的属性名保持一致

public class 处理结果集为自定义类Skill {
    public static void main( String[] args ) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSqlSession();

            Skill s =  sqlSession.selectOne(Skill.class.getName()+".getSkill01", 2);

            System.out.println(s);
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
    }

}

(2) resultMap属性和resultMap标签组合方式

  <resultMap 
    type="com.shxt.model.Skill" 
    id="SkillResultMapper">
    <id column="skill_id" jdbcType="INTEGER" property="skillId" javaType="java.lang.Integer"/>
    <result column="skill_name" jdbcType="VARCHAR" property="skillName" javaType="java.lang.String"/>
    <result column="num" jdbcType="INTEGER" property="num" javaType="java.lang.Integer"/>
  </resultMap>
  <select id="getSkill02"  parameterType="int"  resultMap="SkillResultMapper"
   >
     SELECT
        skill_id ,skill_name ,num
    FROM
        skill
    WHERE
        skill_id=#{id}
  
  </select>
public class 处理结果集为自定义类Skill_resultMap标签处理 {
    public static void main( String[] args ) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSqlSession();

            Skill s =  sqlSession.selectOne(Skill.class.getName()+".getSkill02", 2);

            System.out.println(s);
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
    }

}

4.返回列表别名和resultMap标签处理

  <select id="getSkillList01" resultType="map">
    SELECT
         skill_id ,skill_name ,num
    FROM
        skill
  </select>

  <select id="getSkillList02" resultMap="SkillResultMapper">
    SELECT
         skill_id ,skill_name ,num
    FROM
        skill shxt
  </select>

resultType 返回结果集处理的内置类型
resultMap 去找之前定义的resultMap标签的ID

public class 处理结果集结合 {
    public static void main( String[] args ) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSqlSession();

            List<Map<String,Object>> list01= sqlSession.selectList(Skill.class.getName()+".getSkillList01");
            List<Skill> list02= sqlSession.selectList(Skill.class.getName()+".getSkillList02");

            System.out.println("list01:"+list01);
            System.out.println("list02:"+list02);
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
    }

}

因为我们知道要返回的是一个列表,所以我们使用的是selectList方法

2.sql标签提取公共部分

  <sql id="skill_columns">
   skill_id ,skill_name ,num
  </sql>
  <select id="getSkillList01" resultType="map">
    SELECT
        <include refid="skill_columns"/>
    FROM
        skill
  </select>
  <sql id="skill_columns_alias">
   ${vip}.skill_id ,${vip}.skill_name ,${vip}.num
  </sql>
  <select id="getSkillList02" resultMap="SkillResultMapper">
    SELECT
        <include refid="skill_columns_alias">
            <property name="vip" value="shxt"/>
        </include>
    FROM
        skill shxt
  </select>

sql标签不仅仅是处理字段,只要是公共的部分就可以提出来
可以查看官方文档

上一篇 下一篇

猜你喜欢

热点阅读