栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring boot整合Mybatis-plus过程解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring boot整合Mybatis-plus过程解析

Mybatis初期使用比较麻烦,需要很多配置文件、实体类、dao层映射、还有很多其他的配置。初期开发使用generator可以根据表结构自动生产实体类、dao层代码,这样是可以减轻一部分开发量;后期mybatis进行大量的优化,现在可以使用注解版本,自动管理dao层和配置文件。

maven 依赖 注意:本文使用的是mysql,数据库依赖就不展示了

   
	
	  com.baomidou
      mybatis-plus-boot-starter
      2.3
    
   
	
		org.apache.velocity
		velocity-engine-core
		2.0
	

代码模版引擎需要velocity或freemarker(mybatis-plus默认使用velocity,两者任选其一),这里使用velocity

代码生成器

	public static void main(String[] args) {
		CodeGeneration codeGeneration = new CodeGeneration();
		codeGeneration.execute();
	}
	
	
	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 @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); paginationInterceptor.setDialectType("mysql"); return paginationInterceptor; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134632.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号