MyBatis Plus 是國內人員開發的 MyBatis 加強工具,在 MyBatis 的基礎上只作加強不作改變,爲簡化開發、提升效率而生。spring
MyBatis Plus 的核心功能有:支持通用的 CRUD、代碼生成器與條件構造器。sql
通用 CRUD:定義好 Mapper 接口後,只須要繼承 BaseMapper<T> 接口便可得到通用的增刪改查功能,無需編寫任何接口方法與配置文件數據庫
條件構造器:經過 EntityWrapper<T> (實體包裝類),能夠用於拼接 SQL 語句,而且支持排序、分組查詢等複雜的 SQLmybatis
<dependency>app <groupId>com.baomidou</groupId>工具 <artifactId>mybatis-plus</artifactId>測試 <version>2.3</version>插件 </dependency>對象 |
<!-- 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> |
public interface IUserDao extends BaseMapper<User> { } |
@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; } |
@TableField(exist = false):表示該屬性不爲數據庫表字段,但又是必須使用的。
@TableField(exist = true):表示該屬性爲數據庫表字段。
@TableName:數據庫表相關
@TableId:表主鍵標識
@TableField:表字段標識
@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);
} |