1. 程式人生 > >mybatis Example條件查詢

mybatis Example條件查詢

Criterion是最基本,最底層的Where條件,用於欄位級的篩選

  • Criteria

Criteria包含一個Cretiron的集合,每一個Criteria物件內包含的Cretiron之間是由AND連線的,是邏輯與的關係。

oredCriteria

Example內有一個成員叫oredCriteria,是Criteria的集合,就想其名字所預示的一樣,這個集合中的Criteria是由OR連線的,是邏輯或關係。oredCriteria就是ORed Criteria。

其他

Example類的distinct欄位用於指定DISTINCT查詢。

orderByClause欄位用於指定ORDER BY條件,這個條件沒有構造方法,直接通過傳遞字串值指定。

程式碼

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

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.apache.log4j.pattern.ClassNamePatternConverter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.ssm.mapper.ItemsMapper;
import cn.itcast.ssm.po.ItemsExample;

public class Student {

    public static void main(String[] args) throws IOException {

        /*方式一  */
        ItemsExample itemsExample1 = new ItemsExample();

        itemsExample1.or().andIdEqualTo(5).andNameIsNotNull();
        itemsExample1.or().andPicEqualTo("xxx").andPicIsNull();

        List<Integer> fieldValues = new ArrayList<Integer>();
        fieldValues.add(8);
        fieldValues.add(11);
        fieldValues.add(14);
        fieldValues.add(22);
        itemsExample1.or().andIdIn(fieldValues);
        itemsExample1.or().andIdBetween(5, 9);

        /*  方式二 criteria1與criteria2是or的關係 */

        ItemsExample itemsExample2 = new ItemsExample();
        ItemsExample.Criteria criteria1 = itemsExample2.createCriteria();
        criteria1.andIdIsNull();
        criteria1.andPriceEqualTo((float) 3);

        ItemsExample.Criteria criteria2 = itemsExample2.createCriteria();
        criteria2.andNameIsNull();
        criteria2.andIdGreaterThanOrEqualTo(5);
        itemsExample2.or(criteria2);

        //方式一和方式二是等價的
        
        
        // spring獲取mapper代理物件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        ItemsMapper itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
        itemsMapper.countByExample(itemsExample2);

        // 獲取SqlSessionFactory
        String resource = "SqlMapConfig.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
        // 獲取SqlSession
        SqlSession sqlSession = sqlMapper.openSession();

    }
}

JavaBeans類的成員變數一般稱為屬性(property)。對每個屬性訪問許可權一般定義為privateprotected,而不是定義為public的。注意:屬性名必須以小寫字母開頭。 

對每個屬性,一般定義兩個public方法,它們分別稱為訪問方法(getter)和修改方法(setter),允許容器訪問和修改bean的屬性。 

      public String getColor();

      public void setColor(String);

一個例外是當屬性是boolean型別時,訪問器方法應該定義為isXxx()形式。

物件型別

雖然可以明確的引用物件的屬性名了,但如果要在if元素中測試傳入的user引數,仍然要使用_parameter

來引用傳遞進來的實際引數,因為傳遞進來的User物件的名字是不可考的。如果測試物件的屬性,則直接引用屬性名字就可以了。

測試user物件:

<if test="_parameter != null">

測試user物件的屬性:

<if test="name != null">

map型別

傳入map型別,直接通過#{keyname}就可以引用到鍵對應的值。使用@param註釋的多個引數值也會組裝成一個map資料結構,和直接傳遞map進來沒有區別。

mapper介面:

int updateByExample(@Param("user") User user, @Param("example") UserExample example);

sql對映:

  <update id="updateByExample" parameterType="map" >
    update tb_user
    set id = #{user.id,jdbcType=INTEGER},
    ...
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>

注意這裡測試傳遞進來的map是否為空,仍然使用_parameter

參考文章: