springboot整合Mybatis-plus的實(shí)現(xiàn)
1.添加pom引用
maven的引用很簡(jiǎn)單,官方已經(jīng)給出starter,不需要我們考慮它的依賴關(guān)系了,此處使用的是2.3版本。
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>2.3</version> </dependency>
2.配置
server.port=8080 #mysql spring.datasource.url=jdbc:mysql://localhost:3306/ease-run?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver #mybatis-plus mybatis-plus.mapper-locations=classpath:com/mht/springbootmybatisplus/mapper/xml/*.xml mybatis-plus.type-aliases-package=com.mht.springbootmybatisplus.entity mybatis-plus.configuration.map-underscore-to-camel-case: true
官方已經(jīng)提供了基于springboot的配置,將其拷貝過(guò)來(lái)放在application.yml中即可使用,此處只是將官方部分的配置刪減過(guò)一些。其中column-underline: true特別好用,會(huì)自動(dòng)將下劃線格式的表字段,轉(zhuǎn)換為以駝峰格式命名的屬性。
官方提供的yml配置:
mybatis-plus: global-config: db-config: id-type: auto field-strategy: not_empty #駝峰下劃線轉(zhuǎn)換 column-underline: true #邏輯刪除配置 logic-delete-value: 0 logic-not-delete-value: 1 db-type: mysql refresh: false configuration: map-underscore-to-camel-case: true cache-enabled: false
注意事項(xiàng):
需要更改的地方有:文件輸出路徑(根據(jù)項(xiàng)目需要定制),數(shù)據(jù)源(此類是單獨(dú)的數(shù)據(jù)庫(kù)反向生成代碼執(zhí)行文件,因此springboot的數(shù)據(jù)源不起作用),包配置,以及一些基本的生成策略...總之還是參考一下我的另一篇文章吧,謝謝!
執(zhí)行,刷新,獲得自動(dòng)生成的業(yè)務(wù)代碼,不再贅述。
注意?。。∩珊笠欢ㄓ浀迷趕pring boot項(xiàng)目中添加mybatis的包掃描路徑,或@Mapper注解:
@SpringBootApplication @MapperScan("com.mht.springbootmybatisplus.mapper") public class SpringBootMybatisPlusApplication { private static final Logger logger = LoggerFactory.getLogger(SpringBootMybatisPlusApplication.class); public static void main(String[] args) { SpringApplication.run(SpringBootMybatisPlusApplication.class, args); logger.info("========================啟動(dòng)完畢========================"); } }
或:
@Mapper public interface UserMapper extends BaseMapper<User> { }
否則會(huì)報(bào):Error creating bean with name 'xxxServiceImpl': Unsatisfied dependency expressed through field 'baseMapper';
至此,我們的底層增刪改查操作全部完畢!
3.分頁(yè)
1.添加配置文件,此處配置文件表示開啟mybatis-plus分頁(yè)功能
@EnableTransactionManagement @Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
或者:
package com.paic.ocss.gateway.dao.config; import com.baomidou.mybatisplus.entity.GlobalConfiguration; import com.github.pagehelper.PageHelper; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import java.util.Properties; @Configuration @MapperScan("com.paic.ocss.gateway.dao.mapper*") @Import(value = { com.paic.ocss.monitor.cat.mybatis.SpringCloudCatMybatisConfig.class }) public class MybatisConfig { @Bean public GlobalConfiguration globalConfiguration() { GlobalConfiguration global = new GlobalConfiguration(); global.setDbType("mysql"); return global; } /** * 配置mybatis的分頁(yè)插件pageHelper * @return */ @Bean public PageHelper pageHelper(){ PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("offsetAsPageNum","true"); properties.setProperty("rowBoundsWithCount","true"); properties.setProperty("reasonable","true"); //配置mysql數(shù)據(jù)庫(kù)的方言 properties.setProperty("dialect","mysql"); pageHelper.setProperties(properties); return pageHelper; } }
Mapper:
/** * User 表數(shù)據(jù)庫(kù)控制層接口 */ public interface UserMapper extends BaseMapper<User> { @Select("selectUserList") List<User> selectUserList(Pagination page,String state); }
新建UserMapper配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.baomidou.springmvc.mapper.system.UserMapper"> <!-- 通用查詢結(jié)果列--> <sql id="Base_Column_List"> id, name, age </sql> <select id="selectUserList" resultType="User"> SELECT * FROM sys_user WHERE state=#{state} </select> </mapper>
4.新建service層類UserService:
/** * * User 表數(shù)據(jù)服務(wù)層接口實(shí)現(xiàn)類 * */ @Service public class UserService extends ServiceImpl<UserMapper, User>{ public Page<User> selectUserPage(Page<User> page, String state) { page.setRecords(baseMapper.selectUserList(page,state)); return page; } }
UserService繼承了ServiceImpl類,mybatis-plus通過(guò)這種方式為我們注入了UserMapper,這樣可以使用service層默認(rèn)為我們提供的很多方法,也可以調(diào)用我們自己在dao層編寫的操作數(shù)據(jù)庫(kù)的方法.Page類是mybatis-plus提供分頁(yè)功能的一個(gè)model,繼承了Pagination,這樣我們也不需要自己再編寫一個(gè)Page類,直接使用即可.
5,新建controller層UserController:
@Controller public class UserController extends BaseController { @Autowired private IUserService userService; @ResponseBody @RequestMapping("/page") public Object selectPage(Model model){ Page page=new Page(1,10); //1表示當(dāng)前頁(yè),而10表示每頁(yè)的顯示顯示的條目數(shù) page = userService.selectUserPage(page, "NORMAL"); return page; }
到此這篇關(guān)于springboot整合Mybatis-plus的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot整合Mybatis-plus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java的WeakHashMap源碼解析及使用場(chǎng)景詳解
這篇文章主要介紹了Java的WeakHashMap源碼解析及使用場(chǎng)景詳解,Map本身生命周期很長(zhǎng),需要長(zhǎng)期貯留內(nèi)存中,但Map中的Entry可以刪除,使用時(shí)可以從其它地方再次取得,需要的朋友可以參考下2023-09-09Spring Cloud Config實(shí)現(xiàn)分布式配置中心
這篇文章主要介紹了Spring Cloud Config實(shí)現(xiàn)分布式配置中心,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04詳解MyBatis的Dao層實(shí)現(xiàn)和配置文件深入
這篇文章主要為大家詳細(xì)介紹了MyBatis的Dao層實(shí)現(xiàn)和配置文件深入,文中的示例代碼講解詳細(xì),感興趣的小伙伴快來(lái)跟隨小編一起學(xué)習(xí)一下2022-07-07MyBatis-Plus之邏輯刪除的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus之邏輯刪除的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11解決 Spring RestTemplate post傳遞參數(shù)時(shí)報(bào)錯(cuò)問(wèn)題
本文詳解說(shuō)明了RestTemplate post傳遞參數(shù)時(shí)報(bào)錯(cuò)的問(wèn)題及其原由,需要的朋友可以參考下2020-02-02java實(shí)現(xiàn)砸金蛋抽獎(jiǎng)功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)砸金蛋抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11