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?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);
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
    SpringApplication.run(SpringBootMybatisPlusApplication.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">, args);
    logger.info(</span>"========================启动完毕========================"<span style="color: rgba(0, 0, 0, 1)">);
}

}

或:

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

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

至此,我们的底层增删改查操作全部完毕!下面来编写简单的 controller 来测试效果。

controller:

 

5. 分页

 

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
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> GlobalConfiguration globalConfiguration() {
    GlobalConfiguration global </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> GlobalConfiguration();
    global.setDbType(</span>"mysql"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> global;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 配置mybatis的分页插件pageHelper
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Bean
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> PageHelper pageHelper(){
    PageHelper pageHelper </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PageHelper();
    Properties properties </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Properties();
    properties.setProperty(</span>"offsetAsPageNum","true"<span style="color: rgba(0, 0, 0, 1)">);
    properties.setProperty(</span>"rowBoundsWithCount","true"<span style="color: rgba(0, 0, 0, 1)">);
    properties.setProperty(</span>"reasonable","true"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">配置mysql数据库的方言</span>
    properties.setProperty("dialect","mysql"<span style="color: rgba(0, 0, 0, 1)">);
    pageHelper.setProperties(properties);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> pageHelper;
}

}

 

 

 

Mapper:

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

新建 UserMapper 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!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">
<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 通用查询结果列</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">sql </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="Base_Column_List"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
    id, name, age
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">sql</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">select </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="selectUserList"</span><span style="color: rgba(255, 0, 0, 1)"> resultType</span><span style="color: rgba(0, 0, 255, 1)">="User"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
    SELECT * FROM sys_user WHERE state=#{state}
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">select</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</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
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> IUserService userService;

@ResponseBody
@RequestMapping(</span>"/page"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object selectPage(Model model){

    Page page</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> Page(1,10<span style="color: rgba(0, 0, 0, 1)">);          //1表示当前页,而10表示每页的显示显示的条目数
    page </span>= userService.selectUserPage(page, "NORMAL"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> page;
}</span></pre>