mysql example类_mybatis中的mapper接口文件以及example类的实例函数以及详解

使用Example 过程。

github :https://github.com/foolchild/ibatis-example

1.添加依赖

tk.mybatis

mapper-spring-boot-starter

2.1.5

2.解决mapper.xml文件不能随便放的问题。

1.添加build 属性

src/main/java

**/*.xml

false

src/main/resources

2.添加配置properties

mybatis.mapper-locations=classpath*:com/test/test/mapper/*.xml

3.启动类添加配置

@MapperScan("com.ibatis.example.mapper")

##Example example = new ##Example();

example.setOrderByClause("字段名 ASC"); //升序排列,desc为降序排列。

example.setDistinct(false)//去除重复,boolean型,true为选择不重复的记录。

Criteria criteria = new Example().createCriteria();

is null;is not null;

equal to(value);not equal to(value);

GreaterThan(value);GreaterThanOrEqualTo(value);

LessThan(value); LessThanOrEqualTo(value);

in(item,item,item,...);not in(item,item,item,...);

like("%"+value+"%");not like("%"+value+"%");

Between(value1,value2);not between(value1,value2)

mybatis中mapper的实例函数:

int countByExample(UserExample example) thorws SQLException:按条件计数。

int deleteByPrimaryKey(Integer id) thorws SQLException:按主键删除。

int deleteByExample(UserExample example) thorws SQLException:按条件删除。

String/Integer insert(User record) thorws SQLException:插入(返回值为id值)

User selectByPrimaryKey(Integer id) thorws SQLException:按主键查询。

List>selectByExample(UserExample example) thorws SQLException:按条件查询

List>selectByExampleWithBLOGs(UserExample example) thorws SQLException:按 条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。

int updateByPrimaryKey(User record) thorws SQLException:按主键更新

int updateByPrimaryKeySelective(User record) thorws SQLException:按主键更新值不为null的字段

int updateByExample(User record, UserExample example) thorws SQLException:按条件更新

int updateByExampleSelective(User record, UserExample example) thorwsSQLException:按条件更新值不为null的字段

mybatis中mapper的实例函数详解:

① selectByPrimaryKey()

User user = ##Mapper.selectByPrimaryKey(100); 相当于select * from user where id = 100

② selectByExample() 和 selectByExampleWithBLOGs()

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 UserExample example = newUserExample();2

3 Criteria criteria =example.createCriteria();4

5 criteria.andUsernameEqualTo("joe");6

7 criteria.andUsernameIsNull();8

9 example.setOrderByClause("username asc,email desc");10

11 List>list =##Mapper.selectByExample(example);12

13 相当于:select * from user where username = 'joe' and username is null order by username asc,email desc

View Code

注:在iBator 生成的文件UserExample.java中包含一个static 的内部类 Criteria ,

在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。

③ insert()

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 User user = newUser();2

3 user.setId(101);4

5 user.setUsername("test");6

7 user.setPassword("123")8

9 user.setEmail("joe@163.com");10

11 ##Mapper.insert(user);12

13 相当于:insert into user(ID,username,password,email) values (101,'test','123','joe@163.com');

View Code

④ updateByPrimaryKey() 和 updateByPrimaryKeySelective()

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 User user =newUser();2

3 user.setId(101);4

5 user.setUsername("joe");6

7 user.setPassword("joe");8

9 user.setEmail("joe@163.com");10

11 ##Mapper.updateByPrimaryKey(user);12

13 相当于:update user set username='joe',password='joe',email='joe@163.com' where id=101

14

15 User user = newUser();16

17 user.setId(101);18

19 user.setPassword("joe");20

21 ##Mapper.updateByPrimaryKeySelective(user);22

23 相当于:update user set password='joe' where id=101

View Code

⑤ updateByExample() 和 updateByExampleSelective()

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 UserExample example = newUserExample();2

3 Criteria criteria =example.createCriteria();4

5 criteria.andUsernameEqualTo("joe");6

7 User user = newUser();8

9 user.setPassword("123");10

11 ##Mapper.updateByPrimaryKeySelective(user,example);12

13 相当于:update user set password='123' where username='joe'

View Code

⑥ deleteByPrimaryKey()

##Mapper.deleteByPrimaryKey(101); 相当于:delete from user where id=101

⑦ deleteByExample()

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 UserExample example = newUserExample();2

3 Criteria criteria =example.createCriteria();4

5 criteria.andUsernameEqualTo("joe");6

7 ##Mapper.deleteByExample(example);8

9 相当于:delete from user where username='joe'

View Code

⑧ countByExample()

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 UserExample example = newUserExample();2

3 Criteria criteria =example.createCriteria();4

5 criteria.andUsernameEqualTo("joe");6

7 int count =##Mapper.countByExample(example);8

9 相当于:select count(*) from user where username='joe'

View Code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值