欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合Mybatis-plus的實(shí)現(xiàn)

 更新時間:2020年09月25日 10:16:18   作者:不死碼農(nóng)  
這篇文章主要介紹了springboot整合Mybatis-plus的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.添加pom引用

maven的引用很簡單,官方已經(jīng)給出starter,不需要我們考慮它的依賴關(guān)系了,此處使用的是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

官方已經(jīng)提供了基于springboot的配置,將其拷貝過來放在application.yml中即可使用,此處只是將官方部分的配置刪減過一些。其中column-underline: true特別好用,會自動將下劃線格式的表字段,轉(zhuǎn)換為以駝峰格式命名的屬性。

官方提供的yml配置:

mybatis-plus:
 global-config:
  db-config:
   id-type: auto
   field-strategy: not_empty
   #駝峰下劃線轉(zhuǎn)換
   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

注意事項(xiàng):

需要更改的地方有:文件輸出路徑(根據(jù)項(xiàng)目需要定制),數(shù)據(jù)源(此類是單獨(dú)的數(shù)據(jù)庫反向生成代碼執(zhí)行文件,因此springboot的數(shù)據(jù)源不起作用),包配置,以及一些基本的生成策略...總之還是參考一下我的另一篇文章吧,謝謝!

執(zhí)行,刷新,獲得自動生成的業(yè)務(wù)代碼,不再贅述。

注意?。?!生成后一定記得在spring boot項(xiàng)目中添加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數(shù)據(jù)庫的方言
    properties.setProperty("dialect","mysql");
    pageHelper.setProperties(properties);
    return pageHelper;
  }

}

Mapper:

/**
 * User 表數(shù)據(jù)庫控制層接口
 */
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">

  <!-- 通用查詢結(jié)果列-->
  <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 表數(shù)據(jù)服務(wù)層接口實(shí)現(xiàn)類
 *
 */
@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層默認(rèn)為我們提供的很多方法,也可以調(diào)用我們自己在dao層編寫的操作數(shù)據(jù)庫的方法.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表示當(dāng)前頁,而10表示每頁的顯示顯示的條目數(shù)
    page = userService.selectUserPage(page, "NORMAL");
    return page;
  }

到此這篇關(guān)于springboot整合Mybatis-plus的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot整合Mybatis-plus內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java的WeakHashMap源碼解析及使用場景詳解

    Java的WeakHashMap源碼解析及使用場景詳解

    這篇文章主要介紹了Java的WeakHashMap源碼解析及使用場景詳解,Map本身生命周期很長,需要長期貯留內(nèi)存中,但Map中的Entry可以刪除,使用時可以從其它地方再次取得,需要的朋友可以參考下
    2023-09-09
  • Spring Cloud Config實(shí)現(xiàn)分布式配置中心

    Spring Cloud Config實(shí)現(xiàn)分布式配置中心

    這篇文章主要介紹了Spring Cloud Config實(shí)現(xiàn)分布式配置中心,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • 詳解MyBatis的Dao層實(shí)現(xiàn)和配置文件深入

    詳解MyBatis的Dao層實(shí)現(xiàn)和配置文件深入

    這篇文章主要為大家詳細(xì)介紹了MyBatis的Dao層實(shí)現(xiàn)和配置文件深入,文中的示例代碼講解詳細(xì),感興趣的小伙伴快來跟隨小編一起學(xué)習(xí)一下
    2022-07-07
  • MyBatis-Plus之邏輯刪除的實(shí)現(xiàn)

    MyBatis-Plus之邏輯刪除的實(shí)現(xiàn)

    這篇文章主要介紹了MyBatis-Plus之邏輯刪除的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 解決	Spring RestTemplate post傳遞參數(shù)時報錯問題

    解決 Spring RestTemplate post傳遞參數(shù)時報錯問題

    本文詳解說明了RestTemplate post傳遞參數(shù)時報錯的問題及其原由,需要的朋友可以參考下
    2020-02-02
  • Java設(shè)計模式中的建造者模式詳解

    Java設(shè)計模式中的建造者模式詳解

    這篇文章主要介紹了Java設(shè)計模式中的建造者模式詳解,建造者模式使我們?nèi)粘9ぷ髦斜容^常見的一種設(shè)計模式,和工廠模式一樣屬于創(chuàng)建型設(shè)計模式,用于解耦對象創(chuàng)建和對象使用的邏輯,需要的朋友可以參考下
    2023-12-12
  • Java中Static關(guān)鍵字的五種用法詳解

    Java中Static關(guān)鍵字的五種用法詳解

    這篇文章主要介紹了Java中static的五種用法:修飾成員變量,修飾成員方法,修飾內(nèi)部類,靜態(tài)代碼塊,靜態(tài)導(dǎo)包,想詳細(xì)了解的小伙伴可以參考閱讀本文
    2023-03-03
  • java中原碼、反碼與補(bǔ)碼的問題分析

    java中原碼、反碼與補(bǔ)碼的問題分析

    本篇文章介紹了,在java中原碼、反碼與補(bǔ)碼的問題分析。需要的朋友參考下
    2013-04-04
  • java實(shí)現(xiàn)砸金蛋抽獎功能

    java實(shí)現(xiàn)砸金蛋抽獎功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)砸金蛋抽獎功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Java后端中dto、vo、entity的區(qū)別淺析

    Java后端中dto、vo、entity的區(qū)別淺析

    這篇文章主要給大家介紹了關(guān)于Java后端中dto、vo、entity區(qū)別的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-01-01

最新評論