国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

springboot整合Mybatis-plus的實(shí)現(xiàn)

瀏覽:4日期:2023-04-21 16:39:18

1.添加pom引用

maven的引用很簡單,官方已經(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 #mysqlspring.datasource.url=jdbc:mysql://localhost:3306/ease-run?useUnicode=true&characterEncoding=utf8spring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver#mybatis-plusmybatis-plus.mapper-locations=classpath:com/mht/springbootmybatisplus/mapper/xml/*.xmlmybatis-plus.type-aliases-package=com.mht.springbootmybatisplus.entitymybatis-plus.configuration.map-underscore-to-camel-case: true

官方已經(jīng)提供了基于springboot的配置,將其拷貝過來放在application.yml中即可使用,此處只是將官方部分的配置刪減過一些。其中column-underline: true特別好用,會自動(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ù)庫反向生成代碼執(zhí)行文件,因此springboot的數(shù)據(jù)源不起作用),包配置,以及一些基本的生成策略...總之還是參考一下我的另一篇文章吧,謝謝!

執(zhí)行,刷新,獲得自動(dòng)生成的業(yè)務(wù)代碼,不再贅述。

注意!!!生成后一定記得在spring 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)完畢========================'); }}

或:

@Mapperpublic interface UserMapper extends BaseMapper<User> {}

否則會報(bào):Error creating bean with name ’xxxServiceImpl’: Unsatisfied dependency expressed through field ’baseMapper’;

至此,我們的底層增刪改查操作全部完畢!

3.分頁

1.添加配置文件,此處配置文件表示開啟mybatis-plus分頁功能

@EnableTransactionManagement@Configurationpublic 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的分頁插件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ù)庫的方言 properties.setProperty('dialect','mysql'); pageHelper.setProperties(properties); return pageHelper; }}

Mapper:

/** * User 表數(shù)據(jù)庫控制層接口 */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 resultType='User'> SELECT * FROM sys_user WHERE state=#{state} </select></mapper>

4.新建service層類UserService:

/** * * User 表數(shù)據(jù)服務(wù)層接口實(shí)現(xiàn)類 * */@Servicepublic 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通過這種方式為我們注入了UserMapper,這樣可以使用service層默認(rèn)為我們提供的很多方法,也可以調(diào)用我們自己在dao層編寫的操作數(shù)據(jù)庫的方法.Page類是mybatis-plus提供分頁功能的一個(gè)model,繼承了Pagination,這樣我們也不需要自己再編寫一個(gè)Page類,直接使用即可.

5,新建controller層UserController:

@Controllerpublic 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)前頁,而10表示每頁的顯示顯示的條目數(shù) page = userService.selectUserPage(page, 'NORMAL'); return page; }

到此這篇關(guān)于springboot整合Mybatis-plus的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot整合Mybatis-plus內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 青草青99久久99九九99九九九 | 亚洲 欧美 日韩中文字幕一区二区 | 成人免费高清视频网址 | 亚洲综合无码一区二区 | 韩国在线精品福利视频在线观看 | 欧美成人午夜片一一在线观看 | 永久天堂 | 国产精品亚洲二区在线 | 国产精品6 | 国产午夜在线观看视频播放 | 国产亚洲精彩视频 | 中文字幕一区日韩在线视频 | 国产午夜精品一区二区三区不卡 | 国产在线黄| 中文在线亚洲 | 91资源在线观看 | 经典三级久久久久 | 亚洲精品天堂一区 | 欧美日韩精品一区二区 | 99久久亚洲国产高清观看 | 亚洲午夜精品久久久久久抢 | 欧美高清在线精品一区二区不卡 | 日本高清aⅴ毛片免费 | 欧美一级片在线看 | 亚洲精品欧美精品一区二区 | 久久综合日韩亚洲精品色 | 久久不见久久见免费影院www日本 | 欧美巨乳在线观看 | 免费黄色毛片视频 | 在线不卡一区二区 | 日韩国产在线观看 | 日本强不卡在线观看 | 国产精品自拍亚洲 | 永久免费观看午夜视频在线 | 欧美资源在线观看 | 久久99精品视频在线在线观看 | 亚洲经典三级 | 欧美日韩在线观看区一二 | 国产美女一级特黄毛片 | 国产最爽的乱淫视频国语对 | 全部免费的毛片在线看美国 |