springboot之通用mapper的使用

创建项目

创建时使用mybatis和mysql
然后导入ty-mapper和连接池的依赖

<!-- 通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
<!--        连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.16</version>
        </dependency>

在yml上配置

spring:
 datasource:
   url: jdbc:mysql://127.0.0.1:3306/tinumb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
   username: root
   password: 1024
   type: com.alibaba.druid.pool.DruidDataSource

启动类添加扫包注解

@MapperScan("top.chenyp.mapper")

使用通用mapper

1.添加实体类(略)
在实体类上添加注解

@Table(name = "comment")

id实体类上添加

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
2.在mapper上写接口继承Mapper<Comment>
3.建立service使用

查询所有
mapper.selectAll();
根据id查询
mapper.selectByPrimaryKey(id)

增加
mapper.insert(实体类);

更改
首先根据id查询出来实体类
根据set方法赋值
mapper.updateByPrimaryKey(实体类);

删除
根据id删除
deleteByPrimaryKey(id)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值