1. 程式人生 > 程式設計 >Spring boot整合Mybatis-plus過程解析

Spring boot整合Mybatis-plus過程解析

Mybatis初期使用比較麻煩,需要很多配置檔案、實體類、dao層對映、還有很多其他的配置。初期開發使用generator可以根據表結構自動生產實體類、dao層程式碼,這樣是可以減輕一部分開發量;後期mybatis進行大量的優化,現在可以使用註解版本,自動管理dao層和配置檔案。

maven 依賴 注意:本文使用的是mysql,資料庫依賴就不展示了

   <!-- 引入mvbatie -plus starter-->
	<dependency>
	  <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>2.3</version>
    </dependency>
   <!-- 模板引擎 mybatis使用code生成程式碼需要 -->
	<dependency>
		<groupId>org.apache.velocity</groupId>
		<artifactId>velocity-engine-core</artifactId>
		<version>2.0</version>
	</dependency>

程式碼模版引擎需要velocity或freemarker(mybatis-plus預設使用velocity,兩者任選其一),這裡使用velocity

程式碼生成器

	public static void main(String[] args) {
		CodeGeneration codeGeneration = new CodeGeneration();
		codeGeneration.execute();
	}
	
	/**
	 * 
	 * @ClassName: CodeGeneration
	 * @Description: 程式碼生成器
	 */
	public void execute() {
		AutoGenerator mpg = new AutoGenerator();
		// 全域性配置
		GlobalConfig gc = new GlobalConfig();
		//生成的程式碼路徑(系統路徑)
		gc.setOutputDir("/Users/wangxiaowei/wxw/eclipseWorkSpace/study/src/main/java");
		gc.setFileOverride(true);
		gc.setActiveRecord(false);// 不需要ActiveRecord特性的請改為false
		gc.setEnableCache(false);// XML 二級快取
		gc.setBaseResultMap(true);// XML ResultMap
		gc.setBaseColumnList(false);// XML columList
		gc.setAuthor("wxw");// 作者

		// 自定義檔案命名,注意 %s 會自動填充表實體屬性!
		gc.setControllerName("%sController");
		gc.setServiceName("%sService");
		gc.setServiceImplName("%sServiceImpl");
		gc.setMapperName("%sDao");
		gc.setXmlName("%sMapper");
		mpg.setGlobalConfig(gc);

		// 資料來源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.MYSQL);
		dsc.setDriverName("com.mysql.jdbc.Driver");
		dsc.setUsername("xxx");//資料庫使用者名稱
		dsc.setPassword("xxx");//密碼
        //資料庫路徑
		dsc.setUrl(
				"jdbc:mysql://localhost:30870/horus_dev?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true");
		mpg.setDataSource(dsc);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setTablePrefix(new String[] { "" });// 此處可以修改為您的表字首
		strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
		// 需要生成的表的名稱,這裡app_user_info即為表名
		strategy.setInclude(new String[] { 
			"app_user_info",});

		strategy.setSuperServiceClass(null);
		strategy.setSuperServiceImplClass(null);
		strategy.setSuperMapperClass(null);

		mpg.setStrategy(strategy);

		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent("com.wang.study");//父包,下面的子包均在這父包之下
		pc.setController("controller");//上面生成的controller類 放到controller子包
		pc.setService("service");//上面生成的service 放到service子包,下面類似
		pc.setMapper("dao");
		pc.setEntity("pojo");
		pc.setXml("mapper");
		mpg.setPackageInfo(pc);

		// 執行生成
		mpg.execute();
	}

mybatis 基礎配置(這裡使用的properties)

#資料來源
spring.datasource.url=jdbc:mysql://localhost:30870/horus_dev?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =xxx
spring.datasource.password =xxx
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
#mapper檔案
mybatis-plus.mapper-locations=classpath:com/wang/study/mapper/*.xml

#資料庫表對應的實體類所在包
mybatis-plus.type-aliases-package=com/wang/study/pojo
#日誌 列印sql
logging.level.com.wang.study.dao=debug

mybatis-plus 分頁,在配置類裡新增以下配置

 /**
   * mybatis-plus分頁外掛<br>
   * 文件:http://mp.baomidou.com<br>
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    paginationInterceptor.setDialectType("mysql");
    return paginationInterceptor;
  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。