温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用springboot实现整合Mybatis-plus

发布时间:2020-10-30 16:11:41 来源:亿速云 阅读:167 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关使用springboot实现整合Mybatis-plus,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1.添加pom引用

maven的引用很简单,官方已经给出starter,不需要我们考虑它的依赖关系了,此处使用的是2.3版本。

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>2.3</version>
</dependency>

2.配置

server.port=8080
 
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/ease-run&#63;useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#mybatis-plus
mybatis-plus.mapper-locations=classpath:com/mht/springbootmybatisplus/mapper/xml/*.xml
mybatis-plus.type-aliases-package=com.mht.springbootmybatisplus.entity
mybatis-plus.configuration.map-underscore-to-camel-case: true

官方已经提供了基于springboot的配置,将其拷贝过来放在application.yml中即可使用,此处只是将官方部分的配置删减过一些。其中column-underline: true特别好用,会自动将下划线格式的表字段,转换为以驼峰格式命名的属性。

官方提供的yml配置:

mybatis-plus:
 global-config:
  db-config:
   id-type: auto
   field-strategy: not_empty
   #驼峰下划线转换
   column-underline: true
   #逻辑删除配置
   logic-delete-value: 0
   logic-not-delete-value: 1
   db-type: mysql
  refresh: false
 configuration:
  map-underscore-to-camel-case: true
  cache-enabled: false

注意事项:

需要更改的地方有:文件输出路径(根据项目需要定制),数据源(此类是单独的数据库反向生成代码执行文件,因此springboot的数据源不起作用),包配置,以及一些基本的生成策略...总之还是参考一下我的另一篇文章吧,谢谢!

执行,刷新,获得自动生成的业务代码,不再赘述。

注意!!!生成后一定记得在spring boot项目中添加mybatis的包扫描路径,或@Mapper注解:

@SpringBootApplication
@MapperScan("com.mht.springbootmybatisplus.mapper")
public class SpringBootMybatisPlusApplication {
  private static final Logger logger = LoggerFactory.getLogger(SpringBootMybatisPlusApplication.class);
 
  public static void main(String[] args) {
    SpringApplication.run(SpringBootMybatisPlusApplication.class, args);
    logger.info("========================启动完毕========================");
  }
}

或:

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

否则会报:Error creating bean with name 'xxxServiceImpl': Unsatisfied dependency expressed through field 'baseMapper';

至此,我们的底层增删改查操作全部完毕!

3.分页

1.添加配置文件,此处配置文件表示开启mybatis-plus分页功能

@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
 
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
  }
}

或者:

package com.paic.ocss.gateway.dao.config;

import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.github.pagehelper.PageHelper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.Properties;

@Configuration
@MapperScan("com.paic.ocss.gateway.dao.mapper*")
@Import(value = { com.paic.ocss.monitor.cat.mybatis.SpringCloudCatMybatisConfig.class })
public class MybatisConfig {

  @Bean
  public GlobalConfiguration globalConfiguration() {
    GlobalConfiguration global = new GlobalConfiguration();
    global.setDbType("mysql");
    return global;
  }

  /**
   * 配置mybatis的分页插件pageHelper
   * @return
   */
  @Bean
  public PageHelper pageHelper(){
    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    properties.setProperty("offsetAsPageNum","true");
    properties.setProperty("rowBoundsWithCount","true");
    properties.setProperty("reasonable","true");
    //配置mysql数据库的方言
    properties.setProperty("dialect","mysql");
    pageHelper.setProperties(properties);
    return pageHelper;
  }

}

Mapper:

/**
 * User 表数据库控制层接口
 */
public interface UserMapper extends BaseMapper<User> {
  @Select("selectUserList")
  List<User> selectUserList(Pagination page,String state);
}

新建UserMapper配置文件:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.baomidou.springmvc.mapper.system.UserMapper">

  <!-- 通用查询结果列-->
  <sql id="Base_Column_List">
    id, name, age
  </sql>

  <select id="selectUserList" resultType="User">
    SELECT * FROM sys_user WHERE state=#{state}
  </select>
</mapper>

4.新建service层类UserService:

/**
 *
 * User 表数据服务层接口实现类
 *
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User>{
  public Page<User> selectUserPage(Page<User> page, String state) {
    page.setRecords(baseMapper.selectUserList(page,state));
    return page;
  }
}

UserService继承了ServiceImpl类,mybatis-plus通过这种方式为我们注入了UserMapper,这样可以使用service层默认为我们提供的很多方法,也可以调用我们自己在dao层编写的操作数据库的方法.Page类是mybatis-plus提供分页功能的一个model,继承了Pagination,这样我们也不需要自己再编写一个Page类,直接使用即可.

5,新建controller层UserController:

@Controller
public class UserController extends BaseController {

  @Autowired
  private IUserService userService;

  @ResponseBody
  @RequestMapping("/page")
  public Object selectPage(Model model){

    Page page=new Page(1,10);     //1表示当前页,而10表示每页的显示显示的条目数
    page = userService.selectUserPage(page, "NORMAL");
    return page;
  }

上述就是小编为大家分享的使用springboot实现整合Mybatis-plus了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI