Java教好程序员程分享MyBatis +介绍

  

好程序员Java教程分享MyBatis +介绍:1。MyBatis +介绍

  

MyBatis +是国内人员开发的MyBatis增强工具,在MyBatis的基础上只做增强不做改变,为简化开发,提高效率而生。

  

  

MyBatis +的核心功能有:支持通用的CRUD,代码生成器与条件构造器。

  

  

通用CRUD:定义好Mapper接口后,只需要继承BaseMapper接口即可获得通用的增删改查功能,无需编写任何接口方法与配置文件

  

  

条件构造器:通过EntityWrapper(实体包装类),可以用于拼接SQL语句,并且支持排序、分组查询等复杂的SQL

  

2。添加依赖

  

& lt; dependency>

  
 <代码> & lt; groupId> com.baomidou
  
  & lt; artifactId> mybatis-plus
  
  & lt; version> 2.3 & lt;/version>
  
  & lt;/dependency>  
  

3。配置

  

& lt; !——议员提供的MybatisSqlSessionFactoryBean——在

  
 <代码> & lt; bean id=皊qlSessionFactoryBean”
  
  类=癱om.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean”比;
  
  & lt; !——数据源——比;
  
  & lt;属性名=笆菰础眗ef=笆菰础?比;
  
  & lt; !——别名处理——比;
  
  & lt;属性名=" typeAliasesPackage " value=" https://www.yisu.com/zixun/com.qf.entity "/比;
  
  & lt; !——插件注册——比;
  
  & lt;属性名=安寮北?
  
  & lt; list>  
  

& lt; !——注册分页插件——在

  
 <代码> & lt; bean类=" com.baomidou.mybatisplus.plugins。PaginationInterceptor”/比;
  
  & lt;/list>
  
  & lt;/property>
  
  & lt;/bean>  
  

4. dao层   

公共接口IUserDao延伸BaseMapper{

  

}   

5。实体类

  

@ data   

@TableName (value=https://www.yisu.com/zixun/t_user)

  

公开课用户{

  
 <代码> @TableId (value=" https://www.yisu.com/zixun/id ",输入=IdType.AUTO)
  
  私人整数id;
  
  @TableField (value=" https://www.yisu.com/zixun/username ")
  
  私人字符串名称;
  
  私人整数年龄;
  
  私人密码字符串;
  
  @TableField(存在=false)
  
  私人整数xxx;  
  

}   

6。常见注解

  

@TableField(存在=false):表示该属性不为数据库表字段,但又是必须使用的。

  

@TableField(存在=true):表示该属性为数据库表字段。

  

@TableName:数据库表相关

  

@TableId:表主键标识

  

@TableField:表字段标识

  

7。测试方法

  

@Test   

 <代码>公共空testMybatisPlus () {
  
  System.out.println (“selectById:”+ userDao.selectById (4));//根据Id查询
  
  System.out.println (“selectList:”+ userDao.selectList (null));//查询全部
  
  com.baomidou.mybatisplus.plugins.Page,页面=new com.baomidou.mybatisplus.plugins.Page<的在();
  
  List=userDao列表。selectPage(页面,null);//分页查询
  
  page.setRecords(列表);//把结果封装到分页对象中
  
  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,entityWrapper=new EntityWrapper<的在();
  
  entityWrapper。eq (“id”, 4);
  
  entityWrapper.or ()。像(“用户名”、“3”);
  
  ListselectList=userDao.selectList (entityWrapper);//条件查询
  
  System.out.println(“包装器:“+ selectList);
  
  } 

Java教好程序员程分享MyBatis +介绍