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

Mybatis-Plus 搭建與使用入門(小結(jié))

 更新時(shí)間:2018年06月06日 14:20:33   作者:殷天文  
Mybatis-Plus(簡稱MP)是一個(gè) Mybatis 的增強(qiáng)工具,這篇文章主要介紹了Mybatis-Plus 搭建與使用入門(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

Mybatis-Plus(簡稱MP)是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生。

中文文檔 :http://baomidou.oschina.io/mybatis-plus-doc/#/

本文介紹包括

1)如何搭建
2)代碼生成(controller、service、mapper、xml)
3)單表的CRUD、條件查詢、分頁 基類已經(jīng)為你做好了

一、如何搭建

1. 首先我們創(chuàng)建一個(gè) springboot 工程 --> https://start.spring.io/

2. maven 依賴

  <dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>2.3</version>
  </dependency>
  <!-- velocity 依賴,用于代碼生成 -->
  <dependency>
   <groupId>org.apache.velocity</groupId>
   <artifactId>velocity-engine-core</artifactId>
   <version>2.0</version>
  </dependency>

3. 配置(因?yàn)楦杏X太啰嗦,這里省略了數(shù)據(jù)源的配置)

application.properties

mybatis-plus.mapper-locations=classpath:/mapper/*Mapper.xml
mybatis-plus.typeAliasesPackage=com.taven.web.springbootmp.entity
mybatis-plus.global-config.id-type=3
mybatis-plus.global-config.field-strategy=2
mybatis-plus.global-config.db-column-underline=true
mybatis-plus.global-config.key-generator=com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
mybatis-plus.global-config.logic-delete-value=1
mybatis-plus.global-config.logic-not-delete-value=0
mybatis-plus.global-config.sql-injector=com.baomidou.mybatisplus.mapper.LogicSqlInjector
#這里需要改成你的類
mybatis-plus.global-config.meta-object-handler=com.taven.web.springbootmp.MyMetaObjectHandler
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false
mybatis-plus.configuration.jdbc-type-for-null=null

配置類 MybatisPlusConfig

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.incrementer.H2KeyGenerator;
import com.baomidou.mybatisplus.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.mapper.ISqlInjector;
import com.baomidou.mybatisplus.mapper.LogicSqlInjector;
import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.taven.web.springbootmp.MyMetaObjectHandler;

@EnableTransactionManagement
@Configuration
@MapperScan("com.taven.web.springbootmp.mapper")
public class MybatisPlusConfig {
 /**
  * mybatis-plus SQL執(zhí)行效率插件【生產(chǎn)環(huán)境可以關(guān)閉】
  */
 @Bean
 public PerformanceInterceptor performanceInterceptor() {
  return new PerformanceInterceptor();
 }

 /*
  * 分頁插件,自動(dòng)識別數(shù)據(jù)庫類型 多租戶,請參考官網(wǎng)【插件擴(kuò)展】
  */
 @Bean
 public PaginationInterceptor paginationInterceptor() {
  return new PaginationInterceptor();
 }

 @Bean
 public MetaObjectHandler metaObjectHandler() {
  return new MyMetaObjectHandler();
 }

 /**
  * 注入主鍵生成器
  */
 @Bean
 public IKeyGenerator keyGenerator() {
  return new H2KeyGenerator();
 }

 /**
  * 注入sql注入器
  */
 @Bean
 public ISqlInjector sqlInjector() {
  return new LogicSqlInjector();
 }

}
import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 注入公共字段自動(dòng)填充,任選注入方式即可
 */
//@Component
public class MyMetaObjectHandler extends MetaObjectHandler {

 protected final static Logger logger = LoggerFactory.getLogger(Application.class);

 @Override
 public void insertFill(MetaObject metaObject) {
  logger.info("新增的時(shí)候干點(diǎn)不可描述的事情");
 }

 @Override
 public void updateFill(MetaObject metaObject) {
  logger.info("更新的時(shí)候干點(diǎn)不可描述的事情");
 }
}

二、代碼生成

執(zhí)行 junit 即可生成controller、service接口及實(shí)現(xiàn)、mapper及xml

import org.junit.Test;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * <p>
 * 測試生成代碼
 * </p>
 *
 * @author K神
 * @date 2017/12/18
 */
public class GeneratorServiceEntity {

 @Test
 public void generateCode() {
  String packageName = "com.taven.web.springbootmp";
  boolean serviceNameStartWithI = false;//user -> UserService, 設(shè)置成true: user -> IUserService
  generateByTables(serviceNameStartWithI, packageName, "cable", "station");//修改為你的表名
 }

 private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
  GlobalConfig config = new GlobalConfig();
  String dbUrl = "jdbc:mysql://localhost:3306/communicate";
  DataSourceConfig dataSourceConfig = new DataSourceConfig();
  dataSourceConfig.setDbType(DbType.MYSQL)
    .setUrl(dbUrl)
    .setUsername("root")
    .setPassword("root")
    .setDriverName("com.mysql.jdbc.Driver");
  StrategyConfig strategyConfig = new StrategyConfig();
  strategyConfig
    .setCapitalMode(true)
    .setEntityLombokModel(false)
    .setDbColumnUnderline(true)
    .setNaming(NamingStrategy.underline_to_camel)
    .setInclude(tableNames);//修改替換成你需要的表名,多個(gè)表名傳數(shù)組
  config.setActiveRecord(false)
    .setEnableCache(false)
    .setAuthor("殷天文")
    .setOutputDir("E:\\dev\\stsdev\\spring-boot-mp\\src\\main\\java")
    .setFileOverride(true);
  if (!serviceNameStartWithI) {
   config.setServiceName("%sService");
  }
  new AutoGenerator().setGlobalConfig(config)
    .setDataSource(dataSourceConfig)
    .setStrategy(strategyConfig)
    .setPackageInfo(
      new PackageConfig()
        .setParent(packageName)
        .setController("controller")
        .setEntity("entity")
    ).execute();
 }

// private void generateByTables(String packageName, String... tableNames) {
//  generateByTables(true, packageName, tableNames);
// }
}

到這一步搭建已經(jīng)基本完成了,下面就可以開始使用了!

三、使用 Mybatis-Plus

首先我們執(zhí)行 上面的 generateCode() 會為我們基于 表結(jié)構(gòu) 生成以下代碼(xml是我手動(dòng)移到下面的),service 和 mapper 已經(jīng)繼承了基類,為我們封裝了很多方法,下面看幾個(gè)簡單的例子。

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 殷天文
 * @since 2018-05-31
 */
@Controller
@RequestMapping("/cable")
public class CableController {
 
 @Autowired private CableService cableService;
 
 /**
  * list 查詢測試
  * 
  */
 @RequestMapping("/1")
 @ResponseBody
 public Object test1() {
  // 構(gòu)造實(shí)體對應(yīng)的 EntityWrapper 對象,進(jìn)行過濾查詢
  EntityWrapper<Cable> ew = new EntityWrapper<>();
  ew.where("type={0}", 1)
    .like("name", "王")
    .and("core_number={0}", 24)
    .and("is_delete=0");
  List<Cable> list = cableService.selectList(ew);
  List<Map<String, Object>> maps = cableService.selectMaps(ew);
  System.out.println(list);
  System.out.println(maps);
  return "ok";
 }
 
 /**
  * 分頁 查詢測試
  */
 @RequestMapping("/2")
 @ResponseBody
 public Object test2() {
  // 構(gòu)造實(shí)體對應(yīng)的 EntityWrapper 對象,進(jìn)行過濾查詢
  EntityWrapper<Cable> ew = new EntityWrapper<>();
  ew.where("type={0}", 1)
//    .like("name", "王")
    .and("core_number={0}", 24)
    .and("is_delete=0");
  Page<Cable> page = new Page<>(1,10);
  Page<Cable> pageRst = cableService.selectPage(page, ew);
  return pageRst;
 }
 
 /**
  * 自定義查詢字段
  */
 @RequestMapping("/3")
 @ResponseBody
 public Object test3() {
  Object vl = null;
  // 構(gòu)造實(shí)體對應(yīng)的 EntityWrapper 對象,進(jìn)行過濾查詢
  EntityWrapper<Cable> ew = new EntityWrapper<>();
  ew.setSqlSelect("id, `name`, "
    + "case type\n" + 
    "when 1 then '220kv'\n" + 
    "end typeName")
    .where("type={0}", 1)
//    .like("name", "王")
    .where(false, "voltage_level=#{0}", vl);//當(dāng)vl 為空時(shí),不拼接
  Page<Cable> page = new Page<>(1,10);
  Page<Cable> pageRst = cableService.selectPage(page, ew);
  return pageRst;
 }
 
 /**
  * insert
  */
 @RequestMapping("/4")
 @ResponseBody
 public Object test4() {
  Cable c = new Cable();
  c.setName("測試光纜");
  cableService.insert(c);
  return "ok";
 }
 
 /**
  * update
  */
 @RequestMapping("/5")
 @ResponseBody
 public Object test5() {
  Cable c = cableService.selectById(22284l);
  c.setName("測試光纜2222");
  c.setType(1);
  cableService.updateById(c);
  return "ok";
 } 
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 中 Date 與 Calendar 之間的編輯與轉(zhuǎn)換實(shí)例詳解

    Java 中 Date 與 Calendar 之間的編輯與轉(zhuǎn)換實(shí)例詳解

    這篇文章主要介紹了Java 中 Date 與 Calendar 之間的編輯與轉(zhuǎn)換 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • RestTemplate get請求攜帶headers自動(dòng)拼接參數(shù)方式

    RestTemplate get請求攜帶headers自動(dòng)拼接參數(shù)方式

    這篇文章主要介紹了RestTemplate get請求攜帶headers自動(dòng)拼接參數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java多線程之多線程異常捕捉

    Java多線程之多線程異常捕捉

    在java多線程程序中,所有線程都不允許拋出未捕獲的checked exception,也就是說各個(gè)線程需要自己把自己的checked exception處理掉,通過此篇文章給大家分享Java多線程之多線程異常捕捉,需要的朋友可以參考下
    2015-08-08
  • java加密MD5實(shí)現(xiàn)及密碼驗(yàn)證代碼實(shí)例

    java加密MD5實(shí)現(xiàn)及密碼驗(yàn)證代碼實(shí)例

    這篇文章主要介紹了java加密MD5實(shí)現(xiàn)及密碼驗(yàn)證代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 在idea 中添加和刪除模塊Module操作

    在idea 中添加和刪除模塊Module操作

    這篇文章主要介紹了在idea 中添加和刪除模塊Module操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • JVM優(yōu)先級線程池做任務(wù)隊(duì)列的實(shí)現(xiàn)方法

    JVM優(yōu)先級線程池做任務(wù)隊(duì)列的實(shí)現(xiàn)方法

    這篇文章主要介紹了JVM優(yōu)先級線程池做任務(wù)隊(duì)列的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java并發(fā)編程中構(gòu)建自定義同步工具

    Java并發(fā)編程中構(gòu)建自定義同步工具

    這篇文章主要介紹了Java并發(fā)編程中構(gòu)建自定義同步工具,本文講解了可阻塞狀態(tài)依賴操作的結(jié)構(gòu)、有界緩存實(shí)現(xiàn)基類示例、阻塞實(shí)現(xiàn)方式一:拋異常給調(diào)用者、阻塞實(shí)現(xiàn)方式二:通過輪詢和休眠、阻塞實(shí)現(xiàn)方式三:條件隊(duì)列等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Java concurrency集合之ConcurrentSkipListSet_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java concurrency集合之ConcurrentSkipListSet_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了Java concurrency集合之ConcurrentSkipListSet的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 使用MyBatis 動(dòng)態(tài)update數(shù)據(jù)

    使用MyBatis 動(dòng)態(tài)update數(shù)據(jù)

    使用mybatis寫sql,需要?jiǎng)討B(tài)更新對象數(shù)據(jù),每次需要更新的字段不同,為了防止null空異常,就需要用動(dòng)態(tài)sql了,下面給大家分享一段代碼關(guān)于mybatis動(dòng)態(tài)update,需要的朋友參考下
    2016-11-11
  • Java之哈夫曼壓縮原理案例講解

    Java之哈夫曼壓縮原理案例講解

    這篇文章主要介紹了Java之哈夫曼壓縮原理案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評論