mybatis动态sql以及reslutType和resultMap详解

全局配置文件,以及sql.properties



<configuration>
  
    <properties resource="com/leige/config/jdbcInfo.properties">properties>
 
    <typeAliases>
     
        <typeAlias alias="Student" type="com.leige.domain.Student"/>

        <typeAlias alias="StudentQueryVo" type="com.leige.domain.StudentQueryVo" />
    typeAliases>

  <environments default="development">
    <environment id="development">

      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      dataSource>
    environment>
  environments>


  <mappers>
    <mapper resource="com/leige/domain/Student.xml"/>
  mappers>
configuration>

jdbc配置

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\:///test
username=root
password=

首先介绍resultType和resultMap区别:

简单介绍下resultType和resultMap的区别: resultType可以理解为实现po类属性到数据库表的映射,不支持别名查询
即使用 select sid sid_,age age_,name sname from student where
sid=1时,如果使用resultType接收结果, 会发生数据丢失即sid,age,name无法注入到对象中
这时候可以使用resultMap 1:使用resultMap之前要先定义resultMap column:数据列名
property:po类属性名 例如:

<resultMap type="Student" id="studentMap">

<id column="sid_" property="sid" />
<result column="age_" property="age"/>
<result column="name_" property="name" />
resultMap>

动态sql在实际应用代码中解释*
**当我们需要传入多个sid,查询多个对象或者对象属性组合查询?如何使mybatis满足要求
这就需要使用条件了这时我们可以定义一个对象,包含所有的查询条件StudentQueryVo,使用sql片段来实现,动态的拼接sql语句
注意:
1:定义sql片段时尽量使用单表sql,这样可重用性才高
2:sql片段不要包含where****

实体类:

``
public class Student {
private Integer sid;
private String name;
private Integer age;
setter..
getter...
toString....
}

*StudentQueryVo查询类:*

public class StudentQueryVo {
    //集合类型的值实例化,可以根据使用习惯在内部还是外部
    //传入多个sid查询多个对象
    private List<Integer> sids=new ArrayList<Integer>();
    //传入多个name查询多个对象
    private List<String> names=new ArrayList<String>();
    //传入多个age查询多个对象
    private List<Integer> ages=new ArrayList<Integer>();
    //对象属性组合查询
    private Student student;
            setter..
            getter...
            toString....
    }

SqlUtils工具类:


package com.leige.test;

import java.io.IOException;
import java.io.InputStream;

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


/**
 * @author sqlsession工具类
 *
 */
public class SqlUtils {
    static SqlSessionFactory factory;
    //静态加载session工厂
    static{
        InputStream inputStream;
        try {
            inputStream = Resources.getResourceAsStream("com/leige/config/configuration.xml");
            //1:实例化sqlsessionfactory工厂
            factory= new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {

        }


    }
    public static SqlSession getSession() {
        try{

        //开启session
        return factory.openSession();
        }catch(Exception e){

            throw new RuntimeException(e);
        }
    }
    /**
     * @param session
     * @param mapper
     * @return
     * 保证会话session一致,所以当做参数传过来
     */
    public static Object getmaMapper(SqlSession session,Class mapper){

        //注册映射接口
        factory.getConfiguration().addMapper(mapper);
        //返回操作实例
        return session.getMapper(mapper);
    }

}

测试类:

package com.leige.test;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.leige.domain.Student;
import com.leige.domain.StudentQueryVo;

public class App {
    /**
     * 测试使用resultMap
     */
    @Test
    public void testSelect(){

            SqlSession session=SqlUtils.getSession();

            //map查找
            Student student=session.selectOne("Student.selectResultMap", 1);
            //Student student=session.selectOne("Student.selectStudent", 1);
            System.out.println(student);
            session.close();

    }
    /**
     * 测试sql片段
     */
    @Test
    public void testSelectSql(){
        //获取session
            SqlSession session=SqlUtils.getSession();
            //实例多条件组合查询bean类
            StudentQueryVo queryVo=new StudentQueryVo();
            //设置多值查询,其他属性操作类似
            queryVo.getSids().add(1);
            queryVo.getSids().add(2);
            queryVo.getSids().add(3);
            //查询
            List students=session.selectList("Student.selectMultiply",queryVo);
            //输出
            for(Student stu:students)
                System.out.println(stu);
}


    /**
     * 测试sql变短多属性组合查询
     */
    @Test
    public void testSelectSql2(){
        //获取session
        SqlSession session=SqlUtils.getSession();
        //实例多条件组合查询bean类
        StudentQueryVo queryVo=new StudentQueryVo();
        //设置多属性组合查询
        Student student=new Student();
        student.setAge(22);
        student.setName("leige");
        queryVo.setStudent(student);
        queryVo.getSids().add(1);
        queryVo.getSids().add(2);
        //组合查询
        List students=session.selectList("Student.selectMultiply",queryVo);
        //输出
        for(Student stu:students)
            System.out.println(stu);

    }
}

你可能感兴趣的:(mybatis)