您的位置:首页 > 编程语言 > Java开发

Spring boot整合Mybatis-plus过程解析

2020-04-01 07:06 921 查看

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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring boot 整合 Mybatis plus