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. ...
随机推荐
- hadoop多文件输出MultipleOutputFormat和MultipleOutputs
1.MultipleOutputFormat可以将相似的记录输出到相同的数据集.在写每条记录之前,MultipleOutputFormat将调用generateFileNameForKeyValue方 ...
- 2018-2019-2 20165303《网络攻防技术》Exp5 MSF基础应用
实践目标 1.一个主动攻击实践 ms17_010(成功) ms08_067(成功) 2.一个针对浏览器的攻击 ms13-008(成功唯一) ms11-050(失败) ms13-069(成功唯一) ad ...
- .NetCore WebApi 添加 Log4Net
一 .配置 1.vs2019 创建一个.net core web程序,选择webapi 2.项目中添加一个配置文件:添加--新建项--XML文件,命名为log4net.config 我使用的是log4 ...
- Linux删除/boot后该如何恢复
一.其实不光boot目录被删除,也有系统启动不起来等,一直因为/boot这个目录的的问题,今天就来解决一下 Centos 6 1.进入救援,先修复vmlinz(内核) chroot /mnt/sysi ...
- js点击加载更多可以增加几条数据的显示
<div class="list"> <div class="one"> <div class="img" ...
- 使用XStream解析复杂XML并插入数据库(二)
标注黄色地方:我需要加深学习!!! 我写的是webservice,目前具体写webservice的步骤我还不清楚, 整理完小知识开始整理webservice! 针对以下格式的XML进行解析 <? ...
- php中把美国时间转为北京时间的自定义
我的服务器北京时间,php调用的时间: date.timezone ="America/Chicago" 这是美国这边的一个时间,有的时候跟北京相差13个小时,有的时候跟北京时间相 ...
- Java Design Pattern(Factory,Singleton,Prototype,Proxy)
一.Factory 设计模式: the most common pattern,create a new object ,eg. A a=new A();工厂模式的好处:工厂模式可以做到把创建对象单独 ...
- js for in循环遍历对象,获取key:value值
var testObj = { 'a':'111', 'b':'222', 'c':'333', 'd':'444'}for(var i in testObj){ console.log(i); // ...
- lumion实例渲染6.2
放置一些树 打开室外材质库,选择一个·别墅 放车 “景观”“描绘系统”笔刷调大刷出地面, 人群安置一些墙 改变方向,增大数量,墙就连在一起, 放一些树篱在墙上. 拍照模式添加特效,太阳 添加特效,天气 ...