MyBatisPlus使用流程

发布于:2024-05-25 ⋅ 阅读:(155) ⋅ 点赞:(0)

引入依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.4</version>
</dependency>

版本号根据需要选取

在实体类上加注解声明,表信息

根据数据库字段与所建立类是否符合默认规则,不符合就用注解处理。

使用@TableName、@TableId、@TableField等

在application.yml添加配置

常见配置

mybatis-plus:
  type-aliases-package: com.le.domain.po #别名扫描包
  mapper-locations: "classpath*:/mapper/**/*.xml" #Mapper.xml文件地址,默认值
  configuration:
    map-underscore-to-camel-case: true #是否开启下划线和驼峰映射
    cache-enabled: false #是否开启二级缓存
  global-config:
    db-config:
      id-type: assign_id #id为雪花算法生成
      update-strategy: not_null #更新策略:只更新非空字段

写mapper和service层

      mapper层

 

        service层

        impl

        

下面就是使用里面的API了

注意

 springBoot项目:

要在启动类上加上@MapperScan("xxxx.mapper"),xxxx为mapper位置

 spring项目:

配置 MapperScan

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.baomidou.mybatisplus.samples.quickstart.mapper"/>

</bean>

调整 SqlSessionFactory 为 MyBatis-Plus 的 SqlSessionFactory

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"/>

</bean>


网站公告

今日签到

点亮在社区的每一天
去签到