Mybatis-Plus 是一款 Mybatis 动态 SQL 自动注入 Mybatis 增删改查 CRUD 操作中间件, 减少你的开发周期优化动态维护 XML 实体字段,无入侵全方位 ORM 辅助层让您拥有更多时间陪家人。
http://mp.baomidou.com/
https://github.com/baomidou/mybatis-plus
3.0.5之后,在SpringBoot2的包引入后,只需要这样一个包就搞定了。
com.baomidou
mybatis-plus-boot-starter
3.0.5
3.0.5之后,无需额外的配置,只需要一句@MapperScan("com.xxxx.xxx.mapper")
即可开启Mybatis-Plus。以下配置为常规配置而已,仅供参考。
server:
port: 3333
servlet:
context-path: /mybatisplus
tomcat:
remote-ip-header: x-forward-for
uri-encoding: UTF-8
max-threads: 10
background-processor-delay: 30
spring:
http:
encoding:
force: true
charset: UTF-8
application:
name: spring-cloud-study-mybatisplus
freemarker:
request-context-attribute: req
#prefix: /templates/
suffix: .html
content-type: text/html
enabled: true
cache: false
charset: UTF-8
allow-request-override: false
expose-request-attributes: true
expose-session-attributes: true
expose-spring-macro-helpers: true
#template-loader-path: classpath:/templates/
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
driver-class-name: com.mysql.jdbc.Driver
platform: mysql
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
filters: stat,wall,log4j
推荐用我写的生成器,用sql语句生成实体类,而且最好用lombok简化setget
http://java.bejson.com/generator
Mapper只需要extends BaseMapper
就可以完美实现增删改查等一系列操作,非常方便
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserMapper extends BaseMapper{
}
这里Entity省略setter和getter方法,可以用lombok的@Data代替
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String userId;
private String userName;
private String passWord;
private String name;
private String tell;
private int status;
}
这里随便写个init方法和find方法,init就不用说了,初始化一些数据进去,find使用了QueryWrapper构造器,很方便。
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/init")
public ApiReturnObject init(){
List userList=new ArrayList();
for (int i = 0; i < 10; i++) {
int n=RandomUtil.randomInt(10000,99999)+i;
User user=new User();
user.setId(n);
user.setName(n+"");
user.setPassWord(n+"");
user.setStatus(1);
user.setUserId(n+"");
user.setUserName(n+"");
userMapper.insert(user);
userList.add(user);
user=null;
}
return ApiReturnUtil.success(userList);
}
@GetMapping("/find")
public ApiReturnObject find(){
IPage userList=userMapper.selectPage(
new Page(1, 10),
new QueryWrapper().like("name","1")
);
return ApiReturnUtil.success(userList.getRecords());
}
}
这里还放出一段实战语句,直接用QueryWrapper构造器模糊查询证书编码,这个构造器是mybatis-plus的一个精华,请用好
@RestController
public class CertContoller {
@Autowired
private CertMapper certMapper;
@RequestMapping("/certCompanyDetail")
public Object certCompanyDetail(){
return certMapper.selectOne(new QueryWrapper().like("cert_num","EGAG-SAL05-201811XXXX"));
}
}
访问数据初始化 http://127.0.0.1:1111/mybatisplus/user/init
{
"errorCode": "00",
"errorMessage": "success",
"returnObject": [
{
"id": 52585,
"name": "52585",
"passWord": "52585",
"status": 1,
"userId": "52585",
"userName": "52585"
},
{
"id": 33432,
"name": "33432",
"passWord": "33432",
"status": 1,
"userId": "33432",
"userName": "33432"
}
。。。这里省略其他的
]
}
访问构造器查询 http://127.0.0.1:3333/mybatisplus/user/find
{
"errorCode": "00",
"errorMessage": "success",
"returnObject": [
{
"id": 41618,
"name": "41618",
"passWord": "41618",
"status": 1,
"userId": "41618",
"userName": "41618"
},
{
"id": 17804,
"name": "17804",
"passWord": "17804",
"status": 1,
"userId": "17804",
"userName": "17804"
}
。。。省略其他包含1的
]
}
查询方式 | 说明 |
---|---|
setSqlSelect | 设置 SELECT 查询字段 |
where | WHERE 语句,拼接 + WHERE 条件 |
and | AND 语句,拼接 + AND 字段=值 |
andNew(已失效) | AND 语句,拼接 + AND (字段=值) |
or | OR 语句,拼接 + OR 字段=值 |
orNew(已失效) | OR 语句,拼接 + OR (字段=值) |
eq | 等于= |
allEq | 基于 map 内容等于= |
ne | 不等于<> |
gt | 大于> |
ge | 大于等于>= |
lt | 小于< |
le | 小于等于<= |
like | 模糊查询 LIKE |
notLike | 模糊查询 NOT LIKE |
in | IN 查询 |
notIn | NOT IN 查询 |
isNull | NULL 值查询 |
isNotNull | IS NOT NULL |
groupBy | 分组 GROUP BY |
having | HAVING 关键词 |
orderBy | 排序 ORDER BY |
orderByAsc(有变化,中间有By) | ASC 排序 ORDER BY |
orderByDesc(有变化,中间有By) | DESC 排序 ORDER BY |
exists | EXISTS 条件语句 |
notExists | NOT EXISTS 条件语句 |
between | BETWEEN 条件语句 |
notBetween | NOT BETWEEN 条件语句 |
addFilter | 自由拼接 SQL |
last | 拼接在最后,例如:last("LIMIT 1") |
已经在springboot注入mybatis的分页插件,直接使用Page参数
IPage userList=userMapper.selectPage(
new Page(1, 10),
new QueryWrapper().like("name","1")
);
补充一个分页的DEMO
@PostMapping("/list")
public Object list(@RequestParam("currentPage") Integer currentPage,
@RequestParam("pageSize") Integer pageSize,CertPerson certPerson){
Page page = new Page(currentPage,pageSize);
QueryWrapper queryWrapperw = new QueryWrapper(certPerson);
IPage pageList = certPersonMapper.selectPage(page, queryWrapperw);
return ApiReturnUtil.success(pageList);
}
https://github.com/moshowgame/spring-cloud-study
https://github.com/moshowgame/spring-cloud-study/tree/master/spring-cloud-study-mybatisplus