精品专区-精品自拍9-精品自拍三级乱伦-精品自拍视频-精品自拍视频曝光-精品自拍小视频

網站建設資訊

NEWS

網站建設資訊

mybatisplus如何實現在Springboot上使用

mybatis plus如何實現在Spring boot上使用?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

創新互聯是一家集網站建設,蓬溪企業網站建設,蓬溪品牌網站建設,網站定制,蓬溪網站建設報價,網絡營銷,網絡優化,蓬溪網站推廣為一體的創新建站企業,幫助傳統企業提升企業形象加強企業競爭力。可充分滿足這一群體相比中小企業更為豐富、高端、多元的互聯網需求。同時我們時刻保持專業、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們為更多的企業打造出實用型網站。

maven依賴

    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      1.1.1
    
    
      com.baomidou
      mybatis-plus
      2.0-beta
    

config文件

@Configuration
public class MybatisPlusConfig {
  @Autowired
  private DataSource dataSource;

  @Autowired
  private MybatisProperties properties;

  @Autowired
  private ResourceLoader resourceLoader = new DefaultResourceLoader();

  @Autowired(required = false)
  private Interceptor[] interceptors;

  @Autowired(required = false)
  private DatabaseIdProvider databaseIdProvider;

  /**
   *  mybatis-plus分頁插件
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor page = new PaginationInterceptor();
    page.setDialectType("MySQL");
    return page;
  }
  /**
   * 這里全部使用mybatis-autoconfigure 已經自動加載的資源。不手動指定
   * 配置文件和mybatis-boot的配置文件同步
   * @return
   */
  @Bean
  public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
    MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
    mybatisPlus.setDataSource(dataSource);
    mybatisPlus.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
      mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    mybatisPlus.setConfiguration(properties.getConfiguration());
    if (!ObjectUtils.isEmpty(this.interceptors)) {
      mybatisPlus.setPlugins(this.interceptors);
    }
    MybatisConfiguration mc = new MybatisConfiguration();
    mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    mybatisPlus.setConfiguration(mc);
    if (this.databaseIdProvider != null) {
      mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
      mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
    }
    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
      mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
      mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
    }
    return mybatisPlus;
  }
}

插件以@bean的形式添加在config文件里例如:

@Bean
  public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor page = new PaginationInterceptor();
    page.setDialectType("mysql");
    return page;
  }

這是一個分頁插件。

代碼生成器參考官方文檔,但是他的代碼生成器可供修改的地方不多,只能控制一下代碼生成路徑之類的,自由度不高,推薦把mybatisplus 代碼生成部分單獨抽出來,修改成自己合適的,再打成jar包進行依賴。

springboot properties文件配置

# mybatis_config
mybatis.mapper-locations=classpath:com/boot/mapper/xml/*Mapper.xml 
mybatis.typeAliasesPackage=com.boot.entity

前一個是xml文件的路徑

后面一個時別名包路徑

在springboot的啟動類上加上注解

@MapperScan("com.boot.mapper*")
@SpringBootApplication
public class BootApplication {

@mapperscan 里面是dao的掃描路徑

mybatisplus 提供了比較齊全的crud即增刪改查,不需要在mapper.xml里寫sql可以直接調用
例子:

//可以在controller:
Egg egg = new Egg();
eggService.insert(egg);
//可以在service
Egg egg = new Egg();
this.selectList(new EntityWrapper(egg));//mybatisplus提供依靠實體查詢的方法的寫法
//也可以
mapper.selectList(new EntityWrapper(egg));

分頁查詢demo:

dao:返回list

復制代碼 代碼如下:

List getPage(Pagination page, RoleParam param) throws DataAccessException;

xml:照著普通sql寫就可以了,其他的會自動拼接

service:

public Page getPage(RoleParam param) {
//new 一個page 初始化傳入current當前頁,size每頁幾個,order 排序(默認asc要改的話page.setAsc(false);)
    Page page = new Page(param.getCurrent(), param.getSize(), param.getOrder());
    page.setRecords(iRoleMapper.getPage(page, param));
    return page;
  }

end

接下來是一些小貼士

生成的實體里主鍵要加上@TableId注解不然會報錯

數據庫里有下劃線的字段在查詢返回是會取不到值,需要在config文件中的mybatisSqlSessionFactoryBean方法下加上

mybatisPlus.setDbColumnUnderline(true); 

domain里的所有屬性都會映射到數據庫的字段上,如果你加上數據庫里沒有但要用的屬性需要在上面加上@TableField(exist = false)標簽,這樣他會被忽略

關于mybatis plus如何實現在Spring boot上使用問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創新互聯行業資訊頻道了解更多相關知識。


網站題目:mybatisplus如何實現在Springboot上使用
網頁路徑:http://m.jcarcd.cn/article/jeedce.html
主站蜘蛛池模板: 日韩午夜场 | 欧美日韩精品乱国产 | 国产高清乱伦综合 | 爱豆传媒免费播放 | 国产精品玖 | 91福利免费| 国产综合24| 三级网站视频 | 国产中文在线不卡 | 日产亚洲一 | 日韩欧美在线视频 | 日本电影一区二区 | 日本无吗中| 国产精品福利 | 欧美亚洲日韩综艺 | 国产精品私密 | 成人日本 | 精品国产a自拍 | 三区免费观看 | 日本最新伦中文字幕 | 丝袜线观看 | 成人日韩在线观 | 欧美三级免费观看 | 欧美亚洲性爱电影 | 国产网络自拍 | 91精彩在线视频 | 九九91精品国产 | 国产目拍亚洲精品 | 无码精品毛片成人影院 | 日本免费一区二区 | 乱老熟300部视频 | 国产香线 | 成人七区免费观看 | 日本精品aⅴ在线 | 亚洲无码国产一 | 国产九九在线视频 | 日韩视频一区 | 国产精品玉足视频 | 日韩午夜福| 午夜成人精品网站 | 日韩经典一区二区 |