mybatis Example條件查詢


Criterion是最基本,最底層的Where條件,用於字段級的篩選html


  • Criteriajava

Criteria包含一個Cretiron的集合,每個Criteria對象內包含的Cretiron之間是由AND鏈接的,是邏輯與的關係。spring

oredCriteriasql

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

其餘session

Example類的distinct字段用於指定DISTINCT查詢。數據結構

orderByClause字段用於指定ORDER BY條件,這個條件沒有構造方法,直接經過傳遞字符串值指定。mybatis


代碼app

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的。注意:屬性名必須以小寫字母開頭。 ide

對每一個屬性,通常定義兩個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











參考文章:

http://ljhzzyx.blog.163.com/blog/static/38380312201412043525595/

http://openwares.net/database/mybatis_generator_example.html

http://openwares.net/database/mybatis_parametertype.html

相關文章
相關標籤/搜索