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)
