springboot整合mybatics PLUS
首先添加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的更多相关文章
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- springboot整合mybaits注解开发
springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
- SpringBoot整合SpringCloud搭建分布式应用
什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...
- SpringBoot整合RabbitMQ-整合演示
本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...
随机推荐
- QTL定位相关
1.原理 https://www.sohu.com/a/211301179_278730 较为详细
- package.json bin的作用
许多包有一个或多个可执行文件(executable),他们希望直接导入到全局路径里面,这样可以直接使用,npm很容易达到这点, A lot of packages have one or more e ...
- 华为的云计算基于openstack #stackalytics#
华为的云计算基于openstack,具体实力可以参考openstack 社区提供的stackalytics http://stackalytics.com/
- vue 渲染后更新数据
this.$set(this.selGetData,level,[{},{}])this.$set(this.selGetData,1,{message:"Test add attr&quo ...
- Python --判断路径是否为目录或文件
os.path.isdir( ), os.path.isfile(),os.listdir( ), os.walk( ) 参考网址:https://blog.csdn.net/xxn_723911/a ...
- 客户端与服务器之间通信收不到信息——readLine()
写服务器端和客户端之间通信,结果一直读取不到信息,在https://blog.csdn.net/yiluxiangqian7715/article/details/50173573 上找到了原因:使用 ...
- 给MS的意见
2017-02-27 WPF的中文注释文档翻译得很烂.太多了,列举不过来. 这个是 System.Threading.Tasks.Task.Exception: 获取导致 System.Aggrega ...
- python--网络编程urllib
一.python操作网络也就是打开一个网站,或者请求一个http接口,使用urllib模块.urllib模块是一个标准模块,直接import urllib即可 from urllib import r ...
- 八大排序算法——希尔(shell)排序(动图演示 思路分析 实例代码java 复杂度分析)
一.动图演示 二.思路分析 希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序:随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止. 简单插 ...
- Eclipse无法正常启动,弹出对话框内容为 A Java Runtime...
1.Eclipse无法正常启动,弹出对话框内容为 A Java Runtime...如下图: 原因分析:由于软件版本的更新或者安装其他开发软件无意之间修改了配置文件中的路径,众所周知,Java虚拟机( ...