(四)resultMap、sql片段与动态SQL

一、resultMap

   1.1 为什么要用resultMap

  • resultType:指定输出结果的类型(pojo、简单类型、hashmap),将SQL查询结果映射为Java对象。
    使用resultType注意:sql查询的列名要和resultType指定pojo的属性相同,指定相同,属性方可映射成功。如果sql查询的列名要和resultType指定pojo的属性全部不相同,list中是无法创建pojo对象的。有几个属性对应相同,则能给对应相同的属性赋值。
    resultMap:将SQL查询结果映射为Java对象。如果SQL查询列名和最终要映射的pojo的属性名不一致,使用resultMap将列名和pojo的属性名做一个映射关系(列名和属性名映射配置)。
  • 举例来讲,我们要查询学生记录,学生的pojo对象中的学生名为name,而数据库中的学生名字段为UserName,那么使用resultType则无法将查询到的学生记录映射到学生pojo对象中,因为属性名和字段名必须一致。
  • 解决方法如下:
  • <resultMap type="com.shyroke.entity.User" id="userMap">
            <id column="id" property="id" />
            <result column="userName" property="userName" />
            <result column="passWord" property="passWord" />
        </resultMap>
    
    
    column的值对应着property的值,即数据库字段(column)对应着pojo对象的属性(property)。
     

二、SQL片段

 2.1 案例一

UserMapper.java
package com.shyroke.mapper;

import java.util.List;


import com.shyroke.entity.User;

public interface UserMapper {
 public List<User> findUserByName(String userName);
}

UserMapper.xml

<?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.shyroke.mapper.UserMapper">

    <resultMap type="com.shyroke.entity.User" id="userMap">
        <id column="id" property="id" />
        <result column="userName" property="userName" />
        <result column="passWord" property="passWord" />
    </resultMap>

    <sql id="user_where">
        <if test="_parameter !=null and _parameter.length()>0">
            and userName like '%${_parameter}%'
        </if>
    </sql>


    <select id="findUserByName" parameterType="string" resultMap="userMap">
        select * from user
        <where>
            <include refid="user_where"></include>
        </where>
    </select>

</mapper>
  •  where标签相当于where关键字,可以自动除去第一个and。
  • 引用sql片段,如果sql片段和引用处不在同一个mapper 必须在前边加namespace.

App.java:

package com.shyrolk.firstMybatis;

import java.awt.image.ImageProducer;
import java.io.InputStream;
import java.util.List;

import javax.swing.plaf.synth.SynthSeparatorUI;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.shyroke.entity.User;
import com.shyroke.entity.User2;
import com.shyroke.mapper.UserMapper;


/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {

        InputStream inputStream=App.class.getClassLoader().getResourceAsStream("resource/mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        
        List<User> userList=userMapper.findUserByName("min");
        System.out.println(userList);
        sqlSession.close();
    }
}

结果:

 

 2.2 foreach

  • 在statement中通过foreach遍历parameterType中的集合类型
  • 需求:根据多个用户id查询用户信息
  •  案例:UserMapper.java
package com.shyroke.mapper;

import java.util.List;


import com.shyroke.entity.User;

public interface UserMapper {
 
 public List<User> findUserListById(List<Integer> idList);
}

UserMapper.xml:

<?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.shyroke.mapper.UserMapper">

    <resultMap type="com.shyroke.entity.User" id="userMap">
        <id column="id" property="id" />
        <result column="userName" property="userName" />
        <result column="passWord" property="passWord" />
    </resultMap>

    <select id="findUserListById" resultMap="userMap" >
        select * from user

        <where>
            <foreach collection="list" open="id in(" close=")" item="id"
                separator=",">
                #{id}
            </foreach>

        </where>
    </select>


</mapper>
    1.  collection: pojo中的表示集合的属性  
    2.                 open: 开始循环拼接的串  
    3.                 close: 结束循环拼接的串  
    4.                 item: 每次循环从集合中取到的对象  
    5.                 separator: 没两次循环中间拼接的串
    6. index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置

App.java:

package com.shyrolk.firstMybatis;

import java.awt.image.ImageProducer;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.plaf.synth.SynthSeparatorUI;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.shyroke.entity.User;
import com.shyroke.entity.User2;
import com.shyroke.mapper.UserMapper;


/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {

        InputStream inputStream=App.class.getClassLoader().getResourceAsStream("resource/mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        
        List<Integer> idList=new ArrayList<Integer>();
        idList.add(2);
        idList.add(4);
        List<User> userList=userMapper.findUserListById(idList);
        System.out.println(userList);
        sqlSession.close();
    }
}

结果:

 

posted @ 2017-09-27 16:15  shyroke、  阅读(535)  评论(0编辑  收藏  举报
作者:shyroke 博客地址:http://www.cnblogs.com/shyroke/ 转载注明来源~