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)。对每个属性访问权限一般定义为private或protected,而不是定义为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
参考文章:
http://ljhzzyx.blog.163.com/blog/static/38380312201412043525595/
http://openwares.net/database/mybatis_generator_example.html
http://openwares.net/database/mybatis_parametertype.html
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1741268
原文地址:http://tianxingzhe.blog.51cto.com/3390077/1741268