首先添加maven依赖:  

  <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>

  application.yml配置:

mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
typeAliasesPackage: com.itmayiedu.entity
global-config:
id-type: 0
field-strategy: 2
##允许下划线
db-column-underline: true
##允许大写
capital-mode: true
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句,调试用

  添加MybaticsConfig:

  

package com.itmayiedu.config;

import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyBatisConfig { // /**
// * mybatis-plus SQL执行效率插件【生产环境可以关闭】
// */
// @Bean
// public PerformanceInterceptor performanceInterceptor() {
// return new PerformanceInterceptor();
// } /**
* 分页插件
*
* @return
* @author zhaocheng
* @date 2018年4月14日下午4:13:15
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setLocalPage(true);// 开启 PageHelper 的支持,也可以关闭,plus自带分页 return paginationInterceptor;
} }

  编写dao层,RoleMapper:

package com.itmayiedu.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.itmayiedu.entity.Role; public interface RoleMapper extends BaseMapper<Role> {
}

  编写service层:

  RoleService

package com.itmayiedu.service;

import com.baomidou.mybatisplus.service.IService;
import com.itmayiedu.entity.Role; public interface RoleService extends IService<Role> {
}

  RoleServiceImpl

package com.itmayiedu.service.Impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.itmayiedu.entity.Role;
import com.itmayiedu.mapper.RoleMapper;
import com.itmayiedu.service.RoleService;
import org.springframework.stereotype.Service; @Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
}

  controller:

package com.itmayiedu.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.itmayiedu.entity.Role;
import com.itmayiedu.entity.User;
import com.itmayiedu.service.RoleService;
import com.itmayiedu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
@Slf4j
public class TestController { @Autowired
private UserService userService; @Autowired
private RoleService roleService; @RequestMapping("/testString")
public String testString(){
log.info("testString");
return "springboot01 test";
} @Transactional
@RequestMapping("/insert")
public Integer insertRole(String role_name,String note){
Integer a = userService.insertRole(role_name, note);
int b = 100/Integer.valueOf(note);
return a;
} //使用mybaticsPLUS分页
@RequestMapping("/testSql")
public void testSql(String name){
EntityWrapper<Role> ew = new EntityWrapper<Role>();
Page<Role> page = new Page<>(1, 2);
ew.andNew("note="+name);
Page<Role> userPage = roleService.selectPage(page, ew);
for(Role role:userPage.getRecords()){
log.info(role.getRole_name());
}
System.out.println(ew.getSqlSegment());
} //使用PageHelper分页
@RequestMapping("/testPage")
public void testPage(String name){
// Page page = PageHelper.startPage(size, 2);
PageHelper.startPage(1, 2);
EntityWrapper<Role> ew = new EntityWrapper<Role>();
ew.andNew("note="+name);
List<Role> data = roleService.selectList(ew);
for(Role role:data){
log.info(role.getRole_name());
}
}
}

springboot整合mybatics PLUS的更多相关文章

  1. spring-boot整合mybatis(1)

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...

  2. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  3. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  4. springboot整合mybaits注解开发

    springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...

  5. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  6. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  7. SpringBoot整合Kafka和Storm

    前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...

  8. SpringBoot整合SpringCloud搭建分布式应用

    什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...

  9. SpringBoot整合RabbitMQ-整合演示

    本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...

随机推荐

  1. centos7安装python,mariaDB,django,nginx

    0,安装centos7 centos默认不开启网卡,需要在安装时将ens33设置为on,或者后续通过vi ifcfg-ens33,找到onboot,设置为yes ssg登陆centos7时,如果提示W ...

  2. Log4Net 常见格式说明(不断更新中)

    用户名  %username pc版本 另起一行 %newline

  3. 个人信息——头像更换(拍照或相册上传)~ 微信小程序

    微信小程序中 在用户信息中关于用户头像更换(拍照或相册上传)功能实现. 图像点击触发事件: <image src='{{personImage}}' bindtap='changeAvatar' ...

  4. 1、java的数据类型

    一.基本数据类型 1.整型(byte,short,int,long) byte在内存中占用一个字节,short占用两个字节,int占用四个字节,long占用8个字节: Java语言中整型默认为int型 ...

  5. springboot整理

    lombok 添加maven依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId& ...

  6. C++中的getline

    https://www.cnblogs.com/ymd12103410/p/9514896.html#undefined

  7. Kafka分布式消息队列

    基本架构 Kafka分布式消息队列的作用: 解耦:将消息生产阶段和处理阶段拆分开,两个阶段互相独立各自实现自己的处理逻辑,通过Kafka提供的消息写入和消费接口实现对消息的连接处理.降低开发复杂度,提 ...

  8. mysql存储过程和执行计划案例

    开启event_scheduler指令: SET GLOBAL event_scheduler = ON;SET @@global.event_scheduler = ON;SET GLOBAL ev ...

  9. baidu-map

    1 var map = new BMap.Map("wcp"); // 创建Map实例 2 map.centerAndZoom(new BMap.Point(9.123469591 ...

  10. 颜色表 及 p em fr

      #000000   #2F0000   #600030   #460046   #28004D   #272727   #4D0000   #820041   #5E005E   #3A006F ...