mybatis输入映射(包装类型)+输出映射+动态sql

1.输入映射——包装类型

通过parameterType指定输入参数的类型,类型可以是简单类型,hashmap,pojo包装类型
需求:查询用户综合信息,需要传入查询条件(可能包括用户信息,其他信息)

AdminBean.java

package cn.huan.mybatis;

/**
 * Created by 马欢欢 on 2017/6/29.
 */
public class AdminBean {
    private int a_id;
    private String a_nameid;
    private String a_username;
    private String a_password;
    private int a_trank;

    @Override
    public String toString() {
        return "AdminBean{" +
                "a_id=" + a_id +
                ", a_nameid='" + a_nameid + '\'' +
                ", a_username='" + a_username + '\'' +
                ", a_password='" + a_password + '\'' +
                ", a_trank=" + a_trank +
                '}';
    }

    public int getA_id() {
        return a_id;
    }

    public void setA_id(int a_id) {
        this.a_id = a_id;
    }

    public String getA_nameid() {
        return a_nameid;
    }

    public void setA_nameid(String a_nameid) {
        this.a_nameid = a_nameid;
    }

    public String getA_username() {
        return a_username;
    }

    public void setA_username(String a_username) {
        this.a_username = a_username;
    }

    public String getA_password() {
        return a_password;
    }

    public void setA_password(String a_password) {
        this.a_password = a_password;
    }

    public int getA_trank() {
        return a_trank;
    }

    public void setA_trank(int a_trank) {
        this.a_trank = a_trank;
    }
}

AdminCustom.java //可扩展用户信息

package cn.huan.mybatis;

/**
 * Created by 马欢欢 on 2017/6/30.
 */
public class AdminCustom extends AdminBean {
    //可扩展用户信息
}

AdminQuerVo.java

package cn.huan.mybatis;

/**
 * Created by 马欢欢 on 2017/6/30.
 */
public class AdminQuerVo {
    private AdminCustom adminCustom;

    public AdminCustom getAdminCustom() {
        return adminCustom;
    }

    public void setAdminCustom(AdminCustom adminCustom) {
        this.adminCustom = adminCustom;
    }
}

mapper.xml映射文件


    

接口AdminMapper.java

/**
     * 用户信息综合查询
     * @param adminQuerVo
     * @return
     * @throws Exception
     */
    public List findUserList(AdminQuerVo adminQuerVo) throws Exception;

测试实现

package cn.mapper;

import cn.huan.mybatis.AdminBean;
import cn.huan.mybatis.AdminCustom;
import cn.huan.mybatis.AdminQuerVo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by 马欢欢 on 2017/6/29.
 */
public class AdminMapperTest {
    private SqlSessionFactory sqlSessionFactory;
    @Before
    //测试前执行
   public void setUp() throws IOException {
       //配置文件
       String resource = "mybatis-config.xml";
       //的到配置文件流
       InputStream inputStream =  Resources.getResourceAsStream(resource);
       //创建会话工厂
       sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
   }

    @Test
    public void testFindUserList() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建AdminMapper对象,mybatis自动生成mapper代理对象
        AdminMapper adminMapper = sqlSession.getMapper(AdminMapper.class);

        AdminQuerVo adminQuerVo = new AdminQuerVo();
        AdminCustom adminCustom = new AdminCustom();
        adminCustom.setA_username("员");
        adminCustom.setA_trank(3);
        adminQuerVo.setAdminCustom(adminCustom);

        List list = adminMapper.findUserList(adminQuerVo);
        System.out.println(list);
    }
}

mybatis输入映射(包装类型)+输出映射+动态sql_第1张图片

2.输出映射:

a.resultType

  • 使用resultType进行输出映射,只有查询出来的列明和pojo中的属性名一致,该列才可以映射成功
  • 如果查询出来的列名和pojo中的属性名全不一致,没有创建pojo对象。
  • 只要查询出来的列名和pojo属性有一个一致,就会创建pojo对象。

需求:用户信息的综合查询列表总数,通过查询总数和上边用户综合查询列表才可以实现分页。

mapper.xml映射文件

  

接口AdminMapper.java

    public int findUserCount(AdminQuerVo adminQuerVo) throws Exception;

测试实现

package cn.mapper;

import cn.huan.mybatis.AdminBean;
import cn.huan.mybatis.AdminCustom;
import cn.huan.mybatis.AdminQuerVo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by 马欢欢 on 2017/6/29.
 */
public class AdminMapperTest {
    private SqlSessionFactory sqlSessionFactory;
    @Before
    //测试前执行
   public void setUp() throws IOException {
       //配置文件
       String resource = "mybatis-config.xml";
       //的到配置文件流
       InputStream inputStream =  Resources.getResourceAsStream(resource);
       //创建会话工厂
       sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
   }


    @Test
    public void testFindUserCount() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建AdminMapper对象,mybatis自动生成mapper代理对象
        AdminMapper adminMapper = sqlSession.getMapper(AdminMapper.class);

        AdminQuerVo adminQuerVo = new AdminQuerVo();
        AdminCustom adminCustom = new AdminCustom();
        adminCustom.setA_username("员");
        adminCustom.setA_trank(3);
        adminQuerVo.setAdminCustom(adminCustom);

        int count= adminMapper.findUserCount(adminQuerVo);
        System.out.println(count);
    }
}


mybatis输入映射(包装类型)+输出映射+动态sql_第2张图片
  • 小结:查询出来的结果集只有一行且一列,可以使用简单类型进行输出映射.

b.resultMap使用方法

如果查询出来的列名和pojo的属性名不一致。通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。
1.定义 resultMap
2.使用 resultMap作为输出映射类型

需求: 将 下面的sql语句用 AdminBean完成映射
SELECT a_nameid id_ ,a_username name_ FROM admin WHERE a_nameid=#{value}

1.定义resultMap
将查询结果 和AdminBean做映射



    
        
        
        
        
    

  

接口AdminMapper.java

   public AdminBean findUserByResultMmap(int id) throws Exception;

测试类:

@Test
    public void testFindUserByResultMmap() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建AdminMapper对象,mybatis自动生成mapper代理对象
        AdminMapper adminMapper = sqlSession.getMapper(AdminMapper.class);

        AdminBean adminBean= adminMapper.findUserByResultMmap(10);
        System.out.println(adminBean);
    }
mybatis输入映射(包装类型)+输出映射+动态sql_第3张图片

3.动态sql语句

a.动态sql语句--if判断 及where

 
    

b.动态sql片段

定义


            
                
                    and a_trank=#{adminCustom.a_trank}
                
                
                    and a_username like '%${adminCustom.a_username}%'
                
            
       
    

引用

 
    

c.sql--foreach
需求:
SELECT a_nameid id_ ,a_username name_ FROM admin WHERE a_username like '%${value}%' and( a_id=1 OR a_id=2);
SELECT a_nameid id_ ,a_username name_ FROM admin WHERE a_id IN(1,2,3);



    
    

上一篇:mybatis全局配置文件mybatis-config.xml

文集:mybatis框架学习

你可能感兴趣的:(mybatis输入映射(包装类型)+输出映射+动态sql)