首页 技术 正文
技术 2022年11月18日
0 收藏 431 点赞 5,029 浏览 3860 个字

  首先添加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());
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,076
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894