詳解Spring boot上配置與使用mybatis plus
http://mp.baomidou.com/#/?id=%e7%ae%80%e4%bb%8b 這個是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分頁插件 */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor page = new PaginationInterceptor(); page.setDialectType("mysql"); return page; } /** * 這里全部使用mybatis-autoconfigure 已經(jī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; }
這是一個分頁插件。
代碼生成器參考官方文檔,但是他的代碼生成器可供修改的地方不多,只能控制一下代碼生成路徑之類的,自由度不高,推薦把mybatisplus 代碼生成部分單獨(dú)抽出來,修改成自己合適的,再打成jar包進(jìn)行依賴。
springboot properties文件配置
# mybatis_config mybatis.mapper-locations=classpath:com/boot/mapper/xml/*Mapper.xml mybatis.typeAliasesPackage=com.boot.entity
前一個是xml文件的路徑
后面一個時別名包路徑
在springboot的啟動類上加上注解
@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));
分頁查詢demo:
dao:返回list
List<Role> getPage(Pagination page, RoleParam param) throws DataAccessException;
xml:照著普通sql寫就可以了,其他的會自動拼接
<select id="getPage" resultMap="RoleResultMap"> select <include refid="columns"/> from ella_role <include refid="where"/> </select>
service:
public Page<EllaRole> getPage(RoleParam param) { //new 一個page 初始化傳入current當(dāng)前頁,size每頁幾個,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
接下來是一些小貼士
生成的實(shí)體里主鍵要加上@TableId注解不然會報(bào)錯
數(shù)據(jù)庫里有下劃線的字段在查詢返回是會取不到值,需要在config文件中的mybatisSqlSessionFactoryBean方法下加上
mybatisPlus.setDbColumnUnderline(true);
domain里的所有屬性都會映射到數(shù)據(jù)庫的字段上,如果你加上數(shù)據(jù)庫里沒有但要用的屬性需要在上面加上@TableField(exist = false)標(biāo)簽,這樣他會被忽略
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
通過FeignClient如何獲取文件流steam?is?close問題
這篇文章主要介紹了通過FeignClient如何獲取文件流steam?is?close問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06Java開發(fā)編程到底是用idea好還是eclipse好
這篇文章主要介紹了Java開發(fā)編程到底是用idea好還是eclipse好,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08利用Mybatis?Plus實(shí)現(xiàn)一個SQL攔截器
SQL攔截器是一種用于攔截和修改Mybatis執(zhí)行的SQL語句的工具,通過使用SQL攔截器,開發(fā)人員可以在執(zhí)行SQL語句之前或之后對其進(jìn)行修改或記錄,本文就來借助一下Mybatis-Plus實(shí)現(xiàn)一個SQL攔截器吧2023-05-05java 獲取日期的幾天前,幾個月前和幾年前的實(shí)例
下面小編就為大家?guī)硪黄猨ava 獲取日期的幾天前,幾個月前和幾年前的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10