springboot整合Mybatis-plus的實現
1.添加pom引用
maven的引用很簡單,官方已經給出starter,不需要我們考慮它的依賴關系了,此處使用的是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
官方已經提供了基于springboot的配置,將其拷貝過來放在application.yml中即可使用,此處只是將官方部分的配置刪減過一些。其中column-underline: true特別好用,會自動將下劃線格式的表字段,轉換為以駝峰格式命名的屬性。
官方提供的yml配置:
mybatis-plus: global-config: db-config: id-type: auto field-strategy: not_empty #駝峰下劃線轉換 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
注意事項:
需要更改的地方有:文件輸出路徑(根據項目需要定制),數據源(此類是單獨的數據庫反向生成代碼執行文件,因此springboot的數據源不起作用),包配置,以及一些基本的生成策略...總之還是參考一下我的另一篇文章吧,謝謝!
執行,刷新,獲得自動生成的業務代碼,不再贅述。
注意!!!生成后一定記得在spring boot項目中添加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('========================啟動完畢========================'); }}
或:
@Mapperpublic interface UserMapper extends BaseMapper<User> {}
否則會報: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數據庫的方言 properties.setProperty('dialect','mysql'); pageHelper.setProperties(properties); return pageHelper; }}
Mapper:
/** * User 表數據庫控制層接口 */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'> <!-- 通用查詢結果列--> <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 表數據服務層接口實現類 * */@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層默認為我們提供的很多方法,也可以調用我們自己在dao層編寫的操作數據庫的方法.Page類是mybatis-plus提供分頁功能的一個model,繼承了Pagination,這樣我們也不需要自己再編寫一個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表示當前頁,而10表示每頁的顯示顯示的條目數 page = userService.selectUserPage(page, 'NORMAL'); return page; }
到此這篇關于springboot整合Mybatis-plus的實現的文章就介紹到這了,更多相關springboot整合Mybatis-plus內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: