Java中MyBatis Plus知识点总结
作者:wx5da18b5c4b01e 时间:2023-08-11 14:12:04
好程序员Java教程分享MyBatis Plus介绍:
1.MyBatis Plus 介绍
MyBatis Plus 是国内人员开发的 MyBatis 增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
MyBatis Plus 的核心功能有:支持通用的 CRUD、代码生成器与条件构造器。
通用 CRUD:定义好 Mapper 接口后,只需要继承 BaseMapper<T> 接口即可获得通用的增删改查功能,无需编写任何接口方法与配置文件
条件构造器:通过 EntityWrapper<T> (实体包装类),可以用于拼接 SQL 语句,并且支持排序、分组查询等复杂的 SQL
2.添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
3.配置
<!-- MP 提供的 MybatisSqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean"
class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 别名处理 -->
<property name="typeAliasesPackage" value="com.qf.entity"/>
<!-- 插件注册 -->
<property name="plugins">
<list>
<!-- 注册分页插件 -->
<bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor" />
</list>
</property>
</bean>
4.Dao层
public interface IUserDao extends BaseMapper<User> {
}
5.实体类
@Data
@TableName(value="t_user")
public class User{
@TableId(value="id",type=IdType.AUTO)
private Integer id;
@TableField(value="username")
private String name;
private Integer age;
private String password;
@TableField(exist=false)
private Integer xxx;
}
6.常见注解
@TableField(exist = false):表示该属性不为数据库表字段,但又是必须使用的。
@TableField(exist = true):表示该属性为数据库表字段。
@TableName:数据库表相关
@TableId:表主键标识
@TableField:表字段标识
7.测试方法
@Test
public void testMybatisPlus(){
System.out.println("selectById:"+userDao.selectById(4)); // 根据Id查询
System.out.println("selectList:"+userDao.selectList(null)); // 查询全部
com.baomidou.mybatisplus.plugins.Page<User> page = new com.baomidou.mybatisplus.plugins.Page<>();
List<User> list = userDao.selectPage(page, null); // 分页查询
page.setRecords(list); // 把结果封装到分页对象中
System.out.println(page.getCurrent());
System.out.println(page.getPages());
System.out.println(page.getSize());
System.out.println(page.getTotal());
System.out.println(page.getRecords());
EntityWrapper<User> entityWrapper = new EntityWrapper<>();
entityWrapper.eq("id", 4);
entityWrapper.or().like("username", "3");
List<User> selectList = userDao.selectList(entityWrapper); // 条件查询
System.out.println("wrapper:"+selectList);
}
来源:https://blog.51cto.com/14573321/2446419
标签:Java,MyBatis,Plus
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot+nginx+https+linux实现负载均衡加域名访问简单测试
2022-04-26 22:35:13
![](https://img.aspxhome.com/file/2023/4/92704_0s.png)
struts2 validation.xml 验证规则代码解析
2021-09-14 22:01:27
Android中RecyclerView布局代替GridView实现类似支付宝的界面
2021-08-20 02:32:34
![](https://img.aspxhome.com/file/2023/1/123301_0s.png)
Android 嵌套Fragment的使用实例代码
2022-07-18 06:37:15
![](https://img.aspxhome.com/file/2023/6/111686_0s.png)
Java 导出excel进行换行的案例
2021-07-29 04:09:36
![](https://img.aspxhome.com/file/2023/9/69729_0s.jpg)
Java用邻接矩阵存储图的示例代码
2021-10-05 21:39:18
![](https://img.aspxhome.com/file/2023/1/64171_0s.png)
Java使用组件编写窗口实现网上文件下载
2023-09-14 06:08:36
![](https://img.aspxhome.com/file/2023/8/108438_0s.jpg)
C#在子线程中更新窗口部件的写法
2022-04-01 09:57:50
SpringBoot中的五种对静态资源的映射规则的实现
2023-06-21 08:31:47
![](https://img.aspxhome.com/file/2023/4/108354_0s.jpg)
Java代码实现矩形覆盖实例
2022-02-04 22:02:02
Mybatis 如何开启控制台打印sql语句
2023-08-02 02:01:53
![](https://img.aspxhome.com/file/2023/5/99865_0s.jpg)
C#线程同步的三类情景分析
2023-01-07 22:38:04
Android自定义组件获取本地图片和相机拍照图片
2022-07-09 21:54:48
![](https://img.aspxhome.com/file/2023/1/101831_0s.gif)
Android开发双向滑动选择器范围SeekBar实现
2023-04-07 14:46:59
![](https://img.aspxhome.com/file/2023/2/125372_0s.gif)
如何通过Java实现时间轴过程解析
2022-01-02 00:31:32
![](https://img.aspxhome.com/file/2023/8/67478_0s.png)
C#中系统时间和UNIX时间戳互相转换
2021-06-02 01:13:58
浅谈Java安全之C3P0链利用与分析
2022-05-23 20:31:17
![](https://img.aspxhome.com/file/2023/0/95950_0s.jpg)
浅谈java类和对象
2021-10-01 06:01:59
![](https://img.aspxhome.com/file/2023/3/70933_0s.png)
Android 如何定制vibrator的各种震动模式M 具体方法
2023-08-13 02:09:01
SpringBoot整合Elasticsearch游标查询的示例代码(scroll)
2022-02-11 02:02:13