首先添加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. SKU : Stock Keeping Unit

    Stock Keeping Unit  is a number assigned to a product by a retail store to identify the price, produ ...

  2. @RequestMapping的Ant风格URL

    Ant风格资源地址支持3中匹配符 ? 匹配文件名中一个字符. *   匹配 文件名中任意字符 **  匹配多层路径 例如 /hello/*/myspring 匹配 /hello/abc/mysprin ...

  3. English trip V2 - 6 Sports Teacher:Taylor Key:phrasal verbs

    In this lesson you will learn to talk about sports. 课上内容(Lesson) # How many different sports can you ...

  4. Java+selenium+feeder+AutoIt+自动加载插件

    package dataproject.datapro; import static org.testng.Assert.assertEqualsNoOrder; import java.io.Fil ...

  5. 基于Docker的Mysql主从复制搭建

    来源:https://www.cnblogs.com/songwenjie/p/9371422.html?tdsourcetag=s_pctim_aiomsg   为什么基于Docker搭建? 资源有 ...

  6. 如何使用js改变HTML中title里面固定的文字

    document.title = '这里是你想要替换的文字';

  7. promise用法十道题

    JS是单线程语言,多数的网站不需要大量计算,程序耗时主要是在磁盘I/O和网络I/O上 ,虽然固态硬盘SSD读取很快,但是和CPU比起来却不在一个数量级上,而且网络上的一个数据包来回时间更慢,所以一些C ...

  8. python 调用js代码

    Python2   安装pyv8 pip install-egit://github.com/brokenseal/PyV8-OS-X#egg=pyv8 from pyv8 import PyV8 c ...

  9. python之value和布尔值

    之前做判断的时候如果遇到空列表,空字符串,可以直接使用当做判断条件.比如: s = "" if s: print(s) 不是打印s的,也就在这里if的判断条件是False. 所以, ...

  10. lr_场景设计之知识点-集合点、loadgenerator

    1.controller原理 通过场景设计来模拟用户的真实操作并调用bugen中的脚本,再通过设置的压力机产生压力,在场景运行中实时监控用户的执行情况,tps,响应时间,吞吐量,服务器资源使用情况: ...