1.各方法介紹
mybatis逆向工程生成的mapper源碼:
import com.itheima.springmvc.pojo.Items;
import com.itheima.springmvc.pojo.ItemsExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface ItemsMapper {
//按條件計數
int countByExample(ItemsExample example);
//按條件刪除
int deleteByExample(ItemsExample example);
//按主鍵刪除
int deleteByPrimaryKey(Integer id);
//插入數據(返回值為ID):如果record定義了兩個字段,其中有一個字段是主鍵,數據庫共4個字段,則根據主鍵修改數據庫的兩個字段,其余兩個字段改為null;
int insert(Items record);
//插入數據(返回值為ID):如果record定義了兩個字段,其中有一個字段是主鍵,數據庫共4個字段,則根據主鍵修改數據庫的兩個字段,其余兩個字段不動;
int insertSelective(Items record);
//按條件查詢(包括BLOB字段)。只有當數據表中的字段類型有為二進制的才會產生。
List<Items> selectByExampleWithBLOBs(ItemsExample example);
//按條件查詢
List<Items> selectByExample(ItemsExample example);
//按主鍵查詢
Items selectByPrimaryKey(Integer id);
//按條件更新值不為null的字段:如果example定義了兩個字段,數據庫共4個字段,則修改數據庫的兩個字段,其余兩個字段不動;
int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example);
//和updateByExample相比此方法可以修改大字段類型,其余性質和updateByExample相同
int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
//按條件更新:如果example定義了兩個字段,數據庫共4個字段,則修改數據庫的兩個字段,其余兩個字段改為null;
int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example);
//按主鍵更新值不為null的字段:如果record定義了兩個字段,其中有一個字段是主鍵,數據庫共4個字段,則根據主鍵修改數據庫的兩個字段,其余兩個字段不動;
int updateByPrimaryKeySelective(Items record);
//和updateByPrimaryKey相比此方法可以修改大字段類型,其余性質和updateByPrimaryKey相同
int updateByPrimaryKeyWithBLOBs(Items record);
//按主鍵更新:如果record定義了兩個字段,其中有一個字段是主鍵,數據庫共4個字段,則根據主鍵修改數據庫的兩個字段,其余兩個字段改為null;
int updateByPrimaryKey(Items record);
}
2.方具體使用
1、countByExample ===>根據條件查詢數量
int countByExample(UserExampleexample);
//下面是一個完整的案列
UserExampleexample=newUserExample();
Criteriacriteria=example.createCriteria();
criteria.andUsernameEqualTo("joe");
intcount=userDAO.countByExample(example);
相當於:select count(*)from user where username='joe';
2、deleteByExample ===>根據條件刪除多條
int deleteByExample(AccountExample example);
//下面是一個完整的案例
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
userDAO.deleteByExample(example);
相當於:delete from user where username='joe';
3、deleteByPrimaryKey===>根據條件刪除單條
int deleteByPrimaryKey(Integerid); userDAO.deleteByPrimaryKey(10001); 相當於:delete from user where id=10001;
4、insert===>插入數據
int insert(Account record);
//下面是完整的案例
User user = new User();
user.setId(10001);
user.setUsername("mrHan");
user.setPassword("123456")
user.setEmail("786***195@qq.com");
userDAO.insert(user);
相當於:insert into user(ID,username,password,email) values(10001,'mrHan','123456','786***195@qq.com');
5、insertSelective===>插入數據
int insertSelective(Accountrecord);
6、selectByExample===>根據條件查詢數據
List<Account> selectByExample(AccountExample example);
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<T>list = userDAO.selectByExample(example);
相當於:select * from user where username = 'joe' and username is null order by username asc,email desc
//注:在iBator 生成的文件UserExample.java中包含一個static 的內部類 Criteria ,在Criteria中有很多方法,主要是定義SQL 語句where后的查詢條件。
7、selectByPrimaryKey===>根據主鍵查詢數據
AccountselectByPrimaryKey(Integer id);
//相當於select * from user where id = {id};
8、updateByExampleSelective===>按條件更新值不為null的字段
int updateByExampleSelective(@Param("record") Account record, @Param("example") AccountExample example);
//下面是一個完整的案列
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
User user = new User();
user.setPassword("123456");
userDAO.updateByPrimaryKeySelective(user,example);
相當於:update user set password='123456' where username='joe';
9、updateByExampleSelective===>按條件更新
int updateByExample(@Param("record")Accountrecord,@Param("example")AccountExampleexample);
10、updateByPrimaryKeySelective===>按條件更新
int updateByPrimaryKeySelective(Account record);
//下面是一個完整的案例
User user = new User();user.setId(10001);
user.setPassword("123456");
userDAO.updateByPrimaryKeySelective(user);
相當於:update user set password='123456' where id=10001
11、updateByPrimaryKey===>按主鍵更新
int updateByPrimaryKey(Accountrecord);
//下面是一個完整的案例
Useruser=newUser();
user.setId(10001);
user.setUsername("mrHan");
user.setPassword("123456");
user.setEmail("786***195@qq.com");
userDAO.updateByPrimaryKey(user);
相當於:update user set username='mrHan',password='123456',email='786**195@qq.com'where id=10001;
int updateByPrimaryKeySelective(Accountrecord);
//下面是一個完整的案例
Useruser=newUser();
user.setId(10001);
user.setPassword("123456");
userDAO.updateByPrimaryKeySelective(user);
相當於:update user set password='123456'where id=10001;
補充
Mybatis自動生成的查詢selectByExample(TExample example) 中like需要自己寫通配符
TExample example = new TExample();
TExample.Criteria criteria = example.createCriteria();
if(StringUtils.isNotBlank(userName)){
userName = "%" + userName + "%";
}
if(StringUtils.isNotBlank(userName)){
criteria.andBuyerNameLike(userName);
}
dao.countByExample(example)
