温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中MyBatis Plus知识点总结

发布时间:2020-09-29 23:22:50 来源:脚本之家 阅读:193 作者:wx5da18b5c4b01e 栏目:编程语言

好程序员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);

 }

以上就是本次介绍的全部相关知识点,感谢大家的学习和对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI