SpringBoot整合MybatisPlus的簡單教程實現(xiàn)(簡單整合)
最近在研究springboot,順便就會看看數(shù)據(jù)庫連接這一塊的知識 ,所以當我發(fā)現(xiàn)有通用Mapper和MybatisPlus這兩款網(wǎng)絡上比較火的簡化mybatis開發(fā)的優(yōu)秀軟件之后。就都想試一下,看看哪一款比較適合自己。
先創(chuàng)建一個springboot的項目,可以參考我之前的文章Spring Boot 的簡單教程(一) Spring Boot 項目的創(chuàng)建。
創(chuàng)建好springboot之后就需要整合mybatis和mybatis-plus了。
打開pom.xml文件,將最新的mybatis相關的包都引用進來。
<!-- 這是mysql的依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 這是lombok的依賴 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 這是mybatis-plus依賴 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<!-- 這是mybatis-plus的代碼自動生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.1</version>
</dependency>
<!-- 這是模板引擎依賴 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
需要對application.yml進行相關的配置。
#端口號
server:
port: 8088
#數(shù)據(jù)庫的配置信息
spring:
datasource:
url: jdbc:mysql://localhost:3306/*** #自己的數(shù)據(jù)庫名稱
username: root
password: 123456
mybatis:
#開啟駝峰命名法
configuration:
map-underscore-to-camel-case: true
mybatis-plus:
# xml地址
mapper-locations: classpath:mapper/*Mapper.xml
# 實體掃描,多個package用逗號或者分號分隔
type-aliases-package: *** #自己的實體類地址
configuration:
# 這個配置會將執(zhí)行的sql打印出來,在開發(fā)或測試的時候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
自動生成模塊的方法,在相應的位置上添加上自己的一些包名就可以運行生成相應的Entity、Mapper、Mapper XML、Service、Controller 等各個模塊的代碼。
public class CodeGenerator {
/**
* <p>
* 讀取控制臺內(nèi)容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("請輸入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("請輸入正確的" + tip + "!");
}
public static void main(String[] args) {
// 代碼生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("jobob");
gc.setOpen(false);
// gc.setSwagger2(true); 實體屬性 Swagger2 注解
mpg.setGlobalConfig(gc);
// 數(shù)據(jù)源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/***?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("***");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
//這里有個模塊名的配置,可以注釋掉不用。
// pc.setModuleName(scanner("模塊名"));
pc.setParent("com.zhouxiaoxi.www");
mpg.setPackageInfo(pc);
// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定義輸出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定義配置會被優(yōu)先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 , 如果你 Entity 設置了前后綴、此處注意 xml 的名稱會跟著發(fā)生變化?。?
return projectPath + "/src/main/resources/mapper/"
// + + pc.getModuleName() + 如果放開上面的模塊名,這里就有一個模塊名了
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/*
cfg.setFileCreate(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判斷自定義文件夾是否需要創(chuàng)建
checkDir("調(diào)用默認方法創(chuàng)建的目錄");
return false;
}
});
*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定義輸出模板
//指定自定義模板路徑,注意不要帶上.ftl/.vm, 會根據(jù)使用的模板引擎自動識別
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
//數(shù)據(jù)庫表映射到實體的明明策略
strategy.setNaming(NamingStrategy.underline_to_camel);
//數(shù)據(jù)庫表字段映射到實體的命名策略, 未指定按照 naming 執(zhí)行
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//自定義繼承的Entity類全稱,帶包名
// strategy.setSuperEntityClass("***");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
//自定義繼承的Controller類全稱,帶包名
// strategy.setSuperControllerClass("***");
strategy.setInclude(scanner("表名,多個英文逗號分割").split(","));
//自定義基礎的Entity類,公共字段(可添加更多)
// strategy.setSuperEntityColumns("id");
//駝峰轉(zhuǎn)連字符
strategy.setControllerMappingHyphenStyle(true);
//表前綴
// strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
在生成的controller里面添加對應的方法啟動就可以正常進行訪問了。

當然還需要在 Spring Boot 啟動類中添加 @MapperScan 注解,掃描 Mapper 文件夾:
@SpringBootApplication
@MapperScan("***.*.mapper") //對應你的mapper存放的地址
public class Application {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot 項目如何在tomcat容器中運行的實現(xiàn)方法
這篇文章主要介紹了SpringBoot 項目如何在tomcat容器中運行的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
JavaSwing FlowLayout 流式布局的實現(xiàn)
這篇文章主要介紹了JavaSwing FlowLayout 流式布局的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12
Spring、SpringMVC和SpringBoot的區(qū)別及說明
這篇文章主要介紹了Spring、SpringMVC和SpringBoot的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。2022-10-10
如何使用IntelliJ IDEA的HTTP Client進行接口驗證
這篇文章主要介紹了如何使用IntelliJ IDEA的HTTP Client進行接口驗證,本文給大家分享最新完美解決方案,感興趣的朋友跟隨小編一起看看吧2024-06-06

