mybaties-plus入门
目前正在维护的公司的一个项目是一个ssm架构的java项目,dao层的接口有大量数据库查询的方法,一个条件变化就要对应一个方法,再加上一些通用的curd方法,对应一张表的dao层方法有时候多达近20个,果断决定优化一下,经过一番探索,发现了一个mybatis的好伴侣,mybatis-plus,上手容易,简洁高效,这里有官方的文档入口,文档比较详细,本来不想在重复文档内容,但是关上文档,看着自己改过的项目,还是打算在脑海里过一下使用步骤及相关注意事项,顺便记录一下.
MyBatis-plus有什么特色
1.代码生成 2.条件构造器
对我而言,主要的目的是使用它强大的条件构建器.
快速使用步骤:
1.添加pom文件依赖

<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.0.1</version>
</dependency>

注意:mybatis-plus会自动维护mybatis以及mybatis-spring的依赖,所以不需要引入后两者,避免发生版本冲突.
2.修改配置文件
将mybatis的sqlSessionFactory替换成mybatis-plus的即可,mybatis-plus只做了一些功能的扩展:

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描Mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/*/*.xml"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.baomidou.springmvc.model.*"/>
<property name="plugins">
<array>
<!-- 分页插件配置 -->
<bean id="paginationInterceptor" class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
<property name="dialectType" value="mysql"/>
</bean>
</array>
</property>
<!-- 全局配置注入 -->
<property name="globalConfig" ref="globalConfig" />
</bean>

在上面的配置中,除了mybatis的常规配置,多了一个分页插件的配置和全局配置,mybatis-plus提供了很方便的使用分页的插件,还有一个全局配置如下:

<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!--
AUTO->`0`("数据库ID自增")
INPUT->`1`(用户输入ID")
ID_WORKER->`2`("全局唯一ID")
UUID->`3`("全局唯一ID")
-->
<property name="idType" value="2" />
<!--
MYSQL->`mysql`
ORACLE->`oracle`
DB2->`db2`
H2->`h2`
HSQL->`hsql`
SQLITE->`sqlite`
POSTGRE->`postgresql`
SQLSERVER2005->`sqlserver2005`
SQLSERVER->`sqlserver`
-->
<!-- Oracle需要添加该项 -->
<!-- <property name="dbType" value="oracle" /> -->
<!-- 全局表为下划线命名设置 true -->
<property name="dbColumnUnderline" value="true" />
</bean>

至此,配置工作就算大功告成了,接下来通过一个简单的例子来感受一下它的使用.
1.新建一个User表:

@TableName("user")
public class User implements Serializable { /** 用户ID */
private Long id; /** 用户名 */
private String name; /** 用户年龄 */
private Integer age; @TableField(exist = false)
private String state;
}

这里有两个注解需要注意,第一是@tableName("user"),它是指定与数据库表的关联,这里的注解意味着你的数据库里应该有一个名为user的表与之对应,并且数据表的列名应该就是User类的属性,对于User类中有而user表中没有的属性需要加第二个注解@TableField(exist = false),表示排除User类中的属性.
2.新建Dao层接口UserMapper:

/**
* User 表数据库控制层接口
*/
public interface UserMapper extends BaseMapper<User> {
@Select("selectUserList")
List<User> selectUserList(Pagination page,String state);
}

dao接口需要实现Basemapper,这样就能够使用封装好的很多通用方法,另外也可以自己编写方法,@select注解引用自第三步的UserMapper文件
3.新建UserMapper配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.baomidou.springmvc.mapper.system.UserMapper"> <!-- 通用查询结果列-->
<sql id="Base_Column_List">
id, name, age
</sql> <select id="selectUserList" resultType="User">
SELECT * FROM sys_user WHERE state=#{state}
</select>
</mapper>

4.新建service层类UserService:

/**
*
* User 表数据服务层接口实现类
*
*/
@Service
public class UserService extends ServiceImpl<UserMapper, User>{
public Page<User> selectUserPage(Page<User> page, String state) {
page.setRecords(baseMapper.selectUserList(page,state));
return page;
}
}

UserService继承了ServiceImpl类,mybatis-plus通过这种方式为我们注入了UserMapper,这样可以使用service层默认为我们提供的很多方法,也可以调用我们自己在dao层编写的操作数据库的方法.Page类是mybatis-plus提供分页功能的一个model,继承了Pagination,这样我们也不需要自己再编写一个Page类,直接使用即可.
5,新建controller层UserController

@Controller
public class UserController extends BaseController { @Autowired
private IUserService userService; @ResponseBody
@RequestMapping("/page")
public Object selectPage(Model model){ Page page=new Page(1,10);
page = userService.selectUserPage(page, "NORMAL");
return page;
}

以上就完成了一个基本的功能,下面来看一下它的条件构建器.
mybatis-plus的条件构建器
首先看一个条件构建器实例的简单实用.

public void test(){
EntityWrapper ew=new EntityWrapper();
ew.setEntity(new User());
String name="wang";
Integer age=16;
ew.where("name = {0}",name).andNew("age > {0}",age).orderBy("age");
List<User> list = userService.selectList(ew);
Page page2 = userService.selectPage(page, ew);
}

这里使用了一个条件包装类EntityWrapper,来进行对sql语句的拼装,原理也很好理解,上面的代码中,第一个list查询的结果就是查询数据库中name=wang并且age>16岁的所有记录并按照age排序.而第二个查询就是再多加一个分页的功能.
基本上来说,使用EntityWrapper可以简单地完成一些条件查询,但如果查询方法使用频率很高的话还是建议自己写在UserMapper里.
那么自定义的mapper方法能不能使用EntityWrapper呢,当然也是可以的.
文档中给了一个这样的例子.
1.在Mappper中定义:
List<User> selectMyPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper);
2.在mapper文件中定义:
<select id="selectMyPage" resultType="User">
SELECT * FROM user ${ew.sqlSegment}
</select>
对于EntityMapper的条件拼接,基本可以实现sql中常用的where,and,or,groupby,orderby等语法,具体构建方法可以灵活组合.

@Test
public void testTSQL11() {
/*
* 实体带查询使用方法 输出看结果
*/
ew.setEntity(new User(1));
ew.where("name={0}", "'zhangsan'").and("id=1")
.orNew("status={0}", "0").or("status=1")
.notLike("nlike", "notvalue")
.andNew("new=xx").like("hhh", "ddd")
.andNew("pwd=11").isNotNull("n1,n2").isNull("n3")
.groupBy("x1").groupBy("x2,x3")
.having("x1=11").having("x3=433")
.orderBy("dd").orderBy("d1,d2");
System.out.println(ew.getSqlSegment());
}

mybaties-plus入门的更多相关文章
- springMVC 缓存(入门 spring+mybaties+redis一)
使用redis之前需要咋电脑上安装redis: 使用spring+mybaties+redis的本质是扩展类 org.apache.ibatis.cache.Cache:在我们自己扩展的Cache ...
- IDEA工具下Mybaties框架快速入门程序
本篇文章介绍在IDEA工具下mybatis快速入门程序分为以下五步 1 添加依赖包 2 编写pojo对象 3 编写映射文件 4 编写核心配置文件 5 测试框架 详细如下 建立Mod ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍
笔记 1.SpringBoot2.x持久化数据方式介绍 简介:介绍近几年常用的访问数据库的方式和优缺点 1.原始java访问数据库 开发流程麻烦 ...
- spring入门详细教程(五)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- Spring MVC整合Mybatis 入门
本文记录使用Intellij创建Maven Web工程搭建Spring MVC + Mybatis 的一个非常简单的示例.关于Mybatis的入门使用可参考这篇文章,本文在该文的基础上,引入了Spri ...
- Mybatis最入门---代码自动生成(generatorConfig.xml配置)
[一步是咫尺,一步即天涯] 经过前文的叙述,各位看官是不是已经被Mybatis的强大功能给折服了呢?本文我们将介绍一个能够极大提升我们开发效率的插件:即代码自动生成.这里的代码自动生成包括,与数据库一 ...
- Spring Boot 2.x零基础入门到高级实战教程
一.零基础快速入门SpringBoot2.0 1.SpringBoot2.x课程全套介绍和高手系列知识点 简介:介绍SpringBoot2.x课程大纲章节 java基础,jdk环境,maven基础 2 ...
- IDEA+Maven 整合SSM框架实现简单的增删改查(新手入门,傻瓜操作)
原博客地址:https://blog.csdn.net/khxu666/article/details/79851070 选用SSM框架的原因在目前的企业级Java应用中,Spring框架是必须的.S ...
- [转]小D课堂 - 零基础入门SpringBoot2.X到实战_汇总
原文地址:https://www.cnblogs.com/wangjunwei/p/11392825.html 第1节零基础快速入门SpringBoot2.0 小D课堂 - 零基础入门SpringBo ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_汇总
第1节零基础快速入门SpringBoot2.0 小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_1.SpringBoot2.x课程介绍和高手系 ...
随机推荐
- hdu--2570--迷瘴
#include<iostream> #include<vector> #include<algorithm> using namespace std; int m ...
- (转载)Stackoverflow评选的C++推荐书单
C++必读书籍推荐 (原链接:http://bestcbooks.com/recommended-cpp-books 2013-10-07) 本文内容来自国外著名编程问答网站Stackoverflow ...
- 学习jdbc学习笔记
1.jdbc: java database connection jdbc规范:是sun公司制定的一套连接操作数据库的接口. 我们必须要用具体的驱动类去连接操作数据库. 每个 ...
- 再谈CVE-2017-7047 Triple_Fetch和iOS 10.3.2沙盒逃逸
作者:蒸米 ----------------- 0x00 序 Ian Beer@google发布了CVE-2017-7047Triple_Fetch的exp和writeup[1],chenliang@ ...
- 手算平方根和基于 Java BigInteger 的大整数平方根的实现
为了实现任意大数的运算,long用BigInteger替换带哦. 好了废话少数,先说数学原理,也就是手算平方根计算机代码实现!那么什么叫手算平方根了??? 手开方图解 据说前苏联的普通工人都会的(毛熊 ...
- Linux下MySQL安装和配置
--Linux下MySQL安装和配置 ---------------------------2014/05/18 Linux下MySQL的配置和安装 本文的安装采用 rpm 包安装 1.首先在官网下载 ...
- 关于JS脚本语言的基础语法
JS脚本语言的基础语法:输出语法 alert("警告!"); confirm("确定吗?"); prompt("请输入密码");为弱 ...
- Go语言循环判断的使用~
Go 语言条件语句 条件语句需要开发者通过指定一个或多个条件,并通过测试条件是否为 true 来决定是否执行指定语句,并在条件为 false 的情况在执行另外的语句. 下图展示了程序语言中条件语句的结 ...
- 教程:基于访问控制的ABAP CDS视图权限
Hi! 对每一个CDS视图,我们都可以通过DCL(Data Control Language)定义访问控制.在这篇文章中,我会介绍ABAP CDS视图中非常重要的一面:权限管理. 本文的阐述基于我正在 ...
- 【win10】大水牛主机插入耳机没有声音
主机:大水牛,技嘉主板 操作系统:win10 问题:主机前面插入耳机,没有声音,扬声器图标出错 解决 一..插入耳机 二..Realtek高清晰音频管理器 1.打开音频管理器,点击右下角的设置 2.点 ...