springboot整合Mybatis-plus

1.添加pom引用

maven的引用很簡單,官方已經給出starter,不須要咱們考慮它的依賴關係了,此處使用的是2.3版本。css

<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特別好用,會自動將下劃線格式的表字段,轉換爲以駝峯格式命名的屬性。java

官方提供的yml配置:mysql

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的數據源不起做用),包配置,以及一些基本的生成策略...總之仍是參考一下個人另外一篇文章吧,謝謝!git

執行,刷新,得到自動生成的業務代碼,再也不贅述。github

注意!!!生成後必定記得在spring boot項目中添加mybatis的包掃描路徑,或@Mapper註解:spring

@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("========================啓動完畢========================");
    }
}

或:sql

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

不然會報:Error creating bean with name 'xxxServiceImpl': Unsatisfied dependency expressed through field 'baseMapper';數據庫

至此,咱們的底層增刪改查操做所有完畢!下面來編寫簡單的controller來測試效果。

express

controller:springboot

 

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
    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配置文件:

<?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">

    <!-- 通用查詢結果列-->
    <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;
    }
相關文章
相關標籤/搜索