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
代码生成器
publicstaticvoidmain(String[]args){
CodeGenerationcodeGeneration=newCodeGeneration();
codeGeneration.execute();
}
/**
*
*@ClassName:CodeGeneration
*@Description:代码生成器
*/
publicvoidexecute(){
AutoGeneratormpg=newAutoGenerator();
//全局配置
GlobalConfiggc=newGlobalConfig();
//生成的代码路径(系统路径)
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);//XMLResultMap
gc.setBaseColumnList(false);//XMLcolumList
gc.setAuthor("wxw");//作者
//自定义文件命名,注意%s会自动填充表实体属性!
gc.setControllerName("%sController");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sDao");
gc.setXmlName("%sMapper");
mpg.setGlobalConfig(gc);
//数据源配置
DataSourceConfigdsc=newDataSourceConfig();
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);
//策略配置
StrategyConfigstrategy=newStrategyConfig();
strategy.setTablePrefix(newString[]{""});//此处可以修改为您的表前缀
strategy.setNaming(NamingStrategy.underline_to_camel);//表名生成策略
//需要生成的表的名称,这里app_user_info即为表名
strategy.setInclude(newString[]{
"app_user_info",
});
strategy.setSuperServiceClass(null);
strategy.setSuperServiceImplClass(null);
strategy.setSuperMapperClass(null);
mpg.setStrategy(strategy);
//包配置
PackageConfigpc=newPackageConfig();
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分页插件
*文档:http://mp.baomidou.com
*/ @Bean publicPaginationInterceptorpaginationInterceptor(){ PaginationInterceptorpaginationInterceptor=newPaginationInterceptor(); paginationInterceptor.setDialectType("mysql"); returnpaginationInterceptor; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。