詳解Spring boot上配置與使用mybatis plus
http://mp.baomidou.com/#/?id=%e7%ae%80%e4%bb%8b 這個(gè)是mybatisplus的官方文檔,上面是mybtisplus的配置使用方法,以及一些功能的介紹
下面開始配置
maven依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.0-beta</version>
</dependency>
config文件
@Configuration
public class MybatisPlusConfig {
@Autowired
private DataSource dataSource;
@Autowired
private MybatisProperties properties;
@Autowired
private ResourceLoader resourceLoader = new DefaultResourceLoader();
@Autowired(required = false)
private Interceptor[] interceptors;
@Autowired(required = false)
private DatabaseIdProvider databaseIdProvider;
/**
* mybatis-plus分頁(yè)插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor page = new PaginationInterceptor();
page.setDialectType("mysql");
return page;
}
/**
* 這里全部使用mybatis-autoconfigure 已經(jīng)自動(dòng)加載的資源。不手動(dòng)指定
* 配置文件和mybatis-boot的配置文件同步
* @return
*/
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
mybatisPlus.setDataSource(dataSource);
mybatisPlus.setVfs(SpringBootVFS.class);
if (StringUtils.hasText(this.properties.getConfigLocation())) {
mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
}
mybatisPlus.setConfiguration(properties.getConfiguration());
if (!ObjectUtils.isEmpty(this.interceptors)) {
mybatisPlus.setPlugins(this.interceptors);
}
MybatisConfiguration mc = new MybatisConfiguration();
mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
mybatisPlus.setConfiguration(mc);
if (this.databaseIdProvider != null) {
mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
}
if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
}
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
}
return mybatisPlus;
}
}
插件以@bean的形式添加在config文件里例如:
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor page = new PaginationInterceptor();
page.setDialectType("mysql");
return page;
}
這是一個(gè)分頁(yè)插件。
代碼生成器參考官方文檔,但是他的代碼生成器可供修改的地方不多,只能控制一下代碼生成路徑之類的,自由度不高,推薦把mybatisplus 代碼生成部分單獨(dú)抽出來(lái),修改成自己合適的,再打成jar包進(jìn)行依賴。
springboot properties文件配置
# mybatis_config mybatis.mapper-locations=classpath:com/boot/mapper/xml/*Mapper.xml mybatis.typeAliasesPackage=com.boot.entity
前一個(gè)是xml文件的路徑
后面一個(gè)時(shí)別名包路徑
在springboot的啟動(dòng)類上加上注解
@MapperScan("com.boot.mapper*")
@SpringBootApplication
public class BootApplication {
@mapperscan 里面是dao的掃描路徑
mybatisplus 提供了比較齊全的crud即增刪改查,不需要在mapper.xml里寫sql可以直接調(diào)用
例子:
//可以在controller: Egg egg = new Egg(); eggService.insert(egg); //可以在service Egg egg = new Egg(); this.selectList(new EntityWrapper<Egg >(egg));//mybatisplus提供依靠實(shí)體查詢的方法的寫法 //也可以 mapper.selectList(new EntityWrapper<Egg >(egg));
分頁(yè)查詢demo:
dao:返回list
List<Role> getPage(Pagination page, RoleParam param) throws DataAccessException;
xml:照著普通sql寫就可以了,其他的會(huì)自動(dòng)拼接
<select id="getPage" resultMap="RoleResultMap"> select <include refid="columns"/> from ella_role <include refid="where"/> </select>
service:
public Page<EllaRole> getPage(RoleParam param) {
//new 一個(gè)page 初始化傳入current當(dāng)前頁(yè),size每頁(yè)幾個(gè),order 排序(默認(rèn)asc要改的話page.setAsc(false);)
Page<Role> page = new Page<Role>(param.getCurrent(), param.getSize(), param.getOrder());
page.setRecords(iRoleMapper.getPage(page, param));
return page;
}
end
接下來(lái)是一些小貼士
生成的實(shí)體里主鍵要加上@TableId注解不然會(huì)報(bào)錯(cuò)
數(shù)據(jù)庫(kù)里有下劃線的字段在查詢返回是會(huì)取不到值,需要在config文件中的mybatisSqlSessionFactoryBean方法下加上
mybatisPlus.setDbColumnUnderline(true);
domain里的所有屬性都會(huì)映射到數(shù)據(jù)庫(kù)的字段上,如果你加上數(shù)據(jù)庫(kù)里沒(méi)有但要用的屬性需要在上面加上@TableField(exist = false)標(biāo)簽,這樣他會(huì)被忽略
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
maven項(xiàng)目編譯后沒(méi)有生成target/class文件問(wèn)題
文章介紹了在Maven項(xiàng)目中,`target/classes`目錄用于存放編譯后的字節(jié)碼文件,如果在項(xiàng)目編譯啟動(dòng)后卻沒(méi)有看到這個(gè)文件夾,可以通過(guò)取消勾選`javaoutputfolders`選項(xiàng)來(lái)解決,以便顯示編譯后的Java類文件2024-11-11
解析Java?中for循環(huán)和foreach循環(huán)哪個(gè)更快
這篇文章主要介紹了Java中for循環(huán)和foreach循環(huán)哪個(gè)更快示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
理解JDK動(dòng)態(tài)代理為什么必須要基于接口
這篇文章主要介紹了理解JDK動(dòng)態(tài)代理為什么必須要基于接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
關(guān)于spring依賴注入的方式以及優(yōu)缺點(diǎn)
這篇文章主要介紹了關(guān)于spring依賴注入的方式以及優(yōu)缺點(diǎn),依賴注入,是IOC的一個(gè)方面,是個(gè)通常的概念,它有多種解釋,這概念是說(shuō)你不用創(chuàng)建對(duì)象,而只需要描述它如何被創(chuàng)建,需要的朋友可以參考下2023-07-07
Java的常見(jiàn)熱門ORM框架優(yōu)缺點(diǎn)區(qū)別
Java?ORM框架是一種用于將Java對(duì)象映射到關(guān)系型數(shù)據(jù)庫(kù)中的工具,使得開發(fā)人員能夠通過(guò)對(duì)象操作數(shù)據(jù)庫(kù)而不必直接使用SQL查詢,Java開發(fā)變得更加高效和易于維護(hù),選擇適合你的ORM框架是根據(jù)你的需求決定的,比如你的應(yīng)用場(chǎng)景,數(shù)據(jù)結(jié)構(gòu)和技術(shù)水平等2024-02-02
一文搞懂如何實(shí)現(xiàn)Java,Spring動(dòng)態(tài)啟停定時(shí)任務(wù)
定時(shí)任務(wù)的應(yīng)用場(chǎng)景十分廣泛,如定時(shí)清理文件、定時(shí)生成報(bào)表、定時(shí)數(shù)據(jù)同步備份等。本文將教你實(shí)現(xiàn)Java、Spring動(dòng)態(tài)啟停定時(shí)任務(wù),感興趣的可以學(xué)習(xí)一下2022-06-06

