MyBatis的Example总结

最近在工作中遇到的Example类直接可以当mybatisplus的basemapper来用还是很好的

Example类

  • 什么是Example类呢?
    。其实Example类是mybatis的内容,官方解释是:mybatis-generator会为每个字段产生Criterion,为底层的mapper.xml创建动态sql。如果表的字段比较多,产生的example类会十分庞大。理论上通过example类可以构造你想到的任何筛选条件。在mybatis-generator中加以配置,配置数据表的生成操作就可以自动生成example了。(学过mybatispuls了话就很好理解,跟那mapper一样的)
上图:

在这里插入图片描述

解释

这个是Example类创建的方法API:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

1.查询:

(1)selectByPrimaryKey

User user = UserMapper.selectByPrimaryKey("1001");
等同于:select * from user where id = "1001" 

(2)selectByExample (and条件)

Example example = new Example(User.class);
example.createCriteria().andEqualTo("id", "1001" )
	.andEqualTo("userName", "小李");
User user = UserMapper.selectByExample(example);

等同于:select * from user where id = "1001" and username = '小李'

selectByExample (or条件)

Example example = new Example(User.class);
example.or.andEqualTo("id", "1001" )
example.or.andEqualTo("userName", "小李");
User user = UserMapper.selectByExample(example);

等同于:select * from user where id = "1001" or username = '小李'

selectByExample (and+or多条件查询)

Example example = new Example(User.class);
        example.createCriteria().andEqualTo("id", "1" )
                .andEqualTo("userId", "1001");
        example.and().orEqualTo("userName","小李")
                .orEqualTo("passWord","123");
        List<User> user = UserMapper.selectByExample(example);

等同于:
SELECT id,user_id,user_name,pass_word FROM user WHERE ( id = "1" and user_id = "1001" ) and ( user_name = "小李" or pass_word = "123" )
2.插入:
User user = new User();
user.setId("2");
user.setUserId("1002");
user.setUserName("小王");
user.setPassWord("666")
UserMapper.insert(user);
等同于:insert into user(id,user_id ,user_name,pass_word) values ('2','1002','小王','666');
3.更新:

(1)updateByPrimaryKey

User user = new User();
user.setId("2");
user.setUserId("1002");
user.setUserName("小王");
user.setPassWord("666")
UserMapper.updateByPrimaryKey(user);
等同于:update user set user_id = "1002" user_name='小王', pass_word='666' where id='2'

(2)updateByExampleSelective

注意:updateByExample()更新所有的字段,包括字段为null的,建议使用 updateByExampleSelective()更新需要更新的字段

Example example = new Example(User.class);
example.createCriteria().andEqualTo("id","2");

User user = new User();
user.setUserName("小王");

UserMapper.updateByExampleSelective(user,example);

等同于:update user set user_name='小王' where id='2'

4.删除:

(1)deleteByPrimaryKey

UserMapper.deleteByPrimaryKey("1");  //相当于:delete from user where id="1"
``
(2)deleteByExample
```java
Example example = new Example(User.class);
example.createCriteria().andEqualTo("userId","1002");
UserMapper.deleteByExample(example);
等同于:delete from user where userId='1002'
5.查询数量:
Example example = new Example(User.class);
example.createCriteria().andEqualTo("id", "1001" )
UserMapper.countByExample(example);
等同于:select count(*) from user where id='1001'

总结:

Mybatis逆向工程会生成实例及实例对应的example(用于添加条件,相当于where后的部分)
xxxExample example = new xxxExample();
Criteria
criteria = example.createCriteria()后面可以一直.条件。
这个其他也是属于mybatis的内容,就是别人帮你封装了,你可以不用去写一些单表的crud了,直接去实现自己的业务逻辑就行。
这也大大节省了开发时间,跟mybtaisplus的用法大同小异。

在这里插入图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Spring整合MyBatis的原理可以总结为以下几步: 1. 配置数据源:在Spring配置文件中配置数据源,可以使用Spring自带的数据源或第三方数据源。 2. 配置SqlSessionFactory:SqlSessionFactory是MyBatis的核心配置对象,可以通过Spring的MyBatis-Spring模块来配置SqlSessionFactory。 3. 配置MapperScannerConfigurer:MapperScannerConfigurer是MyBatis-Spring模块提供的一个扫描器,用于自动扫描Mapper接口并注册到Spring容器中。 4. 配置事务管理器:Spring提供了多种事务管理器,可以根据实际情况来选择。 总结一下,Spring整合MyBatis的过程就是将MyBatis的核心配置对象SqlSessionFactory和Mapper接口通过Spring的容器管理起来,并且使用Spring提供的事务管理器来管理事务。 具体的步骤如下: 1. 在Spring配置文件中配置数据源,可以使用Spring自带的数据源或第三方数据源。例如: ``` <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> ``` 2. 配置SqlSessionFactory,可以使用MyBatis-Spring模块提供的SqlSessionFactoryBean来配置。例如: ``` <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.example.model" /> <property name="mapperLocations" value="classpath*:com/example/mapper/*.xml" /> </bean> ``` 其中,typeAliasesPackage用于指定实体类所在的包,mapperLocations用于指定Mapper XML文件所在的路径。 3. 配置MapperScannerConfigurer,可以使用MyBatis-Spring模块提供的MapperScannerConfigurer来配置。例如: ``` <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper" /> </bean> ``` 其中,basePackage用于指定Mapper接口所在的包。 4. 配置事务管理器,可以使用Spring提供的事务管理器。例如: ``` <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> ``` 最后,在Service层的方法上添加@Transactional注解即可实现事务管理。 综上所述,Spring整合MyBatis的过程比较简单,只需要配置几个关键的对象即可。同时,Spring提供了很多方便的注解和工具类,可以更加方便地使用MyBatis

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员小小刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值