SpringBoot通用mapper的简单使用

SpringBoot通用mapper的使用

1.添加依耐

<!-- mysql驱动 -->
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.47</version>
</dependency>
      <!-- 通用mapper -->
<dependency>
   <groupId>tk.mybatis</groupId>
   <artifactId>mapper-spring-boot-starter</artifactId>
   <version>2.1.5</version>
</dependency>
CREATE TABLE `tb_category` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '类目id',
  `name` varchar(32) NOT NULL COMMENT '类目名称',
  `parent_id` bigint(20) NOT NULL COMMENT '父类目id,顶级类目填0',
  `is_parent` tinyint(1) NOT NULL COMMENT '是否为父节点,0为否,1为是',
  `sort` int(4) NOT NULL COMMENT '排序指数,越小越靠前',
  PRIMARY KEY (`id`),
  KEY `key_parent_id` (`parent_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1424 DEFAULT CHARSET=utf8 COMMENT='商品类目表,类目和商品(spu)是一对多关系,类目与品牌是多对多关系';

2.创建pojo

@Table(name="tb_category")
public class Category {
   @Id
   @GeneratedValue(strategy= GenerationType.IDENTITY)
   private Long id;
   private String name;
   private Long parentId;
   private Boolean isParent;// 注意isParent的get和set方法 阿里不推荐使用is开头的字段,会出现缺失的问题
   private Integer sort;
   // getter和setter略
}

3.创建mapper

public interface CategoryMapper extends Mapper<Category> {
}

4.测试

@SpringBootTest
class MapperDemoApplicationTests {
    @Autowired
    CategoryMapper categoryMapper;

    @Test
    void contextLoads() {
        System.out.println("---------");
        Category t = new Category();
        t.setParentId(1L);
        List<Category> select = categoryMapper.select(t);

        if (select != null) {
            System.out.println("------========---");
            for (Category category : select) {
                System.out.println(category.getName());
                System.out.println(category.getIsParent());
            }
        }
    }

}

测试结果

------========—
电子书刊
true
音像
true
英文原版
true
文艺
true
少儿
true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用Mapper需要进行以下步骤: 1. 在启动类上添加`@MapperScan`注解来扫描Mapper包,并将继承的Mapper接口添加到MyBatis配置中。如下所示: ```java @SpringBootApplication @MapperScan("com.cloudsw.mapper") public class MapperApplication { public static void main(String[] args) { SpringApplication.run(MapperApplication.class, args); } } ``` 这样就可以将Mapper接口添加到MyBatis的配置中。 2. 配置数据库和MyBatis。在`application.yml`配置文件中添加数据库和MyBatis的配置。示例如下: ```yaml spring: datasource: name: clodsw-portal-dev url: jdbc:mysql://localhost:3306/cloudsw_portal?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&autoReconnect=true&allowPublicKeyRetrieval=true username: 123456 password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.cloudsw.dto mapper: mappers: com.cloudsw.base.IBaseMapper identity: MYSQL ``` 在上述配置中,配置了数据库连接信息和MyBatis的一些配置项。 3. 使用通用Mapper通用Mapper是一个简化数据访问操作的工具,可以减少大量的重复代码。具体使用方法可以参考官方文档。 总结起来,在Spring Boot中使用Mapper需要在启动类上添加`@MapperScan`注解,配置数据库和MyBatis的相关信息,以及按需使用通用Mapper来简化数据访问操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [一看就会一学就废之SpringBoot整合通用Mapper以及常用方法](https://blog.csdn.net/qq_45675378/article/details/115324620)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值