欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis中example用法

程序员文章站 2024-03-02 00:02:46
...
mybatis中 example用法
        查询:
           排序:example.orderBy("id")
              selectByPrimaryKey(100):主键查询 :where id = 100
              selectByExample()和selectByExampleWithBLOGs() 示例:
                                                           UserExample example = new UserExample();
                                                           Criteria criteria = example.createCriteria();
                                                           criteria.andUsernameEqualTo("wyw");
                                                           criteria.andUsernameIsNull();
                                                           example.setOrderByClause("username asc,email desc");
                                                           List<?>list = XxxMapper.selectByExample(example);
                                                           //相当于:select * from user where username = 'wyw' and  username is null order by username asc,email desc
        插入:
             示例: 
                    User user = new User();
                    user.setId("dsfgsdfgdsfgds");
                    user.setUsername("admin");
                    user.setPassword("admin")
                    user.setEmail("[email protected]");
                    XxxMapper.insert(user);
                    //相当于:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','[email protected]');
        更新:
             updateByPrimanryKey():
                 示例:
                  1) User user =new User();
                     user.setId("dsfgsdfgdsfgds");
                     user.setUsername("wyw");
                     user.setPassword("wyw");
                     user.setEmail("[email protected]");
                     XxxMapper.updateByPrimaryKey(user);
                     //相当于:update user set username='wyw', password='wyw', email='[email protected]' where id='dsfgsdfgdsfgds'	
				
				  2)updateByExample() 和 updateByExampleSelective()
                    UserExample example = new UserExample();
                    Criteria criteria = example.createCriteria();
                    criteria.andUsernameEqualTo("admin");
                    User user = new User();
                    user.setPassword("wyw");
                    XxxMapper.updateByPrimaryKeySelective(user,example);
                    //相当于:update user set password='wyw' where username='admin 
					updateByExample()更新所有的字段,包括字段为null的也更新,建议使用 updateByExampleSelective()更新想更新的字段
					
	    删除:
		     deleteByPrimaryKey():根据key删除
			     示例:
				    XxxMapper.deleteByPrimaryKey(1);  //相当于:delete from user where id=1
		     deleteByExample():根据字段删除
			     示例:
			        UserExample example = new UserExample();
                    Criteria criteria = example.createCriteria();
                    criteria.andUsernameEqualTo("admin");
                    XxxMapper.deleteByExample(example);
                    //相当于:delete from user where username='admin' 
					
		查询数据量:
		     countByExample()
			    示例:
				  UserExample example = new UserExample();
                  Criteria criteria = example.createCriteria();
                  criteria.andUsernameEqualTo("wyw");
                  int count = XxxMapper.countByExample(example);
                  //相当于:select count(*) from user where username='wyw'
				  
				  
				  
  附:
       mapper接口中的函数及方法表:
                          方法名 	                                    
                             int countByExample(UserExample example) 	按条件计数
                             int deleteByPrimaryKey(Integer id) 	按主键删除
                             int deleteByExample(UserExample example) 	按条件查询
                             String/Integer insert(User record) 	插入数据(返回值为ID)
                             User selectByPrimaryKey(Integer id) 	按主键查询
                             ListselectByExample(UserExample example) 	按条件查询
                             ListselectByExampleWithBLOGs(UserExample example) 	按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
                             int updateByPrimaryKey(User record) 	按主键更新
                             int updateByPrimaryKeySelective(User record) 	按主键更新值不为null的字段
                             int updateByExample(User record, UserExample example) 	按条件更新
                             int updateByExampleSelective(User record, UserExample example) 	按条件更新值不为null的字段
    
	    example实例方法
		                  方法名
     						 example.setOrderByClause(“字段名 ASC”); 	添加升序排列条件,DESC为降序
                             example.setDistinct(false) 	去除重复,boolean型,true为选择不重复的记录。
                             example.and(Criteria criteria) 	为example添加criteria查询条件,关系为与
                             example.or(Criteria criteria) 	为example添加criteria查询条件,关系为或
                             criteria.andXxxIsNull 	添加字段xxx为null的条件 
                             criteria.andXxxIsNotNull 	添加字段xxx不为null的条件
                             criteria.andXxxEqualTo(value) 	添加xxx字段等于value条件
                             criteria.andXxxNotEqualTo(value) 	添加xxx字段不等于value条件
                             criteria.andXxxGreaterThan(value) 	添加xxx字段大于value条件
                             criteria.andXxxGreaterThanOrEqualTo(value) 	添加xxx字段大于等于value条件
                             criteria.andXxxLessThan(value) 	添加xxx字段小于value条件
                             criteria.andXxxLessThanOrEqualTo(value) 	添加xxx字段小于等于value条件
                             criteria.andXxxIn(List<?>) 	添加xxx字段值在List<?>条件
                             criteria.andXxxNotIn(List<?>) 	添加xxx字段值不在List<?>条件
                             criteria.andXxxLike(“%”+value+”%”) 	添加xxx字段值为value的模糊查询条件
                             criteria.andXxxNotLike(“%”+value+”%”) 	添加xxx字段值不为value的模糊查询条件
                             criteria.andXxxBetween(value1,value2) 	添加xxx字段值在value1和value2之间条件
                             criteria.andXxxNotBetween(value1,value2) 	添加xxx字段值不在value1和value2之间条件